tot = int(input())
ls = list(map(int,input().split()))
ans = 0
mx = 0
for i in range(0,tot):
    if mx < ls[i]:
        mx = ls[i]
    if i != 0:
        if ls[i] < ls[i-1]:
            ans += mx - ls[i]
print(ans)