def rec(i,j):
    if i + j == N:
        return
    tmp = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - i)
    if tmp > dp[i+1][j]:
        dp[i+1][j] = tmp
    tmp_ = dp[i][j] + A[i+j][0] * abs(A[i+j][1] - N + j + 1)
    if tmp_ > dp[i][j+1]:
        dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
    if tmp > dp[i+1][j]:
        rec(i + 1, j)
    if tmp_ > dp[i][j+1]:
        rec(i, j + 1)
    
N = int(input())
A = sorted([(int(j),i) for i, j in enumerate(input().split())])[::-1]

dp = [[0 for _ in range(N)] for _ in range(N)]
rec(0, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)