#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-12;
int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (N); ++i) {
    cin >> A[i];
  }
  long long ret = 0;
  vector<int> ord(N);
  for (int i = 0; i < (N); ++i) ord[i] = i;
  bool changed;
  do {
    changed = false;
    for (int i = 0; i < (N - 1); ++i)
      for (int j = (i + 1); j < (N); ++j) {
        long long cur = (long long)abs(ord[i] - i) * A[i] +
                        (long long)abs(ord[j] - j) * A[j];
        long long next = (long long)abs(ord[j] - i) * A[i] +
                         (long long)abs(ord[i] - j) * A[j];
        if (next > cur) {
          swap(ord[i], ord[j]);
          ret += (next - cur);
          changed = true;
        }
      }
  } while (changed);
  cout << ret << endl;
  return 0;
}
