#include <bits/stdc++.h>
using namespace std;
int main() {
  long long ans = 0;
  long long ext;
  int whe;
  int n;
  cin >> n;
  vector<long long> a(n);
  priority_queue<pair<long long, int> > q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    q.push(make_pair(a[i] * max(abs(i), abs(n - 1 - i)), i));
  }
  int l = 0, r = n - 1;
  while (q.size() > 0) {
    ext = q.top().first;
    whe = q.top().second;
    q.pop();
    if (abs(l - whe) > abs(whe - r)) {
      ans += a[whe] * abs(l - whe);
      l++;
    } else if (abs(l - whe) < abs(whe - r)) {
      ans += a[whe] * abs(r - whe);
      r--;
    } else if (l != r) {
      int tmp = q.top().second;
      if (abs(l - tmp) > abs(tmp - r)) {
        ans += a[whe] * abs(r - whe);
        r--;
      } else {
        ans += a[whe] * abs(l - whe);
        l++;
      }
    } else {
      ans += a[whe] * abs(l - whe);
      l++;
    }
  }
  cout << ans << endl;
}
