import numpy as np
n = int(input())
aa = list(map(int, input().split()))
ag = list(reversed(np.argsort(aa)))
st = list(reversed(sorted(aa)))
n_min = 0
n_max = n-1
score = 0
for k, v, i in zip(range(len(st)), st, ag):
    if abs(i - n_min) > abs(n_max - i):
        score += v*abs(i - n_min)
        n_min += 1
    elif abs(n_max - i) > abs(i - n_min):
        score += v*abs(n_max - i)
        n_max -= 1
    else:
        if n_min == n_max:
            score += v*abs(n_max - i)
        elif abs(ag[k+1] - n_min) > abs(n_max - ag[k+1]):
            score += v*(n_max - i)
            n_max -= 1
        elif abs(n_max - ag[k+1]) > abs(ag[k+1] - n_min):
            score += v*abs(i - n_min)
            n_min += 1
print(score)
