from functools import cmp_to_key

N = int(input())
A = list(map(int, input().split()))

ans = [(i, a) for i, a in enumerate(A)]

for _ in range(N + 10):
    for i in range(N - 1):
        j = i + 1
        s, a = ans[i]
        t, b = ans[j]

        new = a * abs(s - j) + b * abs(t - i)
        old = a * abs(s - i) + b * abs(t - j)

        if new > old:
            ans[i], ans[j] = ans[j], ans[i]

res = 0
for i, (s, a) in enumerate(ans):
    res += a * abs(i - s)
print(res)
