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


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]))
  
#print(list_calc_minus_2)

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