#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j;
  cin >> n;
  int ar[n];
  int temp[n];
  for (i = 0; i < n; i++) {
    cin >> ar[i];
    temp[i] = ar[i];
  }
  int l = 0, sum = 0;
  int r = n - 1;
  sort(temp, temp + n);
  unordered_map<int, bool> mp;
  for (i = n - 1; i >= 0; i--) {
    for (j = 0; j < n; j++) {
      if (temp[i] == ar[j] && !mp.count(j) > 0) {
        if (abs(j - l) > abs(j - r)) {
          sum += ar[j] * abs(j - l);
          l++;
        } else {
          sum += ar[j] * abs(j - r);
          r--;
        }
        mp[j] = true;
      }
    }
  }
  cout << sum;
}
