#include <bits/stdc++.h>
using namespace std;
long long a[2010];
int main() {
  int n;
  cin >> n;
  vector<pair<long long, long long> > v;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back(make_pair(a[i], i));
  }
  sort(v.rbegin(), v.rend());
  long long leftmost = 0;
  long long rightmost = n - 1;
  long long cnt = 0;
  for (int i = 0; i < v.size(); i++) {
    long long left =
        (max(leftmost, v[i].second) - min(leftmost, v[i].second)) * v[i].first;
    long long right =
        (max(rightmost, v[i].second) - min(rightmost, v[i].second)) *
        v[i].first;
    if (left <= right) {
      cnt += right;
      rightmost--;
    } else {
      cnt += left;
      leftmost++;
    }
  }
  cout << cnt << endl;
  return 0;
}
