use proconio::input;
use std::cmp::max;

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

    let mut v: Vec<(usize, usize)> = Vec::new();
    for (i, j) in a.iter().enumerate() {
        v.push((j.clone(), i));
    }
    v.sort_by(|x, y| (&y.0).cmp(&x.0));
    
    let mut d = vec![vec![0; n + 1]; n + 1];
    let f = |i: i64, j: i64| -> usize {
        ((i - j).abs()) as usize
    };
    for (i, &(act, pos)) in v.iter().enumerate() {
        println!("{} {}", act, pos);

        for x in 0..i + 1 {
            let y = i - x;
            let m_x = f(pos as i64, x as i64);
            let m_y = f((n - y - 1) as i64, pos as i64);
            d[x + 1][y] = max(d[x + 1][y], d[x][y] + m_x * act);
            d[x][y + 1] = max(d[x][y + 1], d[x][y] + m_y * act);
        }
    }
    let ans = d.iter()
        .flat_map(|v| v).max().unwrap();
    println!("{}", ans);
}
