import numpy as np
from numba import jit
N1 = int(input(''))
A1 = np.array(list(map(int, input('').split(' '))))

@jit(nopython=True, cache=True)
def func(N,A):
    step = 0
    for k in range(1,N):
        if (np.max(A[:k]) > A[k]):
            step += (N-k) * (np.max(A[:k]) - A[k])
            A[k:] = np.max(A[:k])
    return step
        
print(func(N1,A1))