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

B = sorted(A, reverse=True)
C = [False for _ in range(len(A))]
ans = 0

for i in range(len(B)):
    x = A.index(B[i])
    A[x]=0
    p = x
    tmp = 0
    for j in range(len(C)):
        if abs(x-j) > tmp and C[j] == False:
            tmp = abs(x-j)
            p=j
    C[p] = True
    ans += B[i]* abs(x-p)

print(ans)