#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::pair<int, int> > test(N);
  int index_left = 0;
  int index_right = N - 1;
  for (int i = N - 1; i >= 0; i--) {
    if (A[i].second - index_left > index_right - A[i].second) {
      test[index_left] = A[i];
      index_left++;
    } else {
      test[index_right] = A[i];
      index_right--;
    }
  }
  long long max = 0;
  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[j].first * (test[j].second - place) *
             ((test[j].second > place) * 2 - 1);
    }
    max = std::max(max, tmp);
  }
  std::cout << max << std::endl;
  return 0;
}
