#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long gcdll(long long a, long long b) {
  if (!b) return a;
  return gcdll(b, a % b);
}
void seive(int n) {
  vector<bool> is_prime(n + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
}
vector<int> constructZFunc(string s) {
  int n = s.size();
  vector<int> z(n);
  int l, r;
  l = r = 0;
  z[0] = 0;
  for (int i = 1; i <= n - 1; i++) {
    z[i] = 0;
    if (i <= r) {
      z[i] = min(r - i + 1, z[i - l]);
    }
    while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
    if (i + z[i] - 1 > r) {
      r = i + z[i] - 1;
      l = i;
    }
  }
  return z;
}
void solve() {
  long long n;
  cin >> n;
  pair<long long, long long> a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a, a + n, greater<pair<long long, long long> >());
  long long res = 0ll;
  long long st = 0, en = n - 1, pos;
  map<pair<int, int>, long long> mp;
  mp[{0, n}] = a[0].first * (llabs(a[0].second - 0));
  mp[{-1, n - 1}] = a[0].first * (llabs(a[0].second - n + 1));
  for (int i = 1; i <= n - 1; i++) {
    map<pair<int, int>, long long> tmp;
    for (auto it : mp) {
      st = it.first.first + 1;
      en = it.first.second - 1;
      if (st + n - 1 - en != i) continue;
      tmp[{st, en + 1}] =
          max(tmp[{st, en + 1}],
              it.second + a[i].first * (llabs(a[i].second - st)));
      tmp[{st - 1, en}] =
          max(tmp[{st - 1, en}],
              it.second + a[i].first * (llabs(a[i].second - en)));
    }
    for (auto it : tmp) {
      mp[it.first] = max(it.second, mp[it.first]);
    }
  }
  long long ans = 0;
  for (auto it : mp) {
    ans = max(ans, it.second);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
