#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;
  }
  long long ans = 0;
  int last_i = N - 2;
  int last_j = N - 1;
  bool end = false;
  while (!end) {
    for (int i = 0; i < N; i++) {
      if (end) {
        break;
      }
      for (int j = 0; j < N; j++) {
        if (i == j) {
          continue;
        }
        long long move_result = A[i].first * ((int)abs(j - A[i].second) -
                                              (int)abs(i - A[i].second));
        move_result += A[j].first *
                       ((int)abs(i - A[j].second) - (int)abs(j - A[j].second));
        if (move_result > 0) {
          std::swap(A[i], A[j]);
          ans += move_result;
          last_i = i;
          last_j = j;
        } else {
          if (i == last_i && j == last_j) {
            end = true;
            break;
          }
        }
      }
    }
  }
  long long max = ans;
  for (int i = 0; i < N; i++) {
    long long move_result = 0;
    for (int j = 0; j < N - 1; j++) {
      move_result += A[j].first * ((int)abs(j + 1 - A[j].second) -
                                   (int)abs(j - A[j].second));
      move_result += A[j + 1].first * ((int)abs(j - A[j + 1].second) -
                                       (int)abs(j + 1 - A[j + 1].second));
      std::swap(A[j], A[j + 1]);
    }
    ans += move_result;
    if (max < ans) {
      max = ans;
    }
  }
  std::cout << max << std::endl;
  return 0;
}
