#include <bits/stdc++.h>
template <class T>
inline T chmax(T& a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T>
inline T chmin(T& a, const T b) {
  return a = (a > b) ? b : a;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = acos(-1);
using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;
template <typename T>
struct bit {
  vector<T> data;
  bit(ll sz) { data.assign(++sz, 0); }
  bit(ll sz, ll v) {
    data.assign(++sz, v);
    for (ll i = 0; i < ll(sz); ++i) data[i + (i & -i)] += data[i];
  }
  T sum(ll k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k) ret += data[k];
    return ret;
  }
  void add(ll k, T x) {
    for (++k; k < data.size(); k += k & -k) data[k] += x;
  }
};
signed main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < ll(N); ++i) cin >> A[i];
  bit<ll> bit(N);
  for (ll i = 0; i < ll(N); ++i) bit.add(i, A[i]);
  vector<bool> used(N, false);
  ll left = 0, right = N - 1;
  ll ans = 0;
  bool mitei = true;
  for (ll i = 0; i < ll(N); ++i) {
    pair<ll, ll> MAX(0, 0);
    for (ll j = 0; j < ll(N); ++j) {
      if (used[j]) continue;
      if (MAX.first < A[j]) MAX = {A[j], j};
    }
    used[MAX.second] = true;
    ll l = abs(MAX.second - left);
    ll r = abs(right - MAX.second);
    ll lll = bit.sum(MAX.second - 1);
    ll rrr = bit.sum(N - 1) - bit.sum(MAX.second);
    if (max(lll, rrr) <= MAX.first) {
      if (l > r) {
        ans += l * MAX.first;
        left++;
      } else {
        ans += r * MAX.first;
        right--;
      }
    } else {
      if (lll < rrr) {
        ans += r * MAX.first;
        right--;
      } else {
        ans += l * MAX.first;
        left++;
      }
    }
    bit.add(MAX.second, -MAX.first);
  }
  cout << ans << "\n";
  return 0;
}
