#include <iostream>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <string.h>

using namespace std;

// #define for(i,a,b) for (int i=(a);i<(b);++i)
typedef long long ll;
typedef pair<ll,ll> P;
#define REP(i,n) for(long long i = 0; i < (long long)(n); i++)
#define pb push_back // vectorに要素追加
#define INF (ll)1e18

ll mod = 1e9+7;
const ll MAX_N = 3000;
/**/
int main() {
	// 入力
	ll N;cin>>N;
	P A[N];
    REP(i,N){
		ll a_tmp;cin>>a_tmp;
		A[i] = make_pair(a_tmp, i); // (活発度, 座標)
	}
    
	// 解法
	ll ans = 0;
	if (N%2==0){
		P A1[N/2];
		REP(i,N/2)A1[i] = A[i];
		P A2[N/2];
		REP(i,N/2)A2[i] = A[i+N/2];
		sort(A1, A1 + N/2, greater<P>());
		sort(A2, A2 + N/2, greater<P>());
		// REP(i,N/2)printf("A1, i: %lld, (%lld, %lld)\n", i, A1[i].first, A1[i].second);
		// REP(i,N/2)printf("A2, i: %lld, (%lld, %lld)\n", i, A2[i].first, A2[i].second);
		REP(i,N/2){
			ll move = A2[i].second - i;
			ll tmp = A2[i].first * move;
			// printf("i: %lld, move: %lld, tmp: %lld\n", i, move, tmp);
			ans += tmp;
		}
		REP(i,N/2){
			ll move = (N-1-i) - A1[i].second;
			ll tmp = A1[i].first * move;
			// printf("i: %lld, move: %lld, tmp: %lld\n", i+N/2, move, tmp);
			ans += tmp;
		}
	}
	else{
		P A1[N/2+1];
		P A2[N/2+1];
		REP(i,N/2+1)A1[i] = A[i];
		REP(i,N/2)A2[i] = A[i+N/2+1];
		sort(A1, A1 + N/2+1, greater<P>());
		sort(A2, A2 + N/2, greater<P>());
		// REP(i,N/2+1)printf("A1, i: %lld, (%lld, %lld)\n", i, A1[i].first, A1[i].second);
		// REP(i,N/2)printf("A2, i: %lld, (%lld, %lld)\n", i, A2[i].first, A2[i].second);
		REP(i,N/2){
			ll move = A2[i].second - i;
			ll tmp = A2[i].first * move;
			// printf("i: %lld, move: %lld, tmp: %lld\n", i, move, tmp);
			ans += tmp;
		}
		REP(i,N/2+1){
			ll move = (N-1-i) - A1[i].second;
			ll tmp = A1[i].first * move;
			// printf("i: %lld, move: %lld, tmp: %lld\n", i+N/2, move, tmp);
			ans += tmp;
		}
		ll ans2 = 0;
		REP(i,N/2)A1[i] = A[i];
		REP(i,N/2+1)A2[i] = A[i+N/2];
		sort(A1, A1 + N/2, greater<P>());
		sort(A2, A2 + N/2+1, greater<P>());
		// REP(i,N/2)printf("A1, i: %lld, (%lld, %lld)\n", i, A1[i].first, A1[i].second);
		// REP(i,N/2+1)printf("A2, i: %lld, (%lld, %lld)\n", i, A2[i].first, A2[i].second);
		REP(i,N/2+1){
			ll move = A2[i].second - i;
			ll tmp = A2[i].first * move;
			// printf("i: %lld, move: %lld, tmp: %lld\n", i, move, tmp);
			ans2 += tmp;
		}
		REP(i,N/2){
			ll move = (N-1-i) - A1[i].second;
			ll tmp = A1[i].first * move;
			// printf("i: %lld, move: %lld, tmp: %lld\n", i+N/2, move, tmp);
			ans2 += tmp;
		}
		if (ans2>ans) ans = ans2;

	}

	//出力
	printf("%lld\n", ans);
}
