from random import shuffle
N = int(input())
A = list(map(int, input().split()))
P = list(range(N))

res = 0
for _ in range(2*N):
    R = list(range(N))
    shuffle(R)
    for _ in range(N//2):
        i = R.pop()
        j = R.pop()
        x, y = P[j], P[i]
        tmp = A[j]*abs(j-y) + A[i]*abs(i-x) - (A[j]*abs(j-x) + A[i]*abs(i-y))
        if tmp > 0:
            P[j], P[i] = y, x
            res += tmp

print(res)


