#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  long long A[N];
  priority_queue<long long> a;
  for (int i = 0; i < N; i++) cin >> A[i], a.push(A[i]);
  long long left = 0;
  long long right = N - 1;
  long long ans = 0;
  long long top, tmp_j;
  for (int i = 0; i < N && a.size() != 0; i++) {
    top = a.top();
    for (int j = 0; j < N; j++) {
      if (A[j] == top) {
        tmp_j = j;
        break;
      }
    }
    if (abs(tmp_j - left) < abs(tmp_j - right))
      ans += top * abs(tmp_j - right), --right;
    else
      ans += top * abs(tmp_j - left), ++left;
    a.pop();
  }
  cout << ans << endl;
  return 0;
}
