#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int64_t res = 0;
  vector<bool> moved(n, false);
  int l = 0, r = n - 1;
  while (l <= r) {
    int64_t mx = -1;
    vector<int64_t> sl(n, -1);
    vector<int64_t> sr(n, -1);
    for (int i = 0; i < n; i++) {
      if (!moved.at(i)) {
        int64_t ai = a.at(i);
        int64_t sil = ai * abs(i - l);
        int64_t sir = ai * abs(i - r);
        sl.at(i) = sil;
        sr.at(i) = sir;
        mx = max(mx, max(sil, sir));
      }
    }
    for (int i = 0; i < n; i++) {
      if (!moved.at(i) && sr.at(i) == mx) {
        res += mx;
        moved.at(i) = true;
        r--;
        break;
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (!moved.at(i) && sl.at(i) == mx) {
        res += mx;
        moved.at(i) = true;
        l++;
        break;
      }
    }
  }
  cout << res << endl;
}
