use proconio::input;
use std::cmp::*;

fn main() {
    input! {
        n: usize,
        a: [i64;n]
    }

    let mut ai: Vec<(i64, i64)> = a.iter().enumerate().map(|(i, &a)| (a, i as i64)).collect();
    //    let mut ai: Vec<(i64, i64)> = a.iter().enumerate().map(|(i, &a)| (a, i as i64)).collect();
    ai.sort();
    ai.reverse();
    // 降順に並んだ。
    // println!("{:?}", ai);
    //for (a, i) in &ai {
    //    println!("{},{}", a, i);
    //}
    let minimum = 0;
    let mut dp = vec![vec![minimum; n + 1]; n + 1];
    dp[0][0] = 0;

    for (i, &(a, j)) in ai.iter().enumerate() {
        for l in 0..i + 1 {
            let r = i - l;
            dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + a * (j - (l as i64)));
            dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + a * ((n - 1 - r) as i64 - j));
        }
    }
    let mut ans = 0;
    for l in 1..n - 1 {
        let r = n - l;
        ans = max(dp[l][r], ans);
    }
    println!("{}", ans);
    //    println!(        "{}",        dp.iter().map(|d| d.iter().max().unwrap()).max().unwrap());
}
