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

#define int long long
#define F first
#define S second
#define fast ios:: sync_with_stdio(false),cin.tie(nullptr);
const int N = 2005;
int dp[N][N];
int n;
vector<pair<int,int>>a;
int solve(int l , int r){
   if(l > r)   return 0;
   int &res = dp[l][r];
   if(res != -1)  return res;
   int id = n - (r - l + 1);
   int pos = a[id].S;
   int val = a[id].F;
   res = max(res , solve(l+1 , r) + abs(pos - l)*val);
   res = max(res , solve(l , r-1) + abs(pos - r)*val);
   return res;
}
int32_t main(){
   fast;
   cin >> n;
   a.resize(n);
   for(int i=0 ; i<n ; i++){
      cin >> a[i].F;
      a[i].S = i;
   }
   sort(a.rbegin() , a.rend());
   memset(dp , -1 , sizeof(dp));
   cout << solve(0 , n-1);
}