#pragma GCC optimize("Ofast",3,"inline")
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;

int a[3000];
ll dp[3000][3000];

int main(){
	int n;cin>>n;
	vector<P>v;
	rep(i,n){
		scanf("%d",&a[i]);
		v.push_back(P(a[i],i));
	}
	sort(v.begin(),v.end(),greater<>());
	memset(dp,-1,sizeof(dp));
	dp[0][0]=0;
	rep(i,n)rep(j,i+1){
		if(dp[i][j]==-1)continue;
		dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+v[i].first*(ll)abs(v[i].second-j));
		dp[i+1][j]=max(dp[i+1][j],dp[i][j]+v[i].first*(ll)abs(v[i].second-(n-(i-j)-1)));
	}
	cout<<max(dp[n],dp[n]+n+1)<<endl;
}