N=int(input())
A = list(map(int, input().split()))
#print(A)
count=0
count2=0
for i in range(len(A)-1):
    if A[i]>A[i+1]:
        count=A[i]-A[i+1]
        #print(count)
        A[i+1]=A[i]
    count2+=count
print(count2)