#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n + 1];
  vector<pair<int, int>> v;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    v.push_back({arr[i], i});
  }
  long long int sum = 0;
  sort(v.begin(), v.end());
  int left = 1;
  int right = n;
  for (int i = v.size() - 1; i >= 0; i--) {
    int x = v[i].second;
    int e = v[i].first;
    if (abs(x - left) > abs(x - right)) {
      sum += (abs(x - left) * e);
      left++;
    } else {
      sum += (abs(x - right) * e);
      right--;
    }
  }
  cout << sum << endl;
  return 0;
}
