n= int(input())
a = list(map(int, input().split()))
flag = True
res = 0
while flag:
    max_num = max(a)
    res += (a[a.index(max_num)] * len(a[a.index(max_num):]))- sum(a[a.index(max_num):])
    a = a[:a.index(max_num)]
    if len(a) == 0:
        flag = False

print(res)