#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int INF = 1e9;
const int MOD = INF + 7;
int main() {
  int N;
  cin >> N;
  priority_queue<pair<int, int>> que, que2;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    que.push({A[i], i});
  }
  que2 = que;
  int l = 0, r = N - 1;
  ll ans = 0, tmp = 0;
  for (int i = 0; i < N; ++i) {
    pair<int, int> now = que.top();
    que.pop();
    if (abs(r - now.second) < abs(l - now.second)) {
      if (i != 0) {
        tmp += now.first * abs(l - now.second);
        ++l;
      } else {
        tmp += now.first * abs(r - now.second);
        --r;
      }
    } else {
      if (i != 0) {
        tmp += now.first * abs(r - now.second);
        --r;
      } else {
        tmp += now.first * abs(l - now.second);
        ++l;
      }
    }
  }
  ans = tmp;
  tmp = 0;
  l = 0, r = N - 1;
  que = que2;
  for (int i = 0; i < N; ++i) {
    pair<int, int> now = que.top();
    que.pop();
    if (abs(r - now.second) < abs(l - now.second)) {
      tmp += now.first * abs(l - now.second);
      ++l;
    } else {
      tmp += now.first * abs(r - now.second);
      --r;
    }
  }
  cout << max(ans, tmp) << endl;
  return 0;
}
