N = int(input())
A = [int(i) for i in input().split()]

ans = 0
max_len = A[0]
for i in range(1, len(A)):
    if A[i-1] < A[i]:
        max_len = A[i]
    else:
        ans += max_len - A[i]

print(ans)



