n = int(input())
l = list(map(int,input().split()))
l =l[::-1]
sum=0
for j in range(len(l)):
    for i in range(len(l)-1):
        if l[i+1]>l[i]:
    ##        print("l[i]= ",l[i])
            sum+=l[i+1]-l[i]
            l[i]+=l[i+1]-l[i]

print(sum)
