N = int(input())
A = list(map(int, input().split())) 

current_index = 0
total = 0
for index in range(N):
  value = A[current_index]
  match_list = [i for i in A[current_index:] if i > value]
  if len(match_list) > 0:
    next_index = A.index(match_list[0])
    diff_list = list(map(lambda x: value - x, A[current_index:next_index]))
    total += sum(diff_list)
    current_index = next_index
  else:
    diff_list = list(map(lambda x: value - x, A[current_index:]))
    total += sum(diff_list)
    break
print(total)