N=int(input())
A=list(map(int,input().split()))
answer=0
for n in range(N-1):
    if A[n]>A[n+1]:
        sa=A[n]-A[n+1]
        answer+=sa
        A[n+1]=A[n]
if A[-2]>A[-1]:
    answer+=A[-2]-A[-1]
print(answer)
