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

for i in range(n):
  if i ==n-1:
    break
  else:
  	if a[i] <= a[i + 1]:
    	continue
  	else:
    	t += a[i] - a[i + 1]
    	a[i] = a[i + 1]
    
print(t)
