def stepUp(val_list):
  counter = 0

  for i in range(len(val_list) - 1):
      current = val_list[i]
      up = val_list[i + 1]

      if (current > up):
          counter += current - up

  if (val_list[len(val_list) - 2] > val_list[len(val_list) -1]):
      counter += val_list[len(val_list) - 2] - val_list[len(val_list) -1]

  return counter

def userInput():
    a_count = int(input())
    a = list(map(int, input().rstrip().split()))
    
    return stepUp(a)
  
print(userInput())