import sys
input = lambda: sys.stdin.readline().rstrip()

def solve(a):
    a_max = max(a)
    idx_max = a.index(a_max)
    return ((len(a) - 1 - idx_max) * a_max) - sum(a[idx_max + 1:]), idx_max

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    while(1):
        k, idx_max = solve(a)
        ans += k
        a = a[:idx_max]
        if idx_max == 0:
            break
    print(ans)

if __name__ == '__main__':
    main()
