#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
/******************************************
* AUTHOR : PANDRAVISAM SHIVA SANTHOSH *
* Handle : santhosh_80 *
* INSTITUTION : IIT Palakkad *
******************************************/
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
#define ll long long int 
#define mod 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define pii pair<int,int>
#define all(v) (v).begin(),(v).end()
#define S(x) scanf("%d",&(x))
#define P(x) printf("%d\n",(x))
#define Sl(x) scanf("%lld",&(x))
#define Pl(x) printf("%lld\n",(x))
#define pb push_back
#define f first
#define s second

bool cmp(pair<ll,ll> x, pair<ll,ll> x1){
	return x.f > x1.f ; 
}



int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n ;cin >> n ;
	vector<pair<ll,ll>> v(n+1) ;
	set<int> s1;
	for(int i = 1; i <= n ; i++){
		s1.insert(i) ;
	}
	ll max_ans = 0;
	for(int i = 1; i <= n ; i++){
		ll x ; cin >> x ;
		v[i] = {x,i} ;
	}
	sort(v.begin()+1,v.end(),cmp);
	int flag = 0;
	for(int i = n ; i >= 2 ; i--){
		if(v[i].f == v[i-1].f)flag=1;
	}

	for(int i = 1; i <= n ; i++){
		pair<ll,ll> x = v[i] ;
		//cout << x.f  << " " << x.s << "\n" ;
		auto it = s1.begin() ;
		auto it1 = s1.end();
		it1--; 
		if(abs(*it-x.s) >= abs(*it1-x.s)){
			max_ans = max_ans + abs(*it-x.s)*x.f ;
			s1.erase(it);
 		}
 		else{
 			max_ans = max_ans + abs(*it1-x.s)*x.f ; 	
 			s1.erase(it1);
 		}
	}
	cout << max_ans+flag ;
 	return 0;
}