#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> r(n);

  rep(i, n) {
    r[i] = i;
  }

  rep(i, n) {
    cin >> a[i];
  }

  rep(i, n - 1) {
    int m = 0;
    int k = i;
    for (int j = i + 1; j < n; j++) {
      int v = 0
        + abs(r[j] - i) * a[r[j]] 
        + abs(r[i] - j) * a[r[i]]
        - abs(r[i] - i) * a[r[i]]
        - abs(r[j] - j) * a[r[j]]; 
      if (v > m) {
        m = v;
        k = j;
      }
    }
    if (k != i) {
      int t = r[k];
      r[k] = r[i];
      r[i] = t;
    }

  int s = 0;

  rep(i, n) {
    s += abs(r[i] - i) * a[r[i]];
  }

  cout << s << endl;

}
