#include <bits/stdc++.h>
using namespace std;
[[maybe_unused]] const int MOD = 1000000007;
[[maybe_unused]] const int INTMAX = numeric_limits<int>::max();
void writeln() { cout << "\n"; }
template <class T>
inline void print(T&& a);
template <class T>
inline void priws(T&& a);
template <class T>
inline void read(T& a);
template <class... Args>
inline void readln(Args&... args) {
  (read(args), ...);
}
template <class H, class... T>
inline void writeln(H&& h, T&&... t) {
  priws(h);
  (print(t), ...);
  writeln();
}
void run() {
  int n;
  readln(n);
  vector<pair<int, int>> a(n);
  for (int i = 0; i < (int)(n); ++i) readln(a[i].first), a[i].second = i;
  set<int> indices;
  for (int i = 0; i < (int)(n); ++i) indices.insert(i);
  sort(begin(a), end(a));
  reverse(begin(a), end(a));
  function<long long(set<int>, int)> get = [&](set<int> indices, int index) {
    long long ans = 0;
    for (int j = index; j < (int)(n); ++j) {
      auto& [x, i] = a[j];
      int left = abs(*indices.begin() - i);
      int right = abs(*indices.rbegin() - i);
      if (left > right + 10)
        ans += x * 1ll * max(left, right), indices.erase(indices.begin());
      else if (right > left + 10)
        ans += x * 1ll * max(left, right), indices.erase(*indices.rbegin());
      else {
        int l = *indices.begin();
        indices.erase(indices.begin());
        long long leftAns = get(indices, j + 1);
        indices.insert(l);
        indices.erase(*indices.rbegin());
        long long rightAns = get(indices, j + 1);
        return ans + max(leftAns + x * 1ll * left, rightAns + x * 1ll * right);
      }
    }
    return ans;
  };
  writeln(get(indices, 0));
}
int main() {
  ios_base::sync_with_stdio(false);
  run();
  cerr << fixed << setprecision(0)
       << "Execution time = " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
template <class T1, class T2>
inline ostream& operator<<(ostream& os, pair<T1, T2> const& _a);
template <
    typename T, typename D = decltype(*begin(declval<T>())),
    typename enable_if<!is_same<T, basic_string<char>>::value>::type* = nullptr>
ostream& operator<<(ostream& os, T const& _a) {
  auto it = begin(_a);
  if (it != end(_a)) os << *it++;
  while (it != end(_a))
    os << "\n "[is_fundamental<typename T::value_type>::value] << *it++;
  return os;
}
template <class T1, class T2>
inline ostream& operator<<(ostream& os, pair<T1, T2> const& _a) {
  return os << _a.first << " " << _a.second;
}
template <class T1, class T2>
inline istream& operator>>(istream& is, pair<T1, T2>& _a) {
  return is >> _a.first >> _a.second;
}
template <class T>
inline istream& operator>>(istream& is, vector<T>& _a) {
  for (int i = 0; i < (int)(_a.size()); ++i) is >> _a[i];
  return is;
}
template <class T>
inline void print(T&& _a) {
  cout << " " << _a;
}
template <class T>
inline void priws(T&& _a) {
  cout << _a;
}
template <class T>
inline void read(T& _a) {
  cin >> _a;
}
