n = int(input())
a_list = list(map(int, input().split()))

step_ttl = 0
step = 0
for i, a in enumerate(a_list):
  if i == 0:
    continue
  if a_list[i-1] < a:
    step = 0
    continue
  if a < step + a_list[i-1]:
    step = (step + a_list[i-1]) - a
  step_ttl += step
  

print(step_ttl)