#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int MAX = 10000000;
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  vector<long long> a(N);
  vector<pair<long long, long long> > c(N);
  for (int i = 0; i < (int)(N); i++) {
    cin >> a[i];
    c[i].first = a[i];
    c[i].second = i;
  }
  vector<int> b(N);
  for (int i = 0; i < (int)(N); i++) {
    b[i] = i;
  }
  long long ans = 0;
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());
  for (int i = 0; i < (int)(N / 2); i++) {
    pair<long long, long long> p1 = c[2 * i];
    pair<long long, long long> p2 = c[2 * i + 1];
    ans += max(
        p1.first * abs(p1.second - i) + p2.first * abs(p2.second - (N - i - 1)),
        p1.first * abs(p1.second - (N - i - 1)) +
            p2.first * abs(p2.second - i));
  }
  if (N % 2) {
    ans += c[N / 2].first * abs(c[N / 2].second - N / 2);
  }
  cout << ans << endl;
}
