#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define mset(s,_) memset(s,_,sizeof s)
#define fi first
#define se second
#define pb push_back

template<typename T>
void chmax(T &a,T b){
  if(a<b)a=b;
}

ll dp[2222][3333];
vector<pair<ll,int> >p;
int n;

ll dfs(int pos,int l,int r){
	if(pos>=n) return 0;
	if(dp[l][r]!=-1) return dp[l][r];
	ll t1=dfs(pos+1,l+1,r)+p[pos].fi*abs(p[pos].se-l);
	ll t2=dfs(pos+1,l,r-1)+p[pos].fi*abs(r-p[pos].se);
	return dp[l][r]=max(t1,t2);
}
			
int main(){
 cin>>n;
 rep(i,0,n-1){
	int tp;
	cin>>tp;
	p.pb({tp,i});
 }
 sort(p.rbegin(),p.rend());
 mset(dp,-1);
 ll ans=dfs(0,0,n-1);
 cout<<ans<<'\n';
}