#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char *argv[]) {
  using ll = long long;
  int n;
  cin >> n;
  vector<pair<ll, ll>> va;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    va.push_back({a, i});
  }
  sort(va.begin(), va.end(), greater<pair<ll, ll>>());
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
  ll ans = 0;
  for (int l = 0; l < n; l++) {
    for (int r = 0; r < n; r++) {
      if (l + r == n) {
        ans = max(ans, dp[l][r]);
        break;
      }
      auto [a, idx] = va[l + r];
      dp[l + 1][r] = dp[l][r] + abs(idx - l) * a;
      dp[l][r + 1] = dp[l][r] + abs(idx - (n - 1 - r)) * a;
    }
  }
  cout << ans << '\n';
  return 0;
}
