#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
template <typename T>
bool chmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T>
bool chmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return {l.first + r.first, l.second + r.second};
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return {l.first - r.first, l.second - r.second};
}
template <typename T>
pair<T, T> operator*(const pair<T, T> &l, const T &r) {
  return {l.first * r, l.second * r};
}
template <typename T>
pair<T, T> operator/(const pair<T, T> &l, const T &r) {
  return {l.first / r, l.second / r};
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto v : vec) os << v << ",";
  os << "]";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const deque<T> &vec) {
  os << "deq[";
  for (auto v : vec) os << v << ",";
  os << "]";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &vec) {
  os << "{";
  for (auto v : vec) os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &vec) {
  os << "{";
  for (auto v : vec) os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &vec) {
  os << "{";
  for (auto v : vec) os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) {
  os << "{";
  for (auto v : vec) os << v << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &pa) {
  is >> pa.first >> pa.second;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
  os << "(" << pa.first << "," << pa.second << ")";
  return os;
}
template <typename... Ts>
istream &operator>>(istream &is, tuple<Ts...> &theTuple) {
  apply([&is](Ts &...tupleArgs) { ((is >> tupleArgs), ...); }, theTuple);
  return is;
}
template <typename... Ts>
ostream &operator<<(ostream &os, const tuple<Ts...> &theTuple) {
  apply(
      [&os](const Ts &...tupleArgs) {
        os << '(';
        size_t n(0);
        ((os << tupleArgs << (++n < sizeof...(Ts) ? "," : "")), ...);
        os << ')';
      },
      theTuple);
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
  os << "{";
  for (auto v : mp) os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) {
  os << "{";
  for (auto v : mp) os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}
template <typename T>
void reset(vector<T> &v, const T reset_to) {
  for (auto &x : v) x = reset_to;
}
inline int popcount(const unsigned int x) { return __builtin_popcount(x); }
ll nC2(ll n) { return n * (n - 1) / 2; }
const int intinf = numeric_limits<int>::max();
const ll llinf = numeric_limits<ll>::max();
const pii udlr[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void solve() {
  int N;
  cin >> N;
  vector<int> A(N);
  cin >> A;
  vector<pii> A_sorted(N);
  for (int i = 0; i < (int)(N); i++) A_sorted[i] = {A[i], i};
  sort((A_sorted).rbegin(), (A_sorted).rend());
  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, -llinf));
  dp[0][0] = 0;
  for (int n = (0); n <= (N - 1); n++)
    for (int l = (0); l <= (n); l++) {
      auto [a, i] = A_sorted[n];
      int r = n - l;
      chmax(dp[l + 1][r], dp[l][r] + a * (i - l));
      chmax(dp[l][r + 1], dp[l][r] + a * ((N - 1 - r) - i));
    }
  ll ans = 0;
  for (int l = (0); l <= (N); l++) {
    int r = N - l;
    chmax(ans, dp[l][r]);
  }
  assert(ans >= 0);
  cout << ans << endl;
}
int main() {
  cerr << "start" << endl;
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
