#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;
void print() { cout << endl; }
template <typename Head, typename... Tail>
void print(Head head, Tail... tail) {
  cout << head;
  if (sizeof...(Tail) > 0) cout << " ";
  print(tail...);
}
void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
  cout << head;
  print0(tail...);
}
using pll = pair<ll, ll>;
void recursive(ll i, ll total, ll left, ll right, ll &current,
               vector<ll> &idealsum, vector<ll> &A, vector<bool> &done) {
  ll N = A.size();
  if (current < total) {
    current = total;
  }
  if (i == N) {
    return;
  }
  if (total + idealsum[i] <= current) {
    return;
  }
  for (ll j = 0; j < N; j++) {
    if (!done[j]) {
      done[j] = true;
      recursive(i + 1, total + A[i] * abs(i - right), left, right - 1, current,
                idealsum, A, done);
      done[j] = false;
      break;
    }
  }
  for (ll j = N; j >= 0; j--) {
    if (!done[j]) {
      done[j] = true;
      recursive(i + 1, total + A[i] * abs(i - left), left + 1, right, current,
                idealsum, A, done);
      done[j] = false;
      break;
    }
  }
  bool isleft = true;
  ll maxval = -1;
  ll maxj = -1;
  for (ll j = 0; j < N; j++) {
    if (done[j]) {
      continue;
    }
    ll lval = A[i] * abs(i - left);
    ll rval = A[i] * abs(i - right);
    if (maxval < lval) {
      maxval = lval;
      maxj = j;
      isleft = true;
    }
    if (maxval < rval) {
      maxval = rval;
      maxj = j;
      isleft = false;
    }
  }
  done[maxj] = true;
  if (isleft) {
    recursive(i + 1, total + maxval, left + 1, right, current, idealsum, A,
              done);
  } else {
    recursive(i + 1, total + maxval, left, right - 1, current, idealsum, A,
              done);
  }
  done[maxj] = false;
}
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll current = 0;
  {
    ll heuri = 0;
    vector<pll> ca(N);
    for (ll i = 0; i < N; i++) {
      ca[i] = make_pair(i, A[i]);
    }
    sort(ca.begin(), ca.end(),
         [](pll x, pll y) { return x.second > y.second; });
    vector<bool> destdone(N, false);
    for (ll i = 0; i < N; i++) {
      pll x = ca[i];
      ll maxj = x.first;
      for (ll j = 0; j < N; j++) {
        if (!destdone[j]) {
          if (abs(x.first - j) > abs(x.first - maxj)) {
            maxj = j;
          }
        }
      }
      destdone[maxj] = true;
      heuri += x.second * abs(x.first - maxj);
    }
    current = heuri;
  }
  vector<ll> ideal(N, 0);
  {
    for (ll i = 0; i < N; i++) {
      ideal[i] = A[i] * max(abs(i - 0), abs(N - 1 - i));
    }
  }
  vector<ll> idealsum(N, 0);
  {
    ll itotal = 0;
    for (ll i = 0; i < N; i++) {
      itotal += ideal[i];
    }
    for (ll i = 0; i < N; i++) {
      idealsum[i] = itotal;
      itotal -= ideal[i];
    }
  }
  {
    vector<bool> done(N, false);
    recursive(0, 0, 0, N - 1, current, idealsum, A, done);
  }
  print(current);
}
