# coding: utf-8
N = int(input())
_A = list(map(int, input().split()))
A = []
for i,a in enumerate(_A, 1):
    A.append((i, a))
perm = list(range(1,N+1))
A = sorted(A, key=lambda x:x[1], reverse=True)

happiness = 0
for k, Ak in A:
    if abs(perm[0]-k) > abs(perm[-1]-k):
        d, v = perm[0],abs(perm[0]-k)
    d, v = max([(0, abs(perm[0]-k)), (-1, abs(perm[-1]-k))], key=lambda x:x[1])
    happiness += v * Ak
    del perm[d]

print(happiness)