import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n = int(input())
A = list(map(int, input().split()))
L = [(a, i) for i, a in enumerate(A)]
L.sort(reverse=True)
dp = dict()
def dfs(i, l, r):
    a, idx = L[i]
    if i == n-1:
        res = max(idx-l, r-idx) * a
        return res
    if (i, l, r) in dp:
        return dp[(i, l, r)]
    res = max((idx-l)*a + dfs(i+1, l+1, r), (r-idx)*a + dfs(i+1, l, r-1))
    dp[(i, l, r)] = res
    return res
ans = dfs(0, 0, n-1)
print(ans)