#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<long long> L(N + 1, 0);
  priority_queue<pair<int, int>> Q;
  bool flg;
  flg = true;
  for (int i = 0; i < N; i++) {
    Q.push(make_pair(A[N - i - 1], N - i - 1));
    priority_queue<pair<int, int>> Q2 = Q;
    int count = 0;
    while (!Q2.empty()) {
      if (Q2.top().second - count < 0) {
        flg = false;
        break;
      }
      L[i + 1] += Q2.top().first * (Q2.top().second - count);
      count++;
      Q2.pop();
    }
    if (!flg) {
      break;
    }
  }
  vector<long long> R(N + 1, 0);
  while (!Q.empty()) {
    Q.pop();
  }
  flg = true;
  for (int i = 0; i < N; i++) {
    Q.push(make_pair(A[i], i));
    priority_queue<pair<int, int>> Q2 = Q;
    int count = N - 1;
    while (!Q2.empty()) {
      if (count - Q2.top().second < 0) {
        flg = false;
        break;
      }
      R[N - 1 - i] += Q2.top().first * (count - Q2.top().second);
      count--;
      Q2.pop();
    }
    if (!flg) {
      break;
    }
  }
  long long ans = 0;
  for (int i = 0; i <= N; i++) {
    ans = max(ans, L[i] + R[i]);
  }
  cout << ans << endl;
}
