loop = int(input())

tall = list(map(int, input().split()))
front = tall[0]
standard = front
result = 0


for i in range(1, loop):
  back = tall[i]
  
  if back-front > 0:
    standard = back
  elif back-front < 0:
    result += standard - back
  front = back

print(result)