#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main() {
  int n;
  cin >> n;
  vector<pair<ll, int>> a(n);
  ll sum = 0;
  for (int i = 0; i < (int)(n); i++) {
    cin >> a.at(i).first;
    a.at(i).second = i;
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  int begin = 0, end = n - 1;
  for (int i = 0; i < (int)(n); i++) {
    int center = (begin + end) / 2;
    if (a.at(i).second < center) {
      sum += a.at(i).first * abs(a.at(i).second - end);
      end--;
    } else {
      sum += a.at(i).first * abs(a.at(i).second - begin);
      begin++;
    }
  }
  cout << sum << endl;
}
