#At Coder E: Active Infants

import random
N = int(input())
A = list(map(int,input().split()))
sA = sorted(A)
f = [n for n in range(N)]
ans = 0
new = [0 for n in range(N)]

for n in range(N):
    a = sA.pop()
    i = A.index(a)
    A[i] = 0
    if i - f[0] > f[-1] - i:
        p = f.pop(0)
        ans += (i-p) * a
    else:
        p = f.pop()
        ans += (p-i) * a
    new[p] = a
    
print(ans)
        