
N=int(input())
a=list(map(int,input().split()))
b = [(val,i) for i,val in enumerate(a)]
c = sorted(b, reverse=True)
res = [(0,0)]*N
left = 0
right = 0
ans = 0
now1=(-1,-1)
now2=(-1,-1)
for j,(val,i) in enumerate(c):
    if j%2==0:
        now1 = (val,i)
        if j==N-1:
            res[N//2] = now1
            # 最後
    else:
        now2 = (val,i)
        # swap?
        if now1[1] > now2[1]:
            res[j//2] = now1
            res[N-1-j//2] = now2
        else:
            res[j // 2] = now2
            res[N - 1 - j // 2] = now1
            
for j,(val,i) in enumerate(res):
    ans += val*abs(j-b[i][1])
print(ans)


