#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  ll n, count = 0, ans = 0;
  string s;
  cin >> n;
  vector<P> a;
  for (int i = 0; i < (int)(n); i++) {
    cin >> count;
    a.push_back(make_pair(count, i + 1));
  }
  sort(a.rbegin(), a.rend());
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1));
  dp[0][0] = 0;
  for (int i = 1; i <= (int)(n); i++) {
    dp[i][0] = dp[i - 1][0] + a[i - 1].first * (a[i - 1].second - i);
    dp[0][i] = dp[0][i - 1] + a[i - 1].first * ((n - i) - a[i - 1].second);
  }
  for (int i = 1; i <= (int)(n); i++) {
    for (int l = 1; l <= (int)(n); l++) {
      if (i + l > n) continue;
      dp[i][l] =
          max(dp[i][l - 1] +
                  a[i + l - 1].first * ((n - l + 1) - a[i + l - 1].second),
              dp[i - 1][l] + a[i + l - 1].first * (a[i + l - 1].second - i));
    }
  }
  for (int i = 0; i < (int)(n + 1); i++) {
    chmax(ans, dp[i][n - i]);
  }
  cout << ans << endl;
}
