int_n = int(input())
list_x = list(map(int, input().split()))

#print(max(list_x))

#同じ値には、最小のインデックスを返す
#print(list_x.index(max(list_x)))

#int_plus = 0
list_x_left = list_x
list_calc_minus = list()

while len(list_x_left) != 0:
  #list_x_prosess = list_x_left[list_x_left.index(max(list_x_left)):]
  
  int_list_x_prosess_len = len(list_x_left) - list_x_left.index(max(list_x_left))
  int_max_list_x_prosess = max(list_x_left)
  list_x_left = list_x_left[:list_x_left.index(max(list_x_left))]
  
  [list_calc_minus.insert(0, int_max_list_x_prosess) for i in range(int_list_x_prosess_len)]
  
  ##  print(sum([max(list_x_prosess)-i for i in list_x_prosess]))
  #int_plus = int_plus + sum([max(list_x_prosess)-i for i in list_x_prosess])
#
  ##  print(list_x_left, list_x_prosess)
  
#print(int_plus)
#print([x - y for (x, y) in zip(list_calc_minus, list_x)])
print(sum([x - y for (x, y) in zip(list_calc_minus, list_x)]))

