#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define all(x) (x).begin() , (x).end()
#define f first
#define s second
#define pr(x) cout<<x<<endl;
#define pr2(x,y) cout<<x<<" "<<y<<endl;
#define pr3(x,y,z) cout<<x<<" "<<y<<endl;
#define prv(v) for(auto x:v) cout<<x<<" ";
#define ffs fflush(stdout);
using namespace std;
 
const ll N =(2e5+5);
const ll MOD = 1e9+7;
const ll INF = LLONG_MAX;
const ll LOG = 29;
#define PI 3.141592653589793238 
  
double circumference(double r) 
{ 
    double cir = 2.0;
    cir*=PI;
    cir*=r; 
    return cir; 
} 
long long binpow(long long a, long long b) {
    a%=MOD;    
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a)%MOD;
        a = (a * a)%MOD;
 
        
        b >>= 1;
    }
    res%=MOD;
    return res;
} 
ll fact[N];
void pre(){
    fact[0]=1;
    for(ll i =1;i<N;i++){
        fact[i] = (fact[i-1] * i)%MOD; 
    }
}
ll ncr(ll n,ll r){
    ll g = fact[n];
    g%=MOD;
    if(g<0)g+=MOD;
    ll gg = binpow(fact[r],MOD-2)%MOD;
    if(gg<0)gg%=MOD;
    ll gg2 = binpow(fact[n-r],MOD-2)%MOD;
    if(gg2<0)gg2%=MOD;
    g = (g*gg)%MOD;
    if(g<0)g+=MOD;
    g = (g*gg2)%MOD;
    if(g<0)g+=MOD;
    return g;   
}
void solve(){
    ll n;
    cin>>n;
    ll a[n+1];
    for(int i =1;i<=n;i++){
        cin>>a[i];
    }
    ll ans[n+1]={-1};
    priority_queue<pll> pq;
    for(int i= 1;i<=n;i++){
        pq.push(mp(a[i],i));
        ans[i]=-1;
    }
    ll l = 1,r=n;
    ll cnt =1;
    vector<bool> done(n+1,false);
    while(cnt<=n){
        ll ma = -1;
        ll idx = -1;
        ll keep =-1;
        char cc;
        for(int i =1;i<=n;i++){
            if(done[i] == false){
                if(abs(i-r) > abs(i-l)){
                    if(abs(i-r)*a[i] > ma){
                        ma = a[i]*abs(i-r);
                        idx=i;
                        keep=r;
                        cc='r';
                    }
                }
                else{
                    if(abs(i-l)*a[i] > ma){
                        ma = a[i]*abs(i-l);
                        idx = i;
                        keep=l;
                        cc='l';
                    }
                }
            }
        }
       // cout<<idx<<" "<<keep<<" "<<cc<<endl;
        if(cc == 'l'){
            ans[keep] = idx;
            l++;
            done[idx]=1;
        }
        else if(cc=='r'){
            ans[keep] = idx;
            r--;
            done[idx] =true;
        }
        cnt++;
    }
    ll o = 0;
    for(int i =1;i<=n;i++){
        ll idx;
        for(int j = 1;j<=n;j++){
            if(ans[j] == i){
                idx=j;break;
            }
        }
        //cout<<i<<" "<<idx<<endl;
        o += abs(idx-i)*a[i];
    }
    cout<<o<<endl;
}
 
 
int main(){
    ios_base::sync_with_stdio(0);
     cin.tie(NULL);
     
    ll t=1;
    //cin>>t;
    while(t--){
        solve();
    }
     
 
 
    return 0;     
    
        
}//42364286 418930126