#include<bits/stdc++.h>
#define ll long long 
#define mp make_pair 
#define pb push_back 
#define F first 
#define S second 
#define f(i,n) for(int i=0;i<n;i++) 
#define fr(i,a,b) for(int i=a;i<b;i++) 

using namespace std; 

void test(){
	ll n;
	cin>>n;
	ll a[n];
	f(i,n)cin>>a[i];
	bool vis[n]={false};
	ll sum = 0;
	
	ll ma = n-1,mi = 0;
	for(int i=0;i<n;i++){
		ll bestin,bestval=0;
		
		for(int j=0;j<n;j++){
			if(!vis[j]){
				ll f1 = abs(j-mi)*a[j];
				ll f2 = abs(j-ma)*a[j];
				ll f3 = max(f1,f2);
				
				if(f3>=bestval){
					bestval = f3;
					bestin = j;
				}
			}
		}
		
		vis[bestin] = true;
		sum+=bestval;
		if(abs(bestin-mi)*a[bestin]==bestval){
			mi++;
		}else
			ma--;
		
	}
	cout<<sum<<"\n";
}

int main(){
	int t=1;
//	cin>>t;
	while(t--){
		test();
	}
}