n=int(input())
arr=list(map(int,input().split()))
ans=0
for i in range(1,n):
    m=0
    c=0
    for j in range(i,0,-1):
       
        if arr[j-1]>m:
            m=arr[j-1]
        
    if arr[i]<m:
        c+=m-arr[i]
    ans+=c
print(ans)