func readInts() -> [Int64]{
    return readLine()!.split(separator: " ").map{Int64($0)!};
}
var N = Int(readLine()!)!, ans = Int64(0), A: Array<(Int64, Int)> = Array(repeating: (0,0), count: 2020), dp: Array<Array<Int64>> = Array(repeating: Array(repeating: 0, count: 2020), count: 2020)
let a = readInts()
for i in 0 ..< N {
    A[i].0 = a[i]
    A[i].1 = i
}
A.sort { $0 > $1 }
for j in 1 ... N {
    dp[0][j] = dp[0][j - 1] + A[j - 1].0 * Int64(N - j - A[j - 1].1)
}
for i in 1 ... N {
    dp[i][0] = dp[i - 1][0] + A[i - 1].0 * Int64(A[i - 1].1 - i + 1)
    for j in 1 ..< N - i + 1 {
        dp[i][j] = max(dp[i][j - 1] + A[i + j - 1].0 * Int64(N - j - A[i + j - 1].1), dp[i - 1][j] + A[i + j - 1].0 * Int64(A[i + j - 1].1 - i + 1 ))
    }
    ans = max(ans, dp[i][N - i])
}
print(ans)