N = int(input())
a = list(map(int, input().split()))
count = 0
i = 1
for i in range(N-1):
  if a[i-1] <= a[i]:
    continue
  elif a[i-1] > a[i]:
    diff = a[i-1] - a[i]
    count = count + diff
    a[i] = a[i-1]
print(count)    
