n,*a=map(int,open(0).read().split())
count=0
for i in range(1,n):
    if i==1:
        if a[0]>a[1]:
            count+=a[0]-a[1]
    else:
        if max(a[:i])>a[i]:
            count+=max(a[:i])-a[i]
print(count)