n = int(input())
l = list(map(int, input().split()))

INF = 10 ** 16

dp = [-INF] * (n + 1)
dp[0] = 0

import heapq
q = []
for i in range(n):
    heapq.heappush(q, (-l[i], i))

for itera in range(n):
    _, ind = heapq.heappop(q)
    val = l[ind]
    dpnew = [-INF] * (n + 1)

    for i in range(n):
        left = dp[i] + abs(i - ind) * val
        right = dp[i] + abs(n - 1 - itera + i - ind) * val
        if dpnew[i] < right:
            dpnew[i] = right
        if dpnew[i+1] < left:
            dpnew[i+1] = left
    dp = dpnew
print(max(dp))
    
