N = int(input())
A = list(map(int, input().split()))

A = list(enumerate(A, start=1))
A.sort(reverse=True, key=lambda x: x[1])

dp = [[0]*(N + 1) for _ in range(N + 1)]
for j in range(N):
    dp[0][j+1] = dp[0][j] + A[j][1]*((N - j) - A[j][0])
for i in range(N):
    dp[i+1][0] = dp[i][0] + A[i][1]*(A[i][0] - (i + 1))
    for j in range(N - i - 1):
        dp[i+1][j+1] = max(dp[i+1][j] + A[i+j+1][1]*((N - j) - A[i+j+1][0]),
                           dp[i][j+1] + A[i+j+1][1]*(A[i+j+1][0] - (i + 1)))
print(max(dp[i][N-i] for i in range(N+1)))
