use proconio::{input, fastout};
use std::cmp::max;

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

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

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

    let mut ans = 0;
    for i in 1..=n {
        for j in 0..=i {
            let (index, ai) = axi[i-1];
            if j > 0 {
                dp[i][j] = 
                    max(
                        dp[i][j],
                        dp[i-1][j-1] + ai * (index as i32 - (j-1) as i32).abs()
                    );
            }
            let k = i - j;
            if k > 0 {
                dp[i][j] = 
                    max(
                        dp[i][j],
                        dp[i-1][j] + ai * (index as i32 - (n-k) as i32).abs()
                    );
            }
            ans = max(ans, dp[i][j]);
        }
    }

    println!("{}", ans);
}