N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(len(A)-1):
    a_before = A[i]
    a_after = A[i+1]

    diff = a_before-a_after

    if diff < 0:
        ans += abs(diff)

print(ans)
