n = int(input())
l = list(map(int,input().split()))
#print(n,l)

ans=0
l2=[l[0]]
#print(l2)

for i in range(1,n):
    if l[i-1] <= l[i]:
        l2+=[l[i]]
        continue
    else:
        l2+=[l2[i-1]]
        ans+=(l2[i]-l[i])

print(ans)