#include <bits/stdc++.h>
using namespace std;
int vector_finder(vector<int> vec, int number) {
  auto itr = find(vec.begin(), vec.end(), number);
  size_t index = distance(vec.begin(), itr);
  if (index != vec.size()) {
    return 1;
  } else {
    return 0;
  }
}
int main() {
  int n;
  cin >> n;
  priority_queue<pair<int, int> > a;
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    a.push(make_pair(c, i));
  }
  int max = n;
  int min = 1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int check = a.top().second;
    int acti = a.top().first;
    a.pop();
    if (max - check > check - min) {
      ans += acti * (max - check);
      max--;
    } else {
      ans += acti * (check - min);
      min++;
    }
  }
  cout << ans << endl;
}
