#include <bits/stdc++.h>
using namespace std;
int main() {
  multimap<long long, long long, greater<long long>> mp;
  long long N, A, left[2], right[2], ans[2] = {};
  cin >> N;
  for (int i = 0; i < 2; i++) left[i] = 1;
  for (int i = 0; i < 2; i++) right[i] = N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    mp.insert(make_pair(A, i + 1));
  }
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (abs(it->second - left[0]) > abs(right[0] - it->second)) {
      ans[0] += it->first * abs(it->second - left[0]);
      left[0]++;
    } else {
      ans[0] += it->first * abs(right[0] - it->second);
      right[0]--;
    }
    if (abs(it->second - left[1]) > abs(right[1] - it->second)) {
      if (it == mp.begin()) {
        ans[1] += it->first * abs(right[1] - it->second);
        right[1]--;
      } else {
        ans[1] += it->first * abs(it->second - left[1]);
        left[1]++;
      }
    } else {
      if (it != mp.begin()) {
        ans[1] += it->first * abs(right[1] - it->second);
        right[1]--;
      } else {
        ans[1] += it->first * abs(it->second - left[1]);
        left[1]++;
      }
    }
  }
  cout << max(ans[0], ans[1]) << endl;
  return 0;
}
