#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
#define PI 3.141592653589793

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