n=int(input())
a=list(map(int,input().split()))
s=0

while len(a)>0:
    if sorted(a) == a:
        print(s)
        exit()
    else:
        x=max(a)
        k=a.index(x)
        for i in range(k+1,len(a)):s+=x-a[i]
        a=a[:k]
print(s)



