#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1e18;
int main() {
  ll n, m, i, j, k;
  cin >> n;
  vector<ll> A(n);
  map<ll, deque<ll>> mp;
  for (i = 0; i < n; i++) {
    ll p;
    cin >> p;
    A[i] = p;
    mp[p].push_back(1 + i);
  }
  ll ret = 0;
  sort(A.rbegin(), A.rend());
  for (i = 1; i <= n / 2; i++) {
    ll cnt = A[(i - 1) * 2] * (mp[A[(i - 1) * 2]].back() - i);
    cnt += A[(i - 1) * 2 + 1] * (n - i + 1 - mp[A[(i - 1) * 2 + 1]].front());
    ll cnt2 = A[(i - 1) * 2] * (n - i + 1 - mp[A[(i - 1) * 2]].front());
    cnt2 += A[(i - 1) * 2 + 1] * (mp[A[(i - 1) * 2 + 1]].back() - i);
    if (cnt > cnt2) {
      ret += cnt;
      mp[A[(i - 1) * 2]].pop_back();
      mp[A[(i - 1) * 2 + 1]].pop_front();
    } else {
      ret += cnt2;
      mp[A[(i - 1) * 2]].pop_front();
      mp[A[(i - 1) * 2 + 1]].pop_back();
    }
  }
  if (n % 2 == 1) {
    ret += A[n / 2] * abs(mp[A[n / 2]].front() - (n / 2 + 1));
  }
  cout << ret << endl;
  return 0;
}
