#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  priority_queue<pair<long long, long long> > pq;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    pq.push({x, i});
  }
  long long ans = 0, val;
  long long j = 0;
  int flag = -1;
  while (!pq.empty()) {
    long long x = pq.top().first;
    long long idx = pq.top().second;
    pq.pop();
    if (flag == 1) {
      val = n - 1 - idx - j;
      flag = -1;
      j++;
    } else if (flag == 0) {
      val = idx - j;
      flag = -1;
      j++;
    } else {
      if (idx - j > n - idx - 1 - j) {
        val = idx - j;
        flag = 1;
      } else {
        val = n - idx - 1 - j;
        flag = 0;
      }
    }
    ans += val * x;
  }
  cout << ans << endl;
  return 0;
}
