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

ans = 0
maxnum = 0

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