fn main() {
    proconio::input! {
        n: usize,
        a: [usize; n],
    }
    let mut dp = vec![vec![0;n+1];n+1];
    let mut b: Vec<_> = (0..n).collect();
    b.sort_by_key(|k| a[*k]);
    for (m,k) in b.into_iter().rev().enumerate() {
        let v = a[k];
        let f = |a,b| v*if a>b {a-b} else {b-a};
        for i in 0..m+2 {
            let j = n+i-m-1;
            let x = if i>0 {dp[i-1][j]+f(i-1,k)} else {0};
            let y = if j<n {dp[i][j+1]+f(j,k)} else {0};
            dp[i][j]=std::cmp::max(x,y);
        }
    }
    println!("{}",(0..=n).map(|i| dp[i][n-i]).max().unwrap());
}