##C - Step
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N):
    ##print(i,A)
    if A != []:
        Idx = A.index(max(A))
        ans += max(A)*(len(A) - Idx) - sum(A[Idx::])
        del A[Idx::]
    else:
        break
print(ans)