#include<bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) > (b); --(i))
#define pb push_back
#define pr pair<int,int>
#define mp make_pair
#define ll long long int
const int mod=1e9+7;
int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)cin>>a[i];
    int vis[n]={0};
    int left=0;
    int right=n-1;
    int sum=0;
    vector<pair<int,int>>p;
    for(int i=n-1;i>=0;i--){
        p.pb(mp(a[i],i));
    }
    sort(p.begin(),p.end());
    for(int i=p.size()-1;i>=0;i--){
        if(abs(left-p[i].second)>=abs(right-p[i].second)){
            sum+=p[i].first*abs(left-p[i].second);
            left++;
        }
        if(abs(left-p[i].second)<abs(right-p[i].second)){
            sum+=p[i].first*abs(right-p[i].second);
            right--;
        }
    }
    cout<<sum;
}