#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, i;
  cin >> n;
  vector<pair<int, int>> v;
  for (i = 0; i < n; ++i) {
    cin >> k;
    v.push_back(make_pair(k, i));
  }
  sort(v.begin(), v.end());
  int ans = 0, lp = 0, rp = n - 1;
  for (i = n - 1; i >= 0; --i) {
    if (v[i].second > ((lp + rp) / 2)) {
      ans = ans + v[i].first * (v[i].second - lp);
      ++lp;
    } else {
      ans = ans + v[i].first * (rp - v[i].second);
      --rp;
    }
  }
  cout << ans;
  return 0;
}
