use proconio::{input, fastout};

#[fastout]
fn main() {
    input! {
        n: usize,
        ax: [i32; n]
    }

    let mut dp: Vec<i32> = vec![0; n+1];

    let mut axi: Vec<_> = ax.iter().enumerate().collect();
    axi.sort_by_key(|a| a.1);

    for i in 0..n {
        let (pos, &ai) = axi[i];
        let pos = pos as i32;
        for l in 0..n-i {
            let r = (l + i) as i32;
            let u = dp[l] + ai * (r - pos).abs();
            let v = dp[l+1] + ai * (l as i32 - pos).abs();
            dp[l] = std::cmp::max(u, v);
        }
    }

    println!("{}", dp[0]);
}