#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int inf = 1e9 + 10;
const long long INF = 1e18;
const long double EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(25);
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  long long left = 0, right = n - 1;
  long long ans = 0, t = 0;
  vector<bool> used(n, false);
  for (int i = 0; i < (n); ++i) {
    long long idx = (t % 2 == 0 ? left : right), residx = 0;
    long long res = -1;
    for (int j = 0; j < (n); ++j) {
      if (used[j]) continue;
      if (chmax(res, a[j] * abs(j - idx))) {
        residx = j;
      }
    }
    ans += res;
    used[residx] = true;
    long long ridx = (t % 2 == 1 ? left : right);
    if (abs(residx - idx) == abs(residx - ridx)) {
      long long nxtidx = 0, nxtres = -1;
      for (int j = 0; j < (n); ++j) {
        if (used[j] || j == residx) continue;
        if (chmax(nxtres, a[j] * abs(j - idx))) {
          nxtidx = j;
        }
      }
      if (abs(nxtidx - idx) < abs(nxtidx - ridx)) {
        t ^= 1;
        swap(idx, ridx);
      }
    }
    if (t % 2 == 0)
      left++;
    else
      right--;
    t ^= 1;
  }
  cout << ans << '\n';
  return 0;
}
