N = int(input())
an = list(map(int,input().split()))
solve = []

ans = 0
maxnum = 0

for i in range(1,N):
    print(str(maxnum)+'is maxnum' +str(i))
    if maxnum < an[i]:
        ans += max(an[maxnum:i+1]) - an[i]
        maxnum = an[i]
    if maxnum >= an[i]:
        ans += maxnum - an[i]
    print(str(ans) + 'is ans'+str(i))
print(ans)