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

for i in range(n-1):
    for j in range(i+1,n):
        if list[j] < list[i]:
            count += (list[i] - list[j])
            list[j] = list[i]

print(count)