from random import shuffle
N = int(input())
A = list(map(int, input().split()))
P = list(range(N))
R = [i%N for i in range(N**2)]
shuffle(R)
res = 0
for _ in range(N**2//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)


