import numpy as np
n=int(input())
a=list(map(int,input().split()))

c=[]
for i in range(n):
    if i==0 :
        c.append(a[0])
        
    elif i>0:
        if max(c)>a[i]:
            c.append(max(c))
            
        else :
            c.append(a[i])

na=np.array(a)
nc=np.array(c)
print(sum(nc-na))