#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 100100100100100;
const int MOD = 1000000007;
long long mod_pow(long long N, long long M) {
  if (M == 0) return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  if (M & 1) res = (res * N) % MOD;
  return res %= MOD;
}
int main() {
  int N;
  cin >> N;
  vector<int> retu(N);
  for (int i = 0; i < N; i++) cin >> retu.at(i);
  vector<pair<int, int> > num(N);
  for (int i = 0; i < N; i++) {
    num.at(i).first = retu.at(i);
    num.at(i).second = i;
  }
  sort(num.begin(), num.end());
  reverse(num.begin(), num.end());
  long long ansL = 0, ansR = 0;
  int left = 0, right = N - 1;
  for (int i = 0; i < N; i++) {
    int A = num.at(i).first;
    int place = num.at(i).second;
    if (i == 0) {
      ansL += A * abs(left - place);
      left++;
      continue;
    }
    if (abs(place - left) > abs(place - right)) {
      ansL += A * abs(left - place);
      left++;
    } else {
      ansL += A * abs(right - place);
      right--;
    }
  }
  left = 0;
  right = N - 1;
  for (int i = 0; i < N; i++) {
    int A = num.at(i).first;
    int place = num.at(i).second;
    if (i == 0) {
      ansR += A * abs(right - place);
      right--;
      continue;
    }
    if (abs(place - left) > abs(place - right)) {
      ansR += A * abs(left - place);
      left++;
    } else {
      ansR += A * abs(right - place);
      right--;
    }
  }
  cout << max(ansL, ansR) << endl;
}
