n = int(input())
a = list(map(int, input().split()))
ans = 0
maxi = 0

for i in range(n-1):
    if a[i] > a[i + 1]:
        if a[i] > maxi:
            maxi = a[i]
        ans += maxi - a[i+1]

print(ans)
