#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    vector<pair<ll,ll>> a(n);
    vector<ll> aa;
    for(int i=0;i<n;++i){
        cin>>a[i].first;
        a[i].second = i+1;
        aa.push_back(a[i].first);
    }
    ll vis[n+1]={0};
    ll mn = 1;
    ll mx = n;
    ll ans = 0;
    sort(a.rbegin(),a.rend());
    for(int i=0;i<n;++i){
        if(!vis[a[i].second]){
            if(abs(a[i].second-mn) >= abs(a[i].second-mx)){
                ans += a[i].first*abs(a[i].second-mn);
                mn = min(mn+1,n);
            }
            else{
                ans += a[i].first*abs(a[i].second-mx);
                mx = max(mx-1,0LL);   
            }
            ++vis[a[i].second];
        }
    }
    cout<<ans<<'\n';
    return 0;
}