#include <bits/stdc++.h>
using namespace std;
long long int INF = 1e18;
long long int MOD = 1e9 + 7;
long long int A[2100];
long long int memo[2][2100];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int loop = 0; loop < 2; loop++) {
    long long int B1[2100];
    long long int S = 0;
    for (int i = 0; i < N; i++) {
      B1[i] = A[i];
      S += (N - 1 - i - i) * A[i];
      for (int j = i; j >= 1; j--) {
        if (B1[j] > B1[j - 1]) {
          S += B1[j] - B1[j - 1];
          swap(B1[j], B1[j - 1]);
        }
      }
      memo[loop][i] = S;
    }
    reverse(A, A + N);
  }
  long long int ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans = max(ans, memo[0][i] + memo[1][N - 2 - i]);
  }
  cout << ans << endl;
  return 0;
}
