N = int(input())
A = list(map(int,input().split()))
ans = []

for i in range(N-1):
    if A[i] > A[i+1]:
        youso = A[i] - A[i+1]
        A[i+1] += youso
        ans.append(youso)
        print(ans)
    else:
        pass

print(sum(ans))
