#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#define ll long long int
#define double /*long*/ double
#define endl '\n'
#define vll vector<ll>
#define vvl vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define vppll vector<pll,pll>
#define mp make_pair
#define pb push_back
#define mapll map<ll,ll>
#define fir first
#define sec second
#define _cin ios_base::sync_with_stdio(0);  cin.tie(0);
#define fo(i,b) for(i=0;i<b;i++)
#define repa(i,a,b) for(i=a;i<b;i++)
#define repb(i,a,b) for(i=a;i>=b;i--)
#define all(x) (x).begin(), (x).end()
#define s(v) (ll)v.size()
#define watch(x) cerr << (#x) << " is " << (x) << endl
#define Time cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 
const long long int MAX = (ll)(1e18+1);
const long long int MIN = (ll)(-1e18-1);
const long long int mod = (ll)(1e9+7);
using namespace std;  // order_of_key(k) -> Number of elements strictly smaller than k
using namespace __gnu_pbds; // order_of_key() ; *( find_by_order() ) -> kth element in set;

ll max(ll a,ll b,ll c) {return max(max(a, b), c);}
ll min( ll a, ll b, ll c) { return min(min(a, b), c);}
ll max(ll a,ll b) {return (a>b) ? a : b;}
ll min(ll a,ll b) {return (a<b) ? a : b;}
ll power(ll a, ll n) {ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power_mod(ll a, ll n, ll mod_){ ll p=1; while(n){ if(n%2){ p=(p*a)%mod_;} n/=2; a=(a*a)%mod_;} return p%mod_;}                 
                          /*Code Begins*/




int main(){
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif
    _cin;
    // cout << setprecision(15);
    ll mn = MAX, mx = MIN;
    ll n, m, t,  k, i, j,  sum=0,  prev, flag=0, cnt=0;
    ll x=0, y=0, fx, diff, tot=0, l , r;
    int TC=1;
    // cin >> TC;
while(TC--){
    cin >> n;
    vll a(n);
    fo(i,n) cin >> a[i];
    l=0;r=0;
    vll suml, sumr;
    suml.pb(0);
    sumr.pb(0);
    ll score=0;
    fo(i,n){
        ll kk=0, lft=1;
        ll tmp, mxtmp=0;
        fo(j,s(suml)){
            tmp = score-suml.back()+suml[j]+abs(i-j)*a[i];
            if(tmp>mxtmp){
                mxtmp=tmp;
                kk=j;

            }
        }
        fo(j,s(sumr)){
            tmp = score -sumr.back()+ sumr[j] + abs(n-1-i-j)*a[i];
            if(tmp>mxtmp){
                mxtmp=tmp;
                kk=j;
                lft=0;
            }
        }
        score = mxtmp;
        if(lft){
            x = s(suml);
            suml.pb(0);
            repb(j,x,kk) suml[j+1] = suml[j]+a[i];
        }else{
            x = s(sumr);
            sumr.pb(0);
            repb(j,x,kk) sumr[j+1] = sumr[j]+a[i]; 
        }
    }
    // for(auto x : suml) cout << x << ' ';
    //     cout << endl;
    // for(auto x : sumr) cout << x << ' ';
    //     cout << endl;
    cout << score;
}

    Time;
    return 0;
}
