#include <bits/stdc++.h>
using namespace std;
struct CHT {
  int ptr, sz;
  vector<long long int> M, C;
  CHT() {
    ptr = sz = 0;
    M.clear();
    C.clear();
  }
  bool bad(long long int m, long long int c) {
    return (long double)(c - C[sz - 2]) / (1.0 * M[sz - 2] - m) <
           (long double)(C[sz - 1] - C[sz - 2]) / (1.0 * M[sz - 2] - M[sz - 1]);
  }
  void addLine(long long int m, long long int c) {
    while (sz >= 2 && bad(m, c)) {
      M.pop_back();
      C.pop_back();
      sz--;
    }
    M.push_back(m);
    C.push_back(c);
    sz++;
  }
  void printHull() {
    cout << "HULL" << endl;
    for (int i = 0; i < sz; i++)
      cout << i << " : " << M[i] << ' ' << C[i] << endl;
  }
  long long int getY(int pos, long long int x) { return M[pos] * x + C[pos]; }
  long long int sortedQuery(long long int x) {
    if (ptr >= sz) ptr = sz - 1;
    while (ptr < sz - 1 && getY(ptr + 1, x) <= getY(ptr, x)) ptr++;
    return M[ptr] * x + C[ptr];
  }
  double intersection(int a, int b) {
    return -1.0 * (C[a] - C[b]) / (M[a] - M[b]);
  }
  long long int BSQuery(long long int x) {
    int lo = 0, hi = sz - 2, md;
    if (sz == 1) {
      return getY(sz - 1, x);
    }
    if (intersection(hi, hi + 1) <= x) return M[sz - 1] * x + C[sz - 1];
    while (lo < hi) {
      md = (lo + hi) / 2;
      if (intersection(md, md + 1) >= x)
        hi = md;
      else
        lo = md + 1;
    }
    return M[hi] * x + C[hi];
  }
};
int n;
long long int a[200005], lft[200005], rgt[200005], M[200005], S[200005], ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) lft[i] = lft[i - 1] + i * a[i];
  for (int i = n; i >= 1; i--) rgt[i] = rgt[i + 1] + i * a[i];
  for (int i = 1; i <= n; i++) M[i] = M[i - 1] + i * a[i];
  for (int i = 1; i <= n; i++) S[i] = S[i - 1] + a[i];
  CHT cht = CHT();
  ans = lft[n];
  for (int i = 1; i <= n; i++) {
    cht.addLine(i, lft[i - 1] - M[i - 1] - S[i - 1]);
    ans = max(ans, cht.BSQuery(a[i]) + rgt[i + 1] + M[i - 1] + S[i - 1]);
  }
  cht = CHT();
  for (int i = n; i >= 1; i--) {
    cht.addLine(-i, rgt[i + 1] + M[i] - S[i]);
    ans = max(ans, cht.BSQuery(-a[i]) + lft[i - 1] - M[i] + S[i]);
  }
  printf("%lld\n", ans);
}
