#include <bits/stdc++.h>
using namespace std;
void code() {
  long long int n;
  cin >> n;
  long long int A[n];
  vector<pair<long long int, long long int> > v;
  vector<pair<long long int, pair<long long int, long long int> > > v2;
  long long int val = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> A[i];
    v.push_back({A[i], i});
  }
  sort(v.begin(), v.end());
  long long int maxv = 0;
  for (long long int i = n - 1; i >= 0; i--) {
    val = 0;
    long long int s = 0, e = n - 1;
    if ((v[i].second - s) >= (e - v[i].second)) {
      val += (v[i].first) * (v[i].second - s);
      s++;
    } else {
      val += (v[i].first) * (e - v[i].second);
      e--;
    }
    v2.push_back({val, {v[i].first, v[i].second}});
  }
  val = 0;
  sort(v2.begin(), v2.end());
  long long int s = 0, e = n - 1;
  for (long long int i = n - 1; i >= 0; i--) {
    if ((v2[i].second.second - s) >= (e - v2[i].second.second)) {
      val += (v2[i].second.first) * (v2[i].second.second - s);
      s++;
    } else {
      val += (v2[i].second.first) * (e - v2[i].second.second);
      e--;
    }
  }
  maxv = max(maxv, val);
  cout << maxv << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) {
    code();
  }
}
