import numpy as np
import heapq
n = int(input())
a = np.array(list(map(int, input().split())))

selected = [False]*n
left = 0
right = n-1
ans = 0
d = np.array([max(abs(i - left), abs(right - i)) for i in range(n)])
for _ in range(n):
    t = np.array([v if not selected[i] else 0 for i, v in enumerate(a)])
    r = t * d
    index = np.where(r == np.max(r))[0][0]
    selected[index] = True
    ans += r[index]
    if abs(index - left) == abs(index - right):
        if np.sum(t[:index]) > np.sum(t[index+1:]):
            right -= 1
        else:
            left += 1
    if d[index] == abs(index - left):
        left += 1
    else:
        right -= 1

print(ans)




