#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(int i=a;i<b;i++)
#define fd(i,a,b) for(int i=a;i>b;i--)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pw 1000000000
#define pi 3.141592653589793238
#define fv(i,v) for(auto i=v.begin();i!=v.end();++i)
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}
int large(int x,map<int,int> g,int n)
{
    int y=x;
    f(i,0,n)
    {
        if(i==x)
            continue;
        if(g[i]==1)
            continue;
        else if(g[i]==0){
            if(abs(x-i)>abs(x-y))
                y=i;
        }
    }
    return y;
}
int main()
{
    int t=1;
    //cin>>t;
    while(t--)
    {
       int n;
       cin>>n;
       //string s;
       int a[n];
       f(i,0,n)
       cin>>a[i];
       vector<pair<int,int>> v;
       f(i,0,n)
       v.pb(mp(i,a[i]));
       sort(v.begin(),v.end(),sortbysec);
       int mh=0;
       map<int,int> g;
       f(i,0,n)
       {
           int y=large(v[i].first,g,n);
           g[y]++;
           mh+=v[i].second*abs(v[i].first-y);
       }
       map<int,int> m1;
       f(i,0,n)
       m1[a[i]]++;
       fv(i,v)
       {if(m1[v.second]>1)
       {cout<<mh+1;return 0;}}
       cout<<mh;
       //sort(a,a+n);
       cout<<endl;
    }
    return 0;
}