import numpy as np

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

s = 0

def step(a, s):
    am = np.argmax(a)
    s = s + np.max(a)*a[am+1:].shape[0] - np.sum(a[am+1:])
    
    if am == 0:
        return s
    return step(a[:am], s)

print(step(A, s))
