
item = input().split()
total_num = item[0]
fumidai_sum = 0
before_tall = 0

for idx, tall in enumerate(item[1:]):
  tall = int(tall)
  
  if idx == 0:
    before_tall = tall
    continue
  else:
    if before_tall > tall:
      fumidai_sum += (before_tall - tall)
    else:
      before_tall = tall
      
print(fumidai_sum)