N = input()
A = input()

A = A.split()
A = [int(a) for a in A]

ANS = 0
while len(A) != 0:
    ANS = ANS + (len(A) - A.index(max(A))) * A[A.index(max(A))] - sum(A[A.index(max(A)):])
    A = A[:A.index(max(A))]

print(ANS)