import numpy as np
N = int(input(''))
A = np.array(list(map(int, input('').split(' '))))
step = np.zeros(N, dtype=int)
for k in range(1,N):
    if (np.max(A[:k]) > A[k]):
        step[k:] += np.max(A[:k]) - A[k]
        A[k:] = np.max(A[:k])
    else:
        continue
print(np.sum(step))