n=int(input())
a=list(map(int,input().split()))
s=max(a)
k=0
for i in range(0,(a.index(s))):
    
    if a[i]>a[i+1]:
        k+= (a[i] - a[i+1])
    else:
        continue
for i in range(a.index(s),n):
    if a[i]<s:
        k+= s-a[i]
    else:
        continue
print(k)        