#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const int INF = 1e9 + 100;
const ll INF64 = 7e18l;
const int mod = 1000000007;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  ll n;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort((a).begin(), (a).end(), greater<pair<ll, ll>>());
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; i + j < n; i++) {
      dp[i + 1][j] =
          max(dp[i + 1][j], dp[i][j] + a[i + j].first * (a[i + j].second - i));
      dp[i][j + 1] =
          max(dp[i][j + 1],
              dp[i][j] + a[i + j].first * (n - 1 - j - a[i + j].second));
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; i++) ans = max(ans, dp[i][n - i]);
  cout << ans << endl;
  return 0;
}
