#include <bits/stdc++.h>
using namespace std;
int N = 0;
bool myfunc(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first != b.first) return a.first > b.first;
  return abs(N / 2 - a.second + 1) > abs(N / 2 - b.second + 1);
}
bool myfunc1(const pair<int, int> &a, const pair<int, int> &b) {
  return (max(N - a.second, a.second - 1) * a.first >
          max(N - b.second, b.second - 1) * b.first);
}
void solve() {
  long long n;
  cin >> n;
  N = n;
  long long tmp = 0, cnt = 0, ans = 0, tmp1 = 0, fg = 0, fg1 = 0;
  long long l = 1, r = n;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    v.push_back({tmp, i + 1});
  }
  long long ans1 = 0;
  sort(v.begin(), v.end(), myfunc);
  for (int i = 0; i < n; i++) {
    long long fg = v[i].second;
    if (abs(fg - l) >= abs(fg - r)) {
      ans += abs(fg - l) * v[i].first;
      l++;
    } else {
      ans += abs(fg - r) * v[i].first;
      r--;
    }
  }
  ans1 = max(ans, ans1);
  ans = 0;
  l = 1;
  r = n;
  sort(v.begin(), v.end(), myfunc1);
  for (int i = 0; i < n; i++) {
    long long fg = v[i].second;
    if (abs(fg - l) >= abs(fg - r)) {
      ans += abs(fg - l) * v[i].first;
      l++;
    } else {
      ans += abs(fg - r) * v[i].first;
      r--;
    }
  }
  ans1 = max(ans, ans1);
  cout << ans1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  solve();
  return 0;
}
