#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using ull = long long unsigned;
using P = pair<ll,ll>;
//const int 1001001001;
const int NMAX=8;
const ll MOD=1000000007;
const int dbase=80*160;



using namespace std;
using ll = long long;

int main(){
	ll n;
	cin>>n;
	vector<P> a(n+5);
	rep(i,n){
		ll temp;
		cin>>temp;
		a[i]=make_pair(temp,i);
	}
	sort(a.rbegin(),a.rend());
  ll ans=0;
	vector<vector<int>> d(n+5,vector<int>(n+5,0));

	for(ll i=0;i<n;i++){
		for (ll j=0;j<n;j++){
		if ((j+i)>=n) {
			ans=max(d[i][j],ans);
			break;
		}

		d[i+1][j]=max(d[i+1][j],d[i][j]+(a[i+j].second-i)*(a[i+j].first));
		d[i][j+1]=max(d[i][j+1],d[i][j]+(n-1-j-a[i+j].second)*(a[i+j].first));
	//	cout<<d[i][j]<<' '<<i<<'i'<<j<<'j'<<endl;

	  }
	}
	cout<<ans;

}
