#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
ll ans;
ll ansa;
ll N;
pair<ll,ll> A[20005];
pair<ll,ll> B[20005];
pair<ll,ll> C[20005];
ll dp[20005][20005];




int main(){
    cin >> N;
    for(ll i = 0;i<N;i++){
        ll a;
        cin >> a;
        A[i] = pair<ll,ll> (a,i);
    }
    sort(A,A+N);
    for (int j=1;j<=N;j++){
        dp[0][j] = dp[0][j-1]+A[N-j].first*(N-j-A[N-j].second);
    }
    for (int i=1;i<=N;i++){
        dp[i][0] = dp[i-1][0]+A[N-i].first*(A[N-i].second-i+1);
        for (int j=1;j<=N;j++){
            dp[i][j] = max(dp[i-1][j]+A[N-i-j].first*(A[N-i-j].second-i+1),dp[i][j-1]+A[N-i-j].first*(N-j-A[N-i-j].second));
        }
    }
    
    for (int i=0;i<N;i++){
        if(ans<dp[i][N-i]){
            ans = dp[i][N-i];
        }
    }

    
    cout << ans << endl;

}