#include <bits/stdc++.h>
using namespace std;
int n;
long long int ori, res;
long long int a[200000 + 1], sum[200000 + 2];
int lowerBound1(long long int* sum, vector<int>& st, long long int o) {
  if (st.size() <= 1) return 0;
  int s = 0, e = st.size() - 2, ret = st.size() - 1;
  while (s <= e) {
    int m = (s + e) / 2;
    if ((sum[st[m + 1] - 1] - sum[st[m] - 1]) < o * (st[m + 1] - st[m]))
      s = m + 1;
    else {
      e = m - 1;
      ret = m;
    }
  }
  return ret;
}
int lowerBound2(long long int* sum, vector<int>& st, long long int o) {
  if (st.size() <= 1) return 0;
  int s = 1, e = st.size() - 1, ret = st.size() - 1;
  while (s <= e) {
    int m = (s + e) / 2;
    if ((sum[st[m - 1]] - sum[st[m]]) > o * (st[m - 1] - st[m]))
      s = m + 1;
    else {
      e = m - 1;
      ret = m - 1;
    }
  }
  return ret;
}
long long int solve() {
  long long int ret = 0;
  vector<int> st;
  for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
  for (int i = 1; i <= n; ++i) {
    while (
        st.size() >= 2 &&
        (sum[i - 1] - sum[st[st.size() - 1] - 1]) * (i - st[st.size() - 2]) <=
            (sum[i - 1] - sum[st[st.size() - 2] - 1]) * (i - st[st.size() - 1]))
      st.pop_back();
    st.push_back(i);
    int op = lowerBound1(sum, st, a[i]);
    ret = max(ret, -(i - st[op]) * a[i] + (sum[i - 1] - sum[st[op] - 1]));
  }
  st.clear();
  for (int i = n; i >= 1; --i) {
    while (st.size() >= 2 &&
           (sum[st[st.size() - 1]] - sum[i]) * (st[st.size() - 2] - i) >=
               (sum[st[st.size() - 2]] - sum[i]) * (st[st.size() - 1] - i))
      st.pop_back();
    st.push_back(i);
    int op = lowerBound2(sum, st, a[i]);
    ret = max(ret, (st[op] - i) * a[i] - (sum[st[op]] - sum[i]));
  }
  st.clear();
  return ret;
}
long long int solve1() {
  long long int ori = 0;
  for (int i = 1; i <= n; ++i) ori += a[i] * i;
  return max(ori, ori + solve());
}
long long int solve2() {
  long long int res = 0, ori = 0;
  for (int i = 1; i <= n; ++i) ori += a[i] * i;
  for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
  for (int i = 1; i <= n; ++i) {
    for (int j = i; j <= n; ++j) {
      res = max(res, ori + (j - i) * a[i] - (sum[j] - sum[i]));
      res = max(res, ori + (sum[j - 1] - sum[i - 1]) - (j - i) * a[j]);
    }
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
  printf("%lld\n", solve1());
  return 0;
}
