#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<P> a(n);
  rep(i, n) cin >> a[i].first, a[i].second = i;
  sort(a.rbegin(), a.rend());

  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));

  rep(i, n) for (int j = 0; i + j < n; ++j) {
    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));
  }
  rep(i, n + 1) ans = max(ans, dp[i][n - i]);
  cout << ans << endl;
  return 0;
}
