n = int(input())
a = list(map(int, input().split()))
a_2 = []
for i in range(n):
    a_2.append([i, a[i]])
a_2.sort(key=lambda x: x[1], reverse= True)
x = 0
y = n-1
ans = 0
for i in range(n):
    abs_x = abs(x-a_2[i][0])
    abs_y = abs(y-a_2[i][0])
    if i == n-1:
        ans += a_2[i][1] * abs_x
        continue
    if abs_x > abs_y:
        x += 1
        ans += a_2[i][1] * abs_x
    elif abs_x < abs_y:
        y -= 1
        ans += a_2[i][1] * abs_y
    else:
        if abs(x-a_2[i+1][0]) > abs(y-a_2[i+1][0]):
            y -= 1
            ans += a_2[i][1] * abs_y
        else:
            x += 1
            ans += a_2[i][1] * abs_x
print(ans)
