#include <bits/stdc++.h>
using ll = long long;
using namespace std;
using P = pair<ll, int>;
int main() {
  int n;
  cin >> n;
  int i = 0;
  P babies[n];
  for (auto &bi : babies) {
    cin >> bi.first;
    bi.second = i++;
  }
  sort(babies, babies + n, greater<P>());
  ll dp[n][n + 1];
  fill(dp[0], dp[n], 0);
  for (int w = n; w > 0; w--) {
    P baby = babies[n - w];
    for (int j = 0; j + w <= n; j++) {
      dp[j][j + w - 1] =
          max(dp[j][j + w - 1],
              dp[j][j + w] + baby.first * abs(j + w - 1 - baby.second));
      dp[j + 1][j + w] = max(dp[j + 1][j + w],
                             dp[j][j + w] + baby.first * abs(j - baby.second));
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; i++) ans = max(ans, dp[i][i]);
  cout << ans << '\n';
  return 0;
}
