let input f = stdin.ReadLine() |> f
let inputs f = stdin.ReadLine().Split() |> Array.map f

let N = input int
let A = inputs int64
let ord = [|0..N-1|] |> Array.sortByDescending (fun i -> A.[i])

let inf = System.Int64.MinValue
let dp = Array2D.create (N + 1) (N + 1) inf
dp.[0, 0] <- 0L

for i in 0..N-1 do
    for j in 0..N-1 do
        if dp.[i, j] <> inf && i + j < N then
            let k = ord.[i + j]
            dp.[i + 1, j] <- max dp.[i + 1, j] (dp.[i, j] + A.[k] * int64 (k - i))
            dp.[i, j + 1] <- max dp.[i, j + 1] (dp.[i, j] + A.[k] * int64 ((N - 1 - j) - k))

[for i in 0..N -> dp.[i, N - i]]
|> List.max
|> printfn "%d"