from sys import exit
n = int(input())
if n==1: print(0); exit()
a = list(map(int, input().split()))
al = [0 for _ in range(n)]
for index in range(n-1):
	person = a[index]
	person_behind = a[index+1]
	if person_behind < person:
		k = person - person_behind
		a[index+1] += k
		al[person_behind] = k
print(sum(al))