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

x = A[0]
ans = abs(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)

    