#include <bits/stdc++.h>
using namespace std;
int main() {
  map<long long, int, greater<long long> > order;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long temp;
    cin >> temp;
    order.insert(make_pair(temp, i));
  }
  long long sum = 0;
  int begin = 1;
  int end = n;
  for (auto i = order.begin(); i != order.end(); ++i) {
    if (abs(i->second - begin) > abs(i->second - end)) {
      sum += abs(i->second - begin) * i->first;
      begin++;
    } else {
      sum += abs(i->second - end) * i->first;
      end--;
    }
  }
  cout << sum << endl;
  return 0;
}
