def rec(i,j,k):
    if k == N or i == N - 1 or j == N - 1:
        return
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - i)
    dp[i+1][j] = tmp if tmp > dp[i+1][j] else dp[i+1][j]
    tmp = dp[i][j] + A[k][0] * abs(A[k][1] - N + j + 1)
    dp[i][j+1] = tmp if tmp > dp[i][j+1] else dp[i][j+1]
    rec(i + 1, j, k + 1)
    rec(i, j + 1, k + 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, 0)
ans = -1
for i in range(4):
    for j in range(4):
        if dp[i][j] > ans:
            ans = dp[i][j]
print(ans)