//by Top1
#include <bits/stdc++.h>
          
#define f first
#define s second
#define int long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define pll pair <long, long>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define cerr cout
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return 
#define x1 x21212121
#define y1 y21212121
#define sqr(x) x * x 
      
const int inf = (1e9) + 1;
const int mod = 479001599;
const int maxn = 2e3 + 11;
        
using namespace std;          

int n, dp[maxn][maxn];
pii a[maxn];

void rec(int pos, int l, int r){
	if (pos == n + 1)
		return;
	dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + a[pos].f * abs(a[pos].s - l - 1));
	rec(pos + 1, l + 1, r);
	dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + a[pos].f * abs(a[pos].s - r + 1));
	rec(pos + 1, l, r - 1);
}

void solve(){   
	cin >> n;

	for (int i = 1; i <= n; i++){
		cin >> a[i].f;
		a[i].s = i;
	}

	sort(a + 1, a + n + 1);
	rev(a + 1, a + n + 1);

	rec(1, 0, n + 1);

	int mx = 0;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			mx = max(mx, dp[i][j]);

	cout << mx << endl;
}                              
      
main(){
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);

    boost;
    
    int q = 1;
 
    //cin >> q;
    
    while (q--){
        solve();
    }
}