n = int(input())
a = list(map(int,input().split()))
x = 0
total = 0


for i in range(n):
    if a[i-1] >= a[i]:
        x = a[i-1] - a[i]
        total += x
        i = i+1
    else:
        total += 0
        i = i+1
print(total)