import numpy as np

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

count = [0]
sum = 0


for i in A:
    if max(count) > i:
        sum += max(count) -i
        count.append(i)
    else:
        count.append(i)

print(sum)