#include <bits/stdc++.h>
using namespace std;
long long n, a[2010], pos[2010], ans = 0;
vector<pair<long long, long long> > v;
long long getval(long long x, long long y) {
  return abs(pos[x] - y) * a[y] + abs(x - pos[y]) * a[x] -
         abs(x - pos[x]) * a[x] - abs(y - pos[y]) * a[y];
}
int main() {
  long long i, j, x, y, mx;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    pos[i] = i;
  }
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      v.push_back(make_pair(i, j));
    }
  }
  while (1) {
    mx = 0;
    x = -1;
    y = -1;
    for (i = 0; i < v.size(); i++) {
      if (mx < getval(v[i].first, v[i].second)) {
        mx = getval(v[i].first, v[i].second);
        x = v[i].first;
        y = v[i].second;
      }
    }
    if (x == -1 && y == -1) {
      break;
    }
    swap(pos[x], pos[y]);
    v.erase(find(v.begin(), v.end(), make_pair(x, y)));
  }
  for (i = 0; i < n; i++) {
    ans += a[i] * abs(pos[i] - i);
  }
  cout << ans << endl;
  return 0;
}
