#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1145141919810893364
//#define INF 810114514
#define PI 3.141592653589
typedef pair<int,int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define RREP(i,n) for(int i=(n)-1;i>=0;--i)
#define GOODBYE do { cout << "0" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug false


signed main(void){
    int N;
    int A[2020];
    
    cin >> N;
    REP(i,N){
        cin >> A[i];
    }
    
    vector<pair<int,int>> P;
    vector<int> Q(N,0);
    REP(i,N){
        P.push_back({A[i],-i});
    }
    
    sort(P.begin(),P.end(),greater<>());
    REP(i,N){P[i].second*=-1;}
    #if debug
    REP(i,N){cout << P[i].first << " ";}cout << endl;
    REP(i,N){cout << P[i].second << " ";}cout << endl;
    REP(i,N){cout << ((P[i].second>=i)?"+":"-") << " ";}cout << endl;
    #endif
    
    RREP(i,N){
        if(i==N-1){
            Q[i]=0;
        }else{
            Q[i]=Q[i+1]+(P[i+1].first*((P[i+1].second>=i+1)?1:-1));
        }
    }
    
    //REP(i,N){cout << Q[i] << " ";}cout << endl;
    
    vector<pair<int,int>> S,T;
    
    int now=0;
    REP(i,N){
        //cout << (Q[i]-P[i].first*(abs(i-P[i].second))+P[i].first*(abs(P[i].second-(N-1-now)))) << " ";
        if(P[i].second<i+1 && Q[i]-P[i].first*(abs(i-P[i].second))+P[i].first*(abs(P[i].second-(N-1-now)))>=0){
            now++;
            T.push_back(P[i]);
        }else{
            S.push_back(P[i]);
        }
    }
    //cout << endl;
    sort(S.begin(),S.end(),greater<>());
    sort(T.begin(),T.end());
    
    int Ans=0;
    REP(i,S.size()){
        Ans+=S[i].first*abs(i-S[i].second);
        //cout << S[i].first << " ";
    }
    REP(i,T.size()){
        Ans+=T[i].first*abs((i+(int)S.size())-T[i].second);
        //cout << T[i].first << " ";
    }
    cout << Ans << endl;
    return 0;
}

