N=int(input())
A = list(map(int, input().split()))
#print(A)
count=0
count2=0

if len(A)==1:
    print(0)
    exit()

for i in range(len(A)-1):
    #print(A[i])
    if A[i]>A[i+1]:
        count=A[i]-A[i+1]
        print(count)
        A[i+1]=A[i]
        count2+=count

count2=A[-2]-A[-1]
print(count2)