#include <bits/stdc++.h>
struct read_item {
  read_item(){};
  template <class T>
  operator T() {
    T t;
    std::cin >> t;
    return t;
  }
};
char splf(int i, int n) { return (i + 1 < n) ? ' ' : '\n'; };
template <bool up>
struct _RI {
  int i;
  _RI(int a) : i(a) {}
  int operator*() { return i; }
  void operator++() { i += (up ? 1 : -1); }
  bool operator!=(const _RI& r) { return up ? i < r.i : i >= r.i; }
};
template <bool up>
struct _RX {
  _RI<up> x, y;
  _RI<up> begin() { return x; }
  _RI<up> end() { return y; }
  _RX(int a, int b) : x(up ? a : (a - 1)), y(b) {}
  _RX(int a) : _RX(up ? 0 : a, up ? a : 0) {}
};
const int64_t inf = (int64_t)1.05e18;
using namespace std;
int main() {
  const int N = read_item();
  vector<int> A(N);
  for (auto i : _RX<true>(N)) {
    A[i] = read_item();
  }
  vector<pair<int, int>> sorted(N);
  for (auto i : _RX<true>(N)) {
    sorted[i] = {A[i], i};
  }
  sort(sorted.rbegin(), sorted.rend());
  vector<vector<int64_t>> dp(N + 1);
  for (auto& d : dp) {
    d.resize(N + 1, -inf);
  }
  dp[0][0] = 0;
  for (auto i : _RX<true>(N)) {
    for (auto j : _RX<true>(N)) {
      auto& a = dp[i + 1][j];
      const int64_t v = sorted[i].first;
      const int pos = sorted[i].second;
      const int left = j - 1;
      const int right = N - (i + 1 - j);
      if (left >= 0) {
        const int64_t b = dp[i][j - 1] + abs(pos - left) * v;
        a = max(a, b);
      }
      if (right < N) {
        const int64_t b = dp[i][j] + abs(pos - right) * v;
        a = max(a, b);
      }
    }
  }
  int64_t ans = dp[N][0];
  for (auto i : _RX<true>(N + 1)) {
    ans = max(ans, dp[N][i]);
  }
  printf("%lld\n", ans);
  return 0;
}
