N = int(input())
A = list(map(int, input().split()))
ans = 0
gap = 0

for i in range(1, N):
    if A[i-1] > A[i]:
        ans += A[i-1]-A[i] + gap
        gap += A[i-1]-A[i]
    else:
        gap = 0

print(ans)
