from typing import Counter


n = int(input())
num_list = [int(i) for i in input().split()]
count = 0

while num_list:
    max_num_index = num_list.index(max(num_list))
    for i in range(max_num_index,len(num_list)):
        count += (num_list[max_num_index]-num_list[i])
        num_list[i] = num_list[max_num_index]
    num_list.remove(num_list[max_num_index])

print(count)