#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
inline long long add(long long a, long long b) {
  long long res = a + b;
  if (res >= mod) res -= mod;
  return res;
}
inline long long sub(long long a, long long b) {
  long long res = a - b;
  if (res < 0) res += mod;
  return res;
}
inline long long mul(long long a, long long b) { return (a * b) % mod; }
long long modpow(long long a, long long b) {
  if (a == 0 || a == 1 || b == 1) return a;
  long long ans = 1;
  while (b) {
    if (b % 2) ans = mul(ans, a);
    b /= 2;
    a = mul(a, a);
  }
  return ans;
}
void solve() {
  long long n;
  cin >> n;
  long long a[n];
  map<long long, vector<long long>> pos;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    pos[a[i]].push_back(i + 1);
  }
  long long L = 1, R = n;
  long long ans1 = 0;
  map<long long, vector<long long>>::reverse_iterator it;
  for (it = pos.rbegin(); it != pos.rend(); it++) {
    vector<long long> v = it->second;
    long long m = it->first;
    set<long long> s;
    for (long long x : v) s.insert(x);
    while (!s.empty()) {
      long long Mx = 0;
      long long el;
      for (long long x : s) {
        long long tt = max(R - x, x - L);
        if (tt > Mx) {
          Mx = tt;
          el = x;
        }
      }
      ans1 += m * Mx;
      if (R - el == Mx) {
        R--;
      } else {
        L++;
      }
      s.erase(el);
    }
  }
  set<long long> s;
  for (long long i = 1; i <= n; i++) s.insert(i);
  long long ans2 = 0;
  R = n, L = 1;
  for (long long c = 0; c < n; c++) {
    long long Mx = 0, el = *s.begin();
    for (long long i : s) {
      long long pp = max(abs(R - i), abs(L - i));
      long long contrib = pp * a[i - 1];
      if (contrib > Mx) {
        Mx = contrib;
        el = i;
      }
    }
    ans2 += Mx;
    if (a[el - 1] * abs(R - el) == Mx)
      R--;
    else
      L++;
    s.erase(el);
  }
  cout << max(ans1, ans2) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  long long T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
