#include <bits/stdc++.h>
#define MOD (long long)(1E9+7)
#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;


long long extgcd(long long a)
{
	long long b = MOD;
	long long u = 1, v = 0, q; 
	while ( b > 0 ) {
		q = a / b; 
		a -= q * b; swap(a,b);
		u -= q * v; swap(u,v);
	}
	u %= MOD;
	while (u<0)
		u+=MOD;
	return u;
}
long long comb(long long n,long long k)
{
	long long t = 1;
	long long j = k;
	for(long long i = n; i  >= n-k+1; i--) {
		t *= i;
		t %= MOD;
		t *= extgcd(j);
		t %= MOD;
		j--;
	}  
  	while (t<0)
		t += MOD;
	return t;
}

int main(){
	long long n,k;
	cin >> n;
	vector<long> a(n);
  	pair<long, int> ss(n);
  	
  	rep(i, n) {
    	cin >> a[i];
      	ss[i] = {a[i], i};
    }
  	sort(ss.begin(), ss.end());
  	reverse(ss.begin(), ss.end());
  	int fr=0, bk=0;
	k=0;
  	int t = 0;
  	rep(i, n) {
      	t = ss[i].second();
  		if(abs(t - fr) >= abs((n-bk)-t) ) {
        	fr++;
          	k += ss[i].first()*abs(t-fr);
        } else {
          	k += ss[i].first()*abs((n-bk)-t);
          	bk++;
        }
    }
    
	cout << k << endl;
}
