#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > m;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    m.push_back(make_pair(tmp, i));
  }
  int l = 0, r = n - 1;
  long long int res = 0;
  for (int i = 0; i < n; i++) {
    auto mi = m.begin();
    int maxr = -10;
    int maxk = -10;
    int rest = 0;
    bool rl;
    for (int k = l; k <= r; k++) {
      long long int a = mi->first, b = mi->second;
      if (maxr <= a) {
        if (rest < a * max(abs(b - l), abs(r - b))) {
          maxr = a;
          if (abs(b - l) > abs(r - b)) {
            rl = false;
            maxk = b;
            rest = a * abs(b - l);
          } else {
            rl = true;
            maxk = b;
            rest = a * abs(r - b);
          }
        }
      }
      mi++;
    }
    res += rest;
    swap(m[maxk], m[n - 1 - i]);
    if (rl) {
      r--;
    } else {
      l++;
    }
  }
  cout << res << endl;
  return 0;
}
