#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int>> vvi;

int main() {
	int N;
	cin>>N;
	vector<pair<int,int>> a(N);
	for (int i = 0; i < N; i++){
		cin>>a[i].first;
		a[i].second=i;
	}
	sort(a.begin(),a.end(),[](auto &a, auto &b){return a.first>b.first;});
	vvi dp(N+1,vector<int>(N+1));
	for (int i = 0; i < N; i++){
		for (int l = 0; l <= i+1; l++){
			int r=i+1-l;
			if(l==0)dp[l][r]=dp[l][r-1]+a[i].first*abs(N-r-a[i].second);
			else if(l==i+1)dp[l][r]=dp[l-1][r]+a[i].first*abs(l-1-a[i].second);
			else dp[l][r]=max(dp[l-1][r]+a[i].first*abs(l-1-a[i].second),
				dp[l][r-1]+a[i].first*abs(N-r-a[i].second));
		}
	}
	int ans=0;
	for (int i = 0; i < N+1; i++){
		ans=max(ans,dp[i][N-i]);
	}
	cout<<ans<<endl;
}