#include <bits/stdc++.h>
using namespace std;
int n;
vector<long long int> a;
long long int solve() {
  if (n == 1) return 0;
  vector<vector<long long int>> dp =
      vector<vector<long long int>>(n + 1, vector<long long int>(n, 0));
  vector<pair<long long int, long long int>> order;
  for (int i = 0; i < n; i++) order.emplace_back(a[i], i);
  sort(order.begin(), order.end(),
       [](pair<long long int, long long int> L,
          pair<long long int, long long int> R) {
         if (L.first != R.first) return L.first > R.first;
         return L.second < R.second;
       });
  for (int i = 1; i <= n; i++) {
    dp[0][i] =
        dp[0][i - 1] + abs(order[i - 1].second - n + i) * order[i - 1].first;
    dp[i][0] =
        dp[i - 1][0] + abs(order[i - 1].second - i + 1) * order[i - 1].first;
  }
  for (int i = 1; i <= n; i++)
    for (int L = 1; L < i; L++) {
      int R = i - L;
      dp[L][R] =
          max(dp[L][R], max(dp[L - 1][R] + abs(order[i - 1].second - L + 1) *
                                               order[i - 1].first,
                            dp[L][R - 1] + abs(order[i - 1].second - n + R) *
                                               order[i - 1].first));
    }
  long long int ans = 0;
  for (int L = 0; L <= n; L++) ans = max(ans, dp[L][n - L]);
  dp.clear();
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  while (cin >> n) {
    a = vector<long long int>(n);
    for (auto &&x : a) cin >> x;
    cout << solve() << endl;
    a.clear();
  }
}
