#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
using namespace std;
int main() {
		ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    vector<pair<ll,ll>> v(n+1);
    for(int i=1;i<=n;i++){
			cin>>v[i].first;
			v[i].second = i;
		}
		sort(v.begin(),v.end());
    ll dp[n+2][n+1],k=1;
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
				for(int j=1;j+i-1<=n;j++){
						ll L = j;
						ll R = j+i-1;
						dp[L][R] = max(v[k].first*abs(v[k].second-L)+dp[L+1][R] ,  v[k].first*abs(v[k].second-R)+dp[L][R-1]);
				}
				k++;
		}
		cout<<dp[1][n]<<"\n";
}
 

