n = int(input())
a = [int(s) for s in input().split()]
max_index = a.index(max(a))
count = 0
last_index = len(a)
while(last_index > 0):
    for i in range(max_index,last_index):
        count += a[max_index] - a[i]
    last_index = max_index
    if last_index == 0:
        break
    max_index = a.index(max(a[:last_index]))

print(count)