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

while len(list_x_left) != 0:
  list_x_prosess = list_x_left[list_x_left.index(max(list_x_left)):]
  list_x_left = list_x_left[:list_x_left.index(max(list_x_left))]
  
  #  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)