import sys
sys.setrecursionlimit(400000)

def find_ans(s,lo,hi):
    if hi-lo<=1 :
        return(0)
    j=0
    Max=-10
    for i in range(lo,hi):
        if s[i]>=Max :
            j=i
            Max=s[i]
    
    ans=0
    for i in range(j+1,hi):
        ans+=(Max-s[i])
    
    ans+=find_ans(s,lo,j) 
    return(ans)

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