#include <bits/stdc++.h>
using namespace std;

bool islarge(pair<int,int> a, pair<int,int> b){
  return a.first > b.first;
}

int main(){
  int n; 
  cin >> n;

  vector<pair<int,int>> A;
  for(int i=0; i<n; i++){
    int a;
    cin >> a;
    A.push_back(make_pair(a,i));
  }

  // 大きい順
  sort(A.begin(), A.end(), islarge);

  // DP
  long int X[n+1][n+1]={0};
  long int maxX = 0;

  // iorj=0;
  for(int i=0; i<n; i++){
    X[i+1][0] = X[i][0] + abs(A[i].second-i)*A[i].first;
    X[0][i+1] = X[0][i] + abs(n-1-i-A[i].second)*A[i].first;
  }
  maxX = max(X[n][0],X[0][n]);

  for(int i=1; i<n; i++){
    for (int j=1;j<=i;j++){
      //int I = i;
      X[i+1-j][j] = max( 
        X[i-j][j] + abs( i-j - A[i].second ) * A[i].first
        ,X[i+1-j][j-1] + abs( n-j - A[i].second ) * A[i].first
      );
      if(maxX<X[i+1-j][j]) maxX = X[i+1-j][j];
    }
   // cout << A.at(i).first << A.at(i).second<< endl;
  }

  /*
  for(int i = 0; i<=n; i++){
    for (int j=0; j<=n; j++){
      cout << X[i][j] << " ";
    }
    cout << endl;
  }
  */
  cout << maxX << endl;
  


   return 0;
}