n = int(input())
A = list(map(int,input().split()))

step = [0]
for i in A[1:]:
    if A[A.index(i)-1] >= i:
        step.append(A[A.index(i)-1] - i)
        A[A.index(i)] += A[A.index(i)-1] - i
print(sum(step))
