# C
n = int(input())
A = list(map(int, input().split()))
b = sum(A)
for i in range(n-1):
    if A[i] <= A[(i+1)]:
        continue
    else:
        s = A[i] - A[(i+1)]
        s = s + A[(i+1)]
        A.pop(A[(i+1)])
        A.insert((i+1),s)

print(sum(A) - b)
