#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  priority_queue<pair<int, int>> pq;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    pq.push({a, i});
  }
  long long ans = 0;
  int l = 0, r = n - 1;
  while (pq.size() >= 2) {
    auto [pval, p] = pq.top();
    pq.pop();
    auto [qval, q] = pq.top();
    pq.pop();
    if (p > q) {
      swap(p, q);
      swap(pval, qval);
    }
    ans += abs(r - p) * pval + abs(q - l) * qval;
    l++;
    r--;
  }
  if (!pq.empty()) {
    auto [pval, p] = pq.top();
    ans += abs(l - p) * pval;
  }
  cout << ans;
}
