#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>;
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll total = 0;
  ll left = 0;
  ll right = N - 1;
  vector<bool> done(N, false);
  for (ll turn = 0; turn < N; turn++) {
    bool maxleft = true;
    ll son_left = 0;
    ll son_right = 0;
    ll maxval = -1;
    ll maxj = 0;
    for (ll j = 0; j < N; j++) {
      if (done[j]) {
        continue;
      }
      ll v = A[j] * abs(j - right) - son_left;
      if (maxval <= v) {
        maxval = v;
        maxj = j;
        maxleft = false;
      }
      son_left += A[j];
    }
    for (ll j = N - 1; j >= 0; j--) {
      if (done[j]) {
        continue;
      }
      ll v = A[j] * abs(j - left) - son_right;
      if (maxval <= v) {
        maxval = v;
        maxj = j;
        maxleft = true;
      }
      son_right += A[j];
    }
    if (maxleft) {
      total += A[maxj] * abs(maxj - left);
      left++;
    } else {
      total += A[maxj] * abs(maxj - right);
      right--;
    }
    done[maxj] = true;
  }
  print(total);
}
