#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n;
vector<int> arr;
void solve() {
  vector<int> idx(n);
  for (int i = 0; i < n; ++i) {
    idx[i] = i;
  }
  sort(idx.begin(), idx.end(), [](int i, int j) { return arr[i] > arr[j]; });
  vector<vector<int> > dp(n, vector<int>(n, 0));
  int last = idx[n - 1];
  for (int i = 0; i < n; ++i) {
    dp[i][i] = arr[last] * abs(last - i);
  }
  for (int l = 1; l < n; ++l) {
    for (int i = 0; i + l < arr.size(); ++i) {
      int j = i + l;
      int ind = idx[n - l - 1];
      dp[i][j] = max(dp[i + 1][j] + arr[ind] * abs(ind - i),
                     dp[i][j - 1] + arr[ind] * abs(ind - j));
    }
  }
  cout << dp[0][n - 1] << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  arr = vector<int>(n);
  for (int i = 0; i < arr.size(); ++i) {
    cin >> arr[i];
  }
  solve();
  return 0;
}
