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
for _ in range(n):
    d = np.array([max(i-left, right - i) if not selected[i] else 0 for i in range(n)])
    r = a * d
    index = np.where(r == np.max(r))[0][0]
    selected[index] = True
    ans += r[index]
    if d[index] == index - left:
        left += 1
    else:
        right -= 1

print(ans)

