#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const long long int LINF = 100100100100100100;
const double EPS = 1e-10;
const long double PI = acos(-1.0L);
__attribute__((constructor)) void initial() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
signed main() {
  int n;
  cin >> n;
  vector<pair<long long int, int>> a(n);
  for (int i = 0; i < (int)(n); i++) {
    long long int c;
    cin >> c;
    a.push_back(make_pair(c, i + 1));
  }
  sort((a).rbegin(), (a).rend());
  long long int dp[n][n];
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(n); j++) dp[i][j] = -1;
  dp[0][0] = 0LL;
  for (int idx = 0; idx < (int)(n); idx++) {
    for (int i = 0; i < (int)(idx + 1); i++) {
      if (dp[i][idx - i] == -1) continue;
      dp[i + 1][idx - i] =
          (((dp[i + 1][idx - i]) <
            (dp[i][idx - i] + a[idx].first * abs(i + 1 - a[idx].second)))
               ? (dp[i][idx - i] + a[idx].first * abs(i + 1 - a[idx].second))
               : (dp[i + 1][idx - i]));
      dp[i][idx - i + 1] =
          (((dp[i][idx - i + 1]) <
            (dp[i][idx - i] +
             a[idx].first * abs(n - (idx - i) - a[idx].second)))
               ? (dp[i][idx - i] +
                  a[idx].first * abs(n - (idx - i) - a[idx].second))
               : (dp[i][idx - i + 1]));
    }
  }
  long long int ans = 0LL;
  for (int i = 0; i < (int)(n + 1); i++)
    ans = (((ans) < (dp[i][n - i])) ? (dp[i][n - i]) : (ans));
  cout << ans << endl;
  return 0;
}
