#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll is_query = -LLONG_MAX;
struct Line {
  ll m, b;
  mutable function<const Line*()> succ;
  bool operator<(const Line& rhs) const {
    if (rhs.b != is_query) return m < rhs.m;
    const Line* s = succ();
    if (!s) return 0;
    ll x = rhs.m;
    return b - s->b < (s->m - m) * x;
  }
};
struct HullDynamic : public multiset<Line> {
  bool bad(iterator y) {
    auto z = next(y);
    if (y == begin()) {
      if (z == end()) return 0;
      return y->m == z->m && y->b <= z->b;
    }
    auto x = prev(y);
    if (z == end()) return y->m == x->m && y->b <= x->b;
    return ld(x->b - y->b) * (z->m - y->m) >= ld(y->b - z->b) * (y->m - x->m);
  }
  void insert_line(ll m, ll b) {
    auto y = insert({m, b});
    if (bad(y)) {
      erase(y);
      return;
    }
    while (next(y) != end() && bad(next(y))) erase(next(y));
    y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
    while (y != begin() && bad(prev(y))) erase(prev(y));
    if (y != begin()) prev(y)->succ = [=] { return &*y; };
  }
  ll eval(ll x) {
    auto l = *lower_bound((Line){x, is_query});
    return (l.m * x + l.b);
  }
};
using pii = pair<ll, ll>;
ll solve(std::vector<pii> v, ll dir = 1LL) {
  int n = v.size();
  std::vector<ll> P(n, 0), Q(n, 0);
  for (int i = 1; i < v.size(); i++) {
    P[i] = P[i - 1] + v[i].first * v[i].second;
    Q[i] = Q[i - 1] + v[i].first;
  }
  HullDynamic hull;
  hull.insert_line(v[1].first, dir * Q[1] - P[1]);
  ll ret = -LLONG_MAX;
  for (int i = 2; i < n; i++) {
    ll now = hull.eval(v[i].second) + P[n - 1] - dir * Q[i];
    ret = max(ret, now);
    hull.insert_line(v[i].first, P[i - 1] + dir * Q[i] - P[i]);
  }
  return ret;
}
int main() {
  int n;
  cin >> n;
  std::vector<pii> v(n + 1);
  ll ans = 0LL;
  for (int i = 1; i <= n; i++) {
    cin >> v[i].first;
    v[i].second = i;
    ans += v[i].first * v[i].second;
  }
  ans = max(ans, solve(v));
  reverse(v.begin() + 1, v.end());
  ans = max(ans, solve(v, -1LL));
  cout << ans << endl;
}
