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

sort_a = sorted(A, reverse = True)
L  = 0
R = N-1
ans = 0
for i in range(N):
    n = sort_a[i]
    inde = A.index(n)
    A[inde] = -1
    if inde - L > R - inde:
        ans +=(inde - L) * n
        L +=  1
    else:
        ans += (R - inde) * n
        R -= 1
print(ans)