# -*- coding: utf-8 -*-
# E - Active Infants
# モジュールのインポート
import math

# 標準入力の取得
N = int(input())
A = list(map(int, input().split()))

# 求解処理
A1,A2 = {},{}
divisor = 0
sum_A = sum(A)
for i,a in enumerate(A):
    if divisor >= math.ceil(sum_A/2):
        A2[i] = a
    else:
        A1[i] = a
    divisor += a

result = 0
sorted_A1 = sorted(A1.items(), key=lambda x:x[1], reverse=True)
y1 = N
for x1,a1 in sorted_A1:
    result += a1 * abs(x1+1-y1)
    y1 -= 1

sorted_A2 = sorted(A2.items(), key=lambda x:x[1], reverse=True)
y2 = 1
for x2,a2 in sorted_A2:
    result += a2 * abs(x2+1-y2)
    y2 += 1

print(result)