#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pair<int, int>> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i].first;
    v[i].second = i;
  }
  sort(v.begin(), v.end());
  vector<vector<long long int>> dp(n, vector<long long int>(n));
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i; j < n; j++) {
      int pos = j - i;
      if (i == j) {
        dp[i][j] = v[pos].first * abs(i - v[pos].second);
        continue;
      }
      dp[i][j] = max(v[pos].first * abs(i - v[pos].second) + dp[i + 1][j],
                     v[pos].first * abs(j - v[pos].second) + dp[i][j - 1]);
    }
  }
  cout << dp[0][n - 1] << "\n";
}
