N = int(input())
A = list(input().split())
An = [int(num) for num in A]
print(An)

sum = 0

for i in range(1,N):
  if An[i-1]<=An[i]:
    continue
  else:
    sum += (An[i-1]-An[i])
    An[i] = An[i-1]
    
print(sum)