631_E. Product Sum_9700
Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special.

You are given an array a of length n. The characteristic of this array is the value <image> — the sum of the products of the values ai by i. One may perform the following operation exactly once: pick some element of the array and move to any position. In particular, it's allowed to move the element to the beginning or to the end of the array. Also, it's allowed to put it back to the initial position. The goal is to get the array with the maximum possible value of characteristic.

<image>

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 200 000) — the size of the array a.

The second line contains n integers ai (1 ≤ i ≤ n, |ai| ≤ 1 000 000) — the elements of the array a.

Output

Print a single integer — the maximum possible value of characteristic of a that can be obtained by performing no more than one move.

Examples

Input

4
4 3 2 5


Output

39

Input

5
1 1 2 7 1


Output

49

Input

3
1 1 2


Output

9

Note

In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be 3·1 + 2·2 + 4·3 + 5·4 = 39.

In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be 1·1 + 1·2 + 1·3 + 2·4 + 7·5 = 49.
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int M = 1e6 + 5;
const long long INF = 1e18;
long long values[N];
struct Line {
  long long k, m;
  Line(long long _k = 0, long long _m = 0) {
    k = _k;
    m = _m;
  }
  long long Get(long long val) { return k * val + m; }
};
struct LiChao_max {
  Line t[4 * N];
  bool vis[4 * N];
  void Set(Line v, int c, int ss, int se) {
    if (!vis[c]) {
      vis[c] = true;
      t[c] = v;
      return;
    }
    if (t[c].Get(values[ss]) >= v.Get(values[ss]) &&
        t[c].Get(values[se]) >= v.Get(values[se]))
      return;
    if (t[c].Get(values[ss]) < v.Get(values[ss]) &&
        t[c].Get(values[se]) < v.Get(values[se])) {
      t[c] = v;
      return;
    }
    int mid = (ss + se) >> 1;
    if (t[c].Get(values[ss]) >= v.Get(values[ss]) &&
        t[c].Get(values[mid]) >= v.Get(values[mid])) {
      Set(v, c << 1 | 1, mid + 1, se);
    } else if (t[c].Get(values[ss]) >= v.Get(values[ss]) &&
               t[c].Get(values[mid]) <= v.Get(values[mid])) {
      swap(t[c], v);
      Set(v, c << 1, ss, mid);
    } else if (t[c].Get(values[ss]) <= v.Get(values[ss]) &&
               t[c].Get(values[mid]) >= v.Get(values[mid])) {
      Set(v, c << 1, ss, mid);
    } else {
      swap(t[c], v);
      Set(v, c << 1 | 1, mid + 1, se);
    }
  }
  long long Get(long long ind, int c, int ss, int se) {
    if (!vis[c]) return -INF;
    long long sol = t[c].Get(ind);
    if (ss == se) return sol;
    int mid = (ss + se) >> 1;
    if (ind <= values[mid])
      return max(sol, Get(ind, c << 1, ss, mid));
    else
      return max(sol, Get(ind, c << 1 | 1, mid + 1, se));
  }
} Chao1, Chao2;
long long a[N], n, ans, S[N], sum;
vector<long long> b;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    S[i] = S[i - 1] + a[i];
    ans += a[i] * 1LL * i;
    b.push_back(a[i]);
  }
  sort(b.begin(), b.end());
  b.erase(unique(b.begin(), b.end()), b.end());
  int sz = (int)b.size();
  for (int i = 0; i < sz; i++) values[i + 1] = b[i];
  sum = ans;
  Chao1.Set(Line(n, -S[n]), 1, 1, sz);
  for (int i = n - 1; i >= 1; i--) {
    long long sol = Chao1.Get(a[i], 1, 1, sz) + S[i] - a[i] * 1LL * i;
    ans = max(ans, sum + sol);
    Chao1.Set(Line(i, -S[i]), 1, 1, sz);
  }
  Chao2.Set(Line(1, 0), 1, 1, sz);
  for (int i = 2; i <= n; i++) {
    long long sol = Chao2.Get(a[i], 1, 1, sz) + S[i - 1] - a[i] * 1LL * i;
    ans = max(ans, sum + sol);
    Chao2.Set(Line(i, -S[i - 1]), 1, 1, sz);
  }
  cout << ans;
  return 0;
}

5
1 1 2 7 1
 4
4 3 2 5
 3
1 1 2
 2
1 1
 5
-1 -2 -3 -4 -5
 2
-1000000 1000000
 2
-1 -1
 2
1000000 -1000000
 4
4 1 2 3
 4
2 3 4 1
 4
2 1 3 4
 2
0 0
 6
5 7 11 83 14 999
 5
1 2 5 4 5
 4
87 213 141 1
 4
0 0 0 0
 7
0 0 0 -1 0 0 0
 5
1 2 3 4 5
 2
0 1
 5
-1 -2 -3 0 -5
 2
-1000000 1001000
 2
-2 -1
 4
2 3 0 1
 4
2 1 4 4
 6
5 11 11 83 14 999
 5
1 2 2 4 5
 4
87 414 141 1
 4
0 0 -1 0
 7
0 0 0 -1 0 1 0
 5
1 2 3 2 5
 5
1 1 3 7 1
 4
8 3 2 5
 3
1 2 2
 2
-1000000 1001010
 2
-3 -1
 4
1 1 4 4
 6
5 11 11 13 14 999
 5
1 2 2 4 0
 4
87 414 33 1
 4
0 0 -1 1
 7
0 0 0 0 0 1 0
 5
1 2 3 2 3
 3
2 2 2
 2
-6 -1
 4
2 0 -1 1
 4
1 1 1 4
 2
0 -2
 6
5 11 11 13 14 1639
 5
1 2 2 1 0
 4
87 414 33 2
 4
0 -1 -1 1
 5
1 0 3 2 3
 4
8 3 1 6
 2
-6 0
 2
1001000 -1000000
 2
1 0
 2
-1 0
 4
2 0 0 1
 2
0 -1
 5
1 1 3 7 0
 4
8 3 1 5
 2
-1 -2
 3
2 2 1
 2
-1 1
 4
2 0 -2 1

49
 39
 9
 3
 -45
 1000000
 -3
 1000000
 30
 30
 30
 0
 6517
 62
 1378
 0
 -1
 55
 2
 -25
 1002000
 -4
 17
 33
 6525
 52
 2028
 -1
 5
 48
 53
 54
 11
 1002020
 -5
 31
 6176
 36
 1812
 3
 7
 38
 12
 -8
 9
 22
 -2
 10016
 21
 1815
 1
 34
 55
 -6
 1002000
 2
 -1
 11
 -1
 52
 52
 -4
 11
 1
 7
