#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int LSB(int x) { return x & -x; }
int log2(int x) { return 31 - __builtin_clz(x); }
int st_size(int size) {
  return (size == LSB(size) ? size : 1 << (log2(size) + 1)) << 1;
}
struct cht {
  struct line {
    ll m, b;
    ll eval(ll x) const { return m * x + b; }
  };
  const int size;
  vector<line> st;
  const function<bool(ll, ll)> cmp;
  cht(int size, const function<bool(ll, ll)>& cmp = less<ll>())
      : size(size), cmp(cmp) {}
  int L(int p) { return (p << 1) + 1; }
  int R(int p) { return (p << 1) + 2; }
  int M(int s, int e) { return (s + e) >> 1; }
  void add(const line& l, int p, int s, int e) {
    if (cmp(l.eval(M(s, e)), st[p].eval(M(s, e)))) st[p] = l;
    if (s != e) {
      if ((st[p].eval(s) < l.eval(s)) !=
          (st[p].eval(M(s, e)) < l.eval(M(s, e)))) {
        add(l, L(p), s, M(s, e));
      } else {
        add(l, R(p), M(s, e) + 1, e);
      }
    }
  }
  void add(ll m, ll b) {
    if (st.empty()) {
      st.resize(st_size(size), {m, b});
    } else {
      add({m, b}, 0, 0, size - 1);
    }
  }
  ll opt(ll x, ll y) { return cmp(x, y) ? x : y; }
  ll opt(ll x, int p, int s, int e) {
    ll ans = st[p].eval(x);
    if (s != e) {
      ans = opt(ans, x <= M(s, e) ? opt(x, L(p), s, M(s, e))
                                  : opt(x, R(p), M(s, e) + 1, e));
    }
    return ans;
  }
  ll opt(ll x) { return opt(x, 0, 0, size - 1); }
};
ll solve(int n, vector<ll>& a, vector<ll>& psum, vector<ll>& rpsum, int start_i,
         int delta_i) {
  ll ans = numeric_limits<ll>::min(), delta = 0;
  cht hull{n, greater<ll>()};
  for (int i = start_i, j = 1; j <= n; i += delta_i, j++) {
    delta -= a[i] * (i - delta_i);
    hull.add(a[i], psum[i - delta_i] + delta);
    ans = max(ans, hull.opt(i) - delta + rpsum[i + delta_i]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<ll> a(n + 2), aprod(n + 2), psum(n + 2), rpsum(n + 2);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    aprod[i] = a[i] * i;
  }
  partial_sum(aprod.begin(), aprod.end(), psum.begin());
  partial_sum(aprod.rbegin(), aprod.rend(), rpsum.rbegin());
  cout << max(solve(n, a, psum, rpsum, 1, 1), solve(n, a, rpsum, psum, n, -1))
       << endl;
  return 0;
}
