#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<pair<long long, long long>> a(n);
  for (long long i = (0); i < (long long)(n); i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.begin(), a.end(), greater<pair<long long, long long>>());
  long long l = 0, r = n - 1;
  long long ans = 0;
  vector<bool> seen(n, false);
  vector<long long> debug(n);
  for (long long itr = (0); itr < (long long)(n); itr++) {
    long long mx = -1, id, left;
    for (long long i = (0); i < (long long)(n); i++) {
      if (seen[i]) continue;
      long long dl = a[i].second - l;
      long long dr = r - a[i].second;
      long long tmp = a[i].first * max(dl, dr);
      if (mx < tmp) {
        mx = tmp;
        id = i;
        if (dl > dr)
          left = 1;
        else if (dl == dr) {
          long long lmx = -1, rmx = -1, lid, rid;
          for (long long j = (0); j < (long long)(n); j++) {
            if (seen[j] || i == j) continue;
            long long dl = a[j].second - l;
            long long dr = r - a[j].second;
            if (lmx < max(a[j].first * (dl - 1), a[j].first * dr)) {
              lmx = max(a[j].first * (dl - 1), a[j].first * dr);
              lid = j;
            }
            if (rmx < max(a[j].first * dl, a[j].first * (dr - 1))) {
              rmx = max(a[j].first * dl, a[j].first * (dr - 1));
              rid = j;
            }
          }
          if (lmx > rmx) {
            left = 1;
          } else {
            left = 0;
          }
        } else
          left = 0;
      }
    }
    seen[id] = true;
    ans += mx;
    if (left) {
      debug[l] = a[id].first;
      l++;
    } else {
      debug[r] = a[id].first;
      r--;
    }
  }
  cout << ans << endl;
  for (long long i = (0); i < (long long)(n); i++) cerr << debug[i] << " ";
  cerr << endl;
  return 0;
}
