N = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst, reverse=True)

l_count = N
count = 0

for i in lst2:
    k_index = lst.index(i)
    
    if k_index >= l_count:
        continue
    else:
        count = count + (l_count - k_index) * i
        l_count = k_index

print(count-sum(lst))