#include <bits/stdc++.h>
using namespace std;
using int32 = int32_t;
using str = string;
using cstr = const str;
using ll = long long;
using ull = unsigned long long;
using cint = const ll;
using cll = const ll;
using vi = vector<ll>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using pii = pair<ll, ll>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using psl = pair<string, ll>;
using tiii = tuple<ll, ll, ll>;
using tlll = tuple<ll, ll, ll>;
static const ll MGN = 10;
static const ll ARYSZ_MAX = (ll)1e7;
static const ll LINF = LLONG_MAX / 2;
static const ll INF = LINF;
static double EPS = 1e-9;
static inline bool DBLEQ(double a, double b) { return abs(a - b) < EPS; }
static inline void EPR(str msg) { cerr << msg << endl; }
static inline void AST(bool exp, str msg) {
  if (!exp) {
    EPR(msg);
  }
  assert(exp);
}
static inline void TAST(bool exp, str msg) {
  if (!exp) {
    EPR(msg);
  }
  while (!exp) {
  }
}
int32 main(int32 argc, char* argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ;
  ;
  ll N;
  cin >> N;
  vi A(N);
  for (ll i = ll(0); i < ll(N); ++i) cin >> A[i];
  priority_queue<tiii> pql, pqr;
  for (ll i = ll(0); i < ll(N); ++i)
    if (A[i] * (i - 0) >= A[i] * (N - 1 - i))
      pql.push(make_tuple(A[i] * (i - 0), (i - 0), i));
    else
      pqr.push(make_tuple(A[i] * (N - 1 - i), (N - 1 - i), i));
  vb fixed(N, false);
  ll ans = 0;
  while (true) {
    ll vl = not pql.empty() ? get<(0)>(pql.top()) : -1;
    ll vr = not pqr.empty() ? get<(0)>(pqr.top()) : -1;
    if (vl < 0 and vr < 0) break;
    if (vl >= vr) {
      auto tl = pql.top();
      pql.pop();
      ll x = get<(2)>(tl);
      ll y = x;
      for (ll i = ll(0); i < ll(N); ++i) {
        if (not fixed[i] and abs(i - x) > abs(y - x)) {
          y = i;
        }
      }
      ans += A[x] * abs(x - y);
      fixed[y] = true;
      ll l = -1;
      for (ll i = ll(0); i < ll(N); ++i) {
        if (not fixed[i]) {
          l = i;
          break;
        }
      }
      if (l < 0) break;
      ll r = N - 1;
      for (ll i = N - 1; i >= 0; i--) {
        if (not fixed[i]) {
          r = i;
          break;
        }
      }
      vector<tiii> tmp;
      while (not pql.empty()) {
        auto t = pql.top();
        pql.pop();
        ll x = get<(2)>(t);
        if (A[x] * abs(l - x) >= A[x] * abs(r - x))
          tmp.push_back(make_tuple(A[x] * abs(l - x), l - x, x));
        else
          pqr.push(make_tuple(A[x] * abs(r - x), r - x, x));
      }
      for (auto e : tmp) pql.push(e);
    } else {
      auto tr = pqr.top();
      pqr.pop();
      ll x = get<(2)>(tr);
      ll y = x;
      for (ll i = N - 1; i >= 0; i--) {
        if (not fixed[i] and abs(i - x) > abs(y - x)) {
          y = i;
        }
      }
      ans += A[x] * abs(x - y);
      fixed[y] = true;
      ll l = -1;
      for (ll i = ll(0); i < ll(N); ++i) {
        if (not fixed[i]) {
          l = i;
          break;
        }
      }
      if (l < 0) break;
      ll r = N - 1;
      for (ll i = N - 1; i >= 0; i--) {
        if (not fixed[i]) {
          r = i;
          break;
        }
      }
      vector<tiii> tmp;
      while (not pqr.empty()) {
        auto t = pqr.top();
        pqr.pop();
        ll x = get<(2)>(t);
        if (A[x] * abs(r - x) >= A[x] * abs(l - x))
          tmp.push_back(make_tuple(A[x] * abs(r - x), r - x, x));
        else
          pql.push(make_tuple(A[x] * abs(l - x), l - x, x));
      }
      for (auto e : tmp) pqr.push(e);
    }
  }
  cout << ans << endl;
  return 0;
}
