n = int(input())
a = list(map(int, input().split()))
import copy
b = copy.copy(a)
tall = a[0]
for i in range(n):
    if a[i]<tall:
        a[i] = tall
        cnt +=tall-a[i]
    elif a[i]>tall:
        tall = a[i]
print(sum(a)-sum(b)) 