use proconio::*;

fn run() {
    input! {
        n: usize,
        a: [i64; n],
    }
    let mut ans = 0;
    for i in 0..n {
        let mut f = vec![];
        for j in 0..i {
            f.push((a[j], j));
        }
        let mut t = vec![];
        for j in i..n {
            t.push((a[j], j));
        }
        f.sort();
        t.sort();
        let diff = |a: usize, b: usize| (std::cmp::max(a, b) - std::cmp::min(a, b)) as i64;
        let mut sum = 0;
        for j in ((n - i)..n).rev() {
            let (v, x) = f.pop().unwrap();
            sum += v * diff(x, j);
        }
        for j in 0..(n - i) {
            let (v, x) = t.pop().unwrap();
            sum += v * diff(x, j);
        }
        ans = std::cmp::max(ans, sum);
    }
    println!("{}", ans);
}

fn main() {
    run();
}
