N = int(input())
line = input()
A = [int(l) for l in line.split()]

total = 0

for i in range(N-1):
  diff = A[i-1] - A[i]
  if diff > 0 :
    total = total + diff
    A[i] = A[i] + diff

print(total)