#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vb = vector<bool>;
ll sa(long long a, long long b) {
  if (a > b) return a - b;
  return b - a;
}
int main() {
  ll n;
  cin >> n;
  vll a(n);
  for (auto &i : a) cin >> i;
  vb c(n, true);
  ll l = 0, r = n - 1;
  ll ans = 0;
  while (l <= r) {
    ll R = 0, L = 0;
    ll posl, posr;
    posl = posr = 0;
    for (int i = 0; i < n; i++) {
      if (!c.at(i)) continue;
      if (R < a.at(i) * sa(i, r)) {
        R = a.at(i) * sa(i, r);
        posr = i;
      }
      if (L < a.at(i) * sa(i, l)) {
        L = a.at(i) * sa(i, l);
        posl = i;
      }
    }
    if (R >= L) {
      ans += R;
      c.at(posr) = false;
      r--;
    } else {
      ans += L;
      c.at(posl) = false;
      l++;
    }
  }
  cout << ans << endl;
}
