import numpy as np

N = int(input())
an = list(map(int,input().split()))
solve = []

while len(an) > 0:
    k = np.argmax(an)
    l = max(an)
    j = 0
    for i in range(k,N):
        j = l - an[i]
        solve.append(j)
    del an[k:N]
    N -= len(solve)

solved = 0
for i in range(len(solve)):
    solved += solve[i]

print(solved)