_ = input()
li = list(map(int,input().split()))
ans=0
 
while li!=[] and li!=sorted(li):
    maxValue = max(li)
    maxLocation = li.index(maxValue)
    li_removed = li[maxLocation:]
    li_removed.sort()
    li = li[:maxLocation]
    maxLocation2 = li_removed.index(maxValue)
    li_calculate = li_removed[:maxLocation2]
    
    for i in li_calculate:
        ans = ans + (maxValue - i)
        
print(ans)