import numpy as np

_ = 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 = li[:maxLocation]
    
    ve = np.array(list([maxValue]*len(li_removed))) - np.array(li_removed)
    tmpVe = list([1]*len(li_removed))
    ans +=int(np.matrix(tmpVe)@(np.matrix(ve).T))

print(ans)