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


if A[1] - A[0] >= 0:
    ans = 0
else:
    ans = A[0] - A[1]
y = 0

for i in range(2,N):
    B = A[0:i]
    y = max(B)
    if A[i] < y:
        ans += y - A[i]
    

print(ans)

    