#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline string toString(const T& a) {
  ostringstream oss;
  oss << a;
  return oss.str();
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& vec) {
  os << "{ ";
  for (ll i = (0); i < (vec.size()); i++)
    os << ((i == 0) ? "" : ", ") << i << ":" << vec[i];
  os << " }";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<vector<T>>& vec) {
  os << "{" << endl;
  for (ll i = (0); i < (vec.size()); i++)
    os << "\t   " << i << ":" << vec[i] << ((i == vec.size() - 1) ? "" : ",")
       << endl;
  os << "\t }";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, set<T>& p) {
  os << "{ ";
  for (auto itr = (p).begin(); itr != (p).end(); itr++)
    os << ((itr == p.begin()) ? "" : ", ") << *itr;
  os << " }";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, queue<T>& p) {
  queue<T> q(p);
  os << "[< " << (q.empty() ? " <]" : "");
  while (!q.empty()) {
    os << q.front();
    q.pop();
    os << (q.empty() ? " <]" : ", ");
  }
  return os;
}
template <class T>
ostream& operator<<(ostream& os, priority_queue<T>& p) {
  priority_queue<T> q(p);
  os << "[< " << (q.empty() ? " <]" : "");
  while (!q.empty()) {
    os << q.top();
    q.pop();
    os << (q.empty() ? " <]" : ", ");
  }
  return os;
}
template <class T, class U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T, class U>
ostream& operator<<(ostream& os, map<T, U>& p) {
  os << "{ ";
  for (auto itr = (p).begin(); itr != (p).end(); itr++)
    os << ((itr == p.begin()) ? "" : ", ") << itr->first << ":" << itr->second;
  os << " }";
  return os;
}
void dump_f(string& nm, char t) { cerr << endl; }
template <class T, class... Ts>
void dump_f(string& nm, char t, T&& x, Ts&&... ts) {
  int l = nm.find_first_of(',');
  string name(nm, 0, l), nx(nm, l + 1, -1);
  if (t == '\n')
    cerr << "  " << name << "\t: " << x << t;
  else
    cerr << name << ": " << x << "," << t;
  dump_f(nx, t, forward<Ts>(ts)...);
}
template <class... Ts>
void dump_m(int&& ln, string&& nm, char t, Ts&&... ts) {
  cerr << "[ln: " << ln << "]" << t;
  dump_f(nm, t, forward<Ts>(ts)...);
}
void sort2vectors(vector<ll>& av, vector<ll>& bv) {
  int n = av.size();
  vector<int> p(n);
  vector<ll> av2(n), bv2(n);
  iota(p.begin(), p.end(), 0);
  sort(p.begin(), p.end(), [&](int a, int b) { return av[a] > av[b]; });
  for (int i = 0; i < n; i++) {
    av2[i] = av[p[i]];
    bv2[i] = bv[p[i]];
  }
  av = av2;
  bv = bv2;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<ll> A(N), idx(N);
  for (ll i = (0); i < (N); i++) cin >> A[i];
  std::iota((idx).begin(), (idx).end(), 0);
  sort2vectors(A, idx);
  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, 0));
  for (ll i = (0); i < (N); i++) {
    for (ll y = (0); y < (i + 2); y++) {
      ll x = i + 1 - y;
      if (x != 0) chmax(dp[x][y], dp[x - 1][y] + A[i] * (idx[i] - y));
      if (y != 0) chmax(dp[x][y], dp[x][y - 1] + A[i] * ((N - y) - idx[i]));
    }
  }
  ll ans = 0;
  for (ll i = (0); i < (N + 1); i++) {
    chmax(ans, dp[i][N - i]);
  }
  cout << ans << endl;
  return (0);
}
