#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const long long MAX_N = 200100;
bool leftsort(pair<long long, long long> lhs, pair<long long, long long> rhs) {
  long long a = lhs.first;
  long long b = lhs.second;
  long long c = rhs.first;
  long long d = rhs.second;
  if (a == c)
    return b > d;
  else
    return a < c;
}
bool rightsort(pair<long long, long long> lhs, pair<long long, long long> rhs) {
  long long a = lhs.first;
  long long b = lhs.second;
  long long c = rhs.first;
  long long d = rhs.second;
  if (a == c)
    return b < d;
  else
    return a > c;
}
signed main() {
  long long N;
  cin >> N;
  vector<long long> inp(N);
  for (long long i = 0; i < N; i++) {
    cin >> inp[i];
  }
  if (N % 2 == 0) {
    vector<pair<long long, long long>> left;
    vector<pair<long long, long long>> right;
    for (long long i = 0; i < N; i++) {
      if (i < N / 2)
        left.emplace_back(make_pair(inp[i], i));
      else
        right.emplace_back(make_pair(inp[i], i));
    }
    sort((left).begin(), (left).end(), leftsort);
    sort((right).begin(), (right).end(), rightsort);
    long long ans = 0;
    for (long long i = 0; i < N; i++) {
      pair<long long, long long> tmp;
      if (i < N / 2) {
        tmp = right[i];
        ans += abs(tmp.second - i) * tmp.first;
      } else {
        tmp = left[i - N / 2];
        ans += abs(tmp.second - i) * tmp.first;
      }
    }
    cout << ans << endl;
  } else {
    long long aans = 0;
    for (long long jj = 0; jj < 2; jj++) {
      vector<pair<long long, long long>> left;
      vector<pair<long long, long long>> right;
      if (jj == 0) {
        for (long long i = 0; i < N; i++) {
          if (i < N / 2)
            left.emplace_back(make_pair(inp[i], i));
          else
            right.emplace_back(make_pair(inp[i], i));
        }
      } else {
        for (long long i = 0; i < N; i++) {
          if (i < N / 2 + 1)
            left.emplace_back(make_pair(inp[i], i));
          else
            right.emplace_back(make_pair(inp[i], i));
        }
      }
      sort((left).begin(), (left).end(), leftsort);
      sort((right).begin(), (right).end(), rightsort);
      long long ans = 0;
      for (long long i = 0; i < N; i++) {
        pair<long long, long long> tmp;
        if (i < right.size()) {
          tmp = right[i];
          ans += abs(tmp.second - i) * tmp.first;
        } else {
          tmp = left[i - right.size()];
          ans += abs(tmp.second - i) * tmp.first;
        }
      }
      aans = max(aans, ans);
    }
    cout << aans << endl;
  }
  return 0;
}
