#include <bits/stdc++.h>
int main() {
  int N;
  std::cin >> N;
  std::vector<std::pair<int, int> > A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i].first;
    A[i].second = i;
  }
  std::sort(A.begin(), A.end());
  std::vector<std::vector<std::pair<int, int> > > test(
      1, std::vector<std::pair<int, int> >(N));
  std::vector<std::pair<int, int> > lr(1);
  lr[0].first = 0;
  lr[0].second = N - 1;
  for (int i = N - 1; i >= 0; i--) {
    for (int v = 0; v < test.size(); v++) {
      if (A[i].second - lr[v].first > lr[v].second - A[i].second) {
        test[v][lr[v].first] = A[i];
        lr[v].first++;
      } else if (A[i].second - lr[v].first < lr[v].second - A[i].second) {
        test[v][lr[v].second] = A[i];
        lr[v].second--;
      } else {
        test.push_back(test[v]);
        lr.push_back(lr[v]);
        test[v][lr[v].first] = A[i];
        lr[v].first++;
        test[test.size() - 1][lr[v].second] = A[i];
        lr[test.size() - 1].second--;
      }
    }
  }
  long long max = 0;
  for (int v = 0; v < test.size(); v++) {
    for (int i = 0; i < N; i++) {
      long long tmp = 0;
      for (int j = 0; j < N; j++) {
        int place = i + j;
        if (place >= N) {
          place -= N;
        }
        tmp += test[v][j].first * (test[v][j].second - place) *
               ((test[v][j].second > place) * 2 - 1);
      }
      max = std::max(max, tmp);
    }
  }
  std::cout << max << std::endl;
  return 0;
}
