//Author ::  ABHINAV3010 ::

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef pair<ll,ll> p;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
ll mod=1000000007;
ll gcd(ll a,ll b)
{
    return (b==0)?a:gcd(b,a%b);
}
ll power(ll x, ll y, ll p=mod)
{ll res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1;x = (x*x) % p;}return res;}
int main()
{
fast;
ll n;
cin>>n;
vector<p>v;
for(ll i=0;i<n;i++)
{
    ll a;
    cin>>a;
    v.pb({a,i});
}
ll i=0;
ll j=n-1;
ll ans=0;
while(i<=j)
{
    ll mx=0;
    ll side=-1;
    ll pos=-1;
    ll ele=-1;
    for(ll k=i;k<=j;k++)
    {
        ll val1=v[k].fi*abs(v[k].se-i);
        ll val2=v[k].fi*abs(v[k].se-j);
        if(val1>mx)
        {
            mx=val1;
            side=i;
            pos=k;
            ele=v[k].fi;
        }else if(val1==mx)
        {
            if(v[k].fi>ele)
            {
                mx=val1;
                side=i;
                pos=k;
                ele=v[k].fi;
            }
        }
        if(val2>mx)
        {
            mx=val2;
            side=j;
            pos=k;
            ele=v[k].fi;
        }else if(val2==mx)
        {
            if(v[k].fi>ele)
            {
                mx=val2;
                side=j;
                pos=k;
                ele=v[k].fi;
            }
        }
    }
    //cout<<side<<" "<<pos<<" "<<ele<<" "<<mx<<endl;
    ans+=mx;
    swap(v[side],v[pos]);
    if(side==i)
    {
        i++;
    }else
    {
        j--;
    }

}
cout<<ans<<endl;

return 0;
}
