#include<iostream>
#include<math.h>

#define ll long long

using namespace std;

int main(){
  int N;
  cin >> N;
  ll a[N-1];
  int index[N-1];
  for(int i=0;i<N;i++){
    cin >> a[i];
    index[i]=i;
  }

  /* sort with index*/

  for(int i=0;i<N;i++){
    for(int j=0; j<N-i-1;j++){
      if(a[j]<a[j+1]){
        swap(a[j],a[j+1]);
        swap(index[j],index[j+1]);
      }
    }
  }
/*
  for(int i=0;i<N;i++)
    cout << a[i] << " ";
  cout <<endl;

  for(int i=0;i<N;i++)
    cout << index[i] << " ";
  cout <<endl;
*/

  ll dp[N+1][N+1] = {};
  for(int i=1;i<N+1;i++){
    for(int j=0; j<i+1;j++){
      if(j==0)
        dp[i-j][j] = dp[i-j-1][j] + a[i-1]*abs(index[i-1]-i+j+1);
      else if(i==j)
        dp[i-j][j] = dp[i-j][j-1] + a[i-1]*abs(N-index[i-1]-j);
      else
        dp[i-j][j] = max(dp[i-j-1][j] + a[i-1]*abs(index[i-1]-i+j+1), dp[i-j][j-1] + a[i-1]*abs(N-index[i-1]-j));
    }
  }

  ll max=0;
  for(int i=0;i<N+1;i++)
    if(dp[N-i][i] > max) max = dp[N-i][i];


  cout << max << endl;
/*
  for(int i=0;i<N+1;i++){
    for(int j=0; j<N-i+1;j++){
      cout << dp[i][j] << "\t";
      }
    cout << endl;
  }
*/
  return 0;
}