#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
const int mod = 1000000007;
const int max_n = 200005;
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;
}
int main() {
  ll ng = INF, ok = 0;
  ll n;
  cin >> n;
  vpl v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i].first;
    v[i].second = i + 1;
  }
  sort((v).begin(), (v).end());
  ll ans = 0;
  if (n % 2 == 0) {
    for (ll i = 0; i < n / 2; i++) {
      ll a = v[i * 2].first, b = v[i * 2 + 1].first;
      ll k = n / 2 + i + 1, kk = n / 2 - i;
      ll tmp = abs(v[i * 2].second - k) * a + abs(v[i * 2 + 1].second - kk) * b;
      ll tmpt =
          abs(v[i * 2].second - kk) * a + abs(v[i * 2 + 1].second - k) * b;
      ans += max(tmp, tmpt);
    }
    cout << ans << endl;
  }
  if (n % 2 == 1) {
    ans += v[0].first * abs(v[0].second - (n / 2 + 1));
    for (ll i = 0; i < n / 2; i++) {
      ll a = v[i * 2 + 1].first, b = v[i * 2 + 2].first;
      ll k = n / 2 - i, kk = n / 2 + i + 2;
      ll tmp =
          abs(v[i * 2 + 1].second - k) * a + abs(v[i * 2 + 2].second - kk) * b;
      ll tmpt =
          abs(v[i * 2 + 1].second - kk) * a + abs(v[i * 2 + 2].second - k) * b;
      ans += max(tmp, tmpt);
    }
    cout << ans << endl;
  }
}
