n = int(input())
lst = list(map(int, input().split()))
count = 0
t = 0
for i in range(n-1):
    if lst[i] < lst[i+1]:
        t += lst[i+1] - lst[i]
        count += t
        lst[i+1] = t
print(count)
