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

int_sum = 0
list_calc_minus_2 = list()

#print([max(list_x[0:len(list_x)-i]) for i in range(len(list_x))])

for i in range(len(list_x)):
  #list_calc_minus_2.insert(0, max(list_x[0:len(list_x)-i]))
  int_sum = int_sum + max(list_x[0:len(list_x)-i]) - list_x[len(list_x)-i-1]
#print(list_calc_minus_2)

#print(sum([x - y for (x, y) in zip(list_calc_minus_2, list_x)]))
print(int_sum)
