#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;
const ll MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
inline int toint(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class t>
inline string tostring(t x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class Integer>
inline Integer get_power(Integer base, ull exponential) {
  Integer result = 1;
  while (exponential >= 1) {
    if (exponential & 1) {
      result = result * base;
    }
    base = base * base;
    exponential >>= 1;
  }
  return result;
}
template <class Integer>
class ResidueInteger {
 private:
  Integer mod_;
  Integer n_;
  static Integer global_mod_;
  static inline pair<Integer, Integer> getBezoutsIdentitySolution(Integer a,
                                                                  Integer b) {
    if (b == 0) {
      return {1, 0};
    }
    auto sol = getBezoutsIdentitySolution(b, a % b);
    return {sol.second, sol.first - (a / b) * sol.second};
  }

 public:
  static Integer default_mod_;
  Integer getModValue(const Integer& n) const {
    return (n % mod_ + mod_) % mod_;
  }
  inline Integer getModInverse(const Integer& n) const {
    auto sol = getBezoutsIdentitySolution(n, mod_);
    if (n * sol.first + mod_ * sol.second != 1) {
      return -1;
    }
    return getModValue(sol.first);
  }
  ResidueInteger() : mod_(default_mod_), n_(getModValue(0)) {}
  ResidueInteger(Integer n) : mod_(default_mod_), n_(getModValue(n)) {}
  ResidueInteger(Integer n, Integer mod) : mod_(mod), n_(getModValue(n)) {}
  inline ResidueInteger operator+(const ResidueInteger& rhs) const {
    if (this->mod_ != rhs.mod_) {
      throw invalid_argument("The mods of both operands should be equal.");
    }
    return {this->n_ + rhs.n_, this->mod_};
  }
  inline ResidueInteger operator+(const Integer& rhs) const {
    return {this->n_ + getModValue(rhs), this->mod_};
  }
  friend inline ResidueInteger operator+(const Integer& lhs,
                                         const ResidueInteger& rhs) {
    return {rhs.getModValue(lhs) + rhs.n_, rhs.mod_};
  }
  inline ResidueInteger& operator+=(const ResidueInteger& rhs) {
    if (this->mod_ != rhs.mod_) {
      throw invalid_argument("The mods of both operands should be equal.");
    }
    this->n_ = getModValue(this->n_ + rhs.n_);
    return *this;
  }
  inline ResidueInteger& operator+=(const Integer& rhs) {
    this->n_ = getModValue(this->n_ + getModValue(rhs));
    return *this;
  }
  inline ResidueInteger operator-(const ResidueInteger& rhs) const {
    if (this->mod_ != rhs.mod_) {
      throw invalid_argument("The mods of both operands should be equal.");
    }
    return {this->n_ - rhs.n_, this->mod_};
  }
  inline ResidueInteger operator-(const Integer& rhs) const {
    return {this->n_ - getModValue(rhs), this->mod_};
  }
  friend inline ResidueInteger operator-(const Integer& lhs,
                                         const ResidueInteger& rhs) {
    return {rhs.getModValue(lhs) - rhs.n_, rhs.mod_};
  }
  inline ResidueInteger& operator-=(const ResidueInteger& rhs) {
    if (this->mod_ != rhs.mod_) {
      throw invalid_argument("The mods of both operands should be equal.");
    }
    this->n_ = getModValue(this->n_ - rhs.n_);
    return *this;
  }
  inline ResidueInteger& operator-=(const Integer& rhs) {
    this->n_ = getModValue(this->n_ - getModValue(rhs));
    return *this;
  }
  inline ResidueInteger operator*(const ResidueInteger& rhs) const {
    if (this->mod_ != rhs.mod_) {
      throw invalid_argument("The mods of both operands should be equal.");
    }
    return {this->n_ * rhs.n_, this->mod_};
  }
  inline ResidueInteger operator*(const Integer& rhs) const {
    return {this->n_ * getModValue(rhs), this->mod_};
  }
  friend inline ResidueInteger operator*(const Integer& lhs,
                                         const ResidueInteger& rhs) {
    return {rhs.getModValue(lhs) * rhs.n_, rhs.mod_};
  }
  inline ResidueInteger& operator*=(const ResidueInteger& rhs) {
    if (this->mod_ != rhs.mod_) {
      throw invalid_argument("The mods of both operands should be equal.");
    }
    this->n_ = getModValue(this->n_ * rhs.n_);
    return *this;
  }
  inline ResidueInteger& operator*=(const Integer& rhs) {
    this->n_ = getModValue(this->n_ * getModValue(rhs));
    return *this;
  }
  inline ResidueInteger operator/(const ResidueInteger& rhs) const {
    if (this->mod_ != rhs.mod_) {
      throw invalid_argument("The mods of both operands should be equal.");
    }
    return {this->n_ * getModInverse(rhs.n_), this->mod_};
  }
  inline ResidueInteger operator/(const Integer& rhs) const {
    return {this->n_ * getModInverse(getModValue(rhs)), this->mod_};
  }
  friend inline ResidueInteger operator/(const Integer& lhs,
                                         const ResidueInteger& rhs) {
    return {rhs.getModValue(lhs) * rhs.getModInverse(rhs.n_), rhs.mod_};
  }
  inline ResidueInteger& operator/=(const ResidueInteger& rhs) {
    if (this->mod_ != rhs.mod_) {
      throw invalid_argument("The mods of both operands should be equal.");
    }
    this->n_ = getModValue(this->n_ * getModInverse(rhs.n_));
    return *this;
  }
  inline ResidueInteger& operator/=(const Integer& rhs) {
    this->n_ = getModValue(this->n_ * getModInverse(getModValue(rhs)));
    return *this;
  }
  inline bool operator==(const ResidueInteger& rhs) const {
    return (this->n_ == rhs.n_) && (this->mod_ == rhs.mod_);
  }
  inline bool operator==(const Integer& rhs) const {
    return this->n_ == getModValue(rhs);
  }
  friend bool operator==(const Integer& lhs, const ResidueInteger& rhs) {
    return rhs.getModValue(lhs) == rhs.n_;
  }
  Integer get_n() const { return n_; }
  friend std::ostream& operator<<(std::ostream& lhs,
                                  const ResidueInteger& rhs) {
    return lhs << rhs.n_;
  }
  friend std::istream& operator>>(std::istream& lhs, ResidueInteger& rhs) {
    lhs >> rhs.n_;
    return lhs;
  }
};
using rll = ResidueInteger<ll>;
template <>
ll rll::default_mod_ = 1000000007LL;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<pair<ll, ll>> as(N);
  ll a;
  for (ll i = 0; i < N; ++i) {
    cin >> a;
    as[i] = make_pair(a, i);
  }
  sort(as.begin(), as.end(), greater<pair<ll, ll>>());
  vector<vector<ll>> dps(N + 1, vector<ll>(N + 1, -(1LL << 32)));
  dps[0][0] = 0;
  for (ll n = 0; n < N; ++n) {
    auto val = as[n];
    for (ll i = 0; i <= n; ++i) {
      ll j = n - i;
      dps[i + 1][j] =
          max(dps[i + 1][j], dps[i][j] + val.first * (val.second - i));
      dps[i][j + 1] =
          max(dps[i][j + 1], dps[i][j] + val.first * (N - j - 1 - val.second));
    }
  }
  ll res = -1;
  for (ll i = 0; i <= N; ++i) {
    res = max(res, dps[i][N - i]);
  }
  cout << res << endl;
  return 0;
}
