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

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

print(ans) 