N = int(input())
A = list(map(int,input().split()))

count = [0]
sum = 0

for i in A:
    if max(count) > i:
        sum += max(count) -i
        count.append(i)
    else:
        count.append(i)

print(sum)