import numpy as np
import heapq
n = int(input())
a = np.array(list(map(int, input().split())))
check = [-1] * n
ans = 0
for u in range(n//2):
    t = np.array([abs(i-u) for i in range(n)])
    r = a * t
    index = np.where(r == np.max(r))[0][-1]
    ans += r[index]
    a[index] = 0

for u in reversed(range(n//2, n)):
    t = np.array([abs(i - u) for i in range(n)])
    r = a * t
    index = np.where(r == np.max(r))[0][-1]
    ans += r[index]
    a[index] = 0

print(ans)


