#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1000000007;
ll n_pow(ll a, ll n) {
  if (n == 0) return 1;
  ll t = n_pow(a, n / 2);
  t *= t;
  t %= mod;
  if (n % 2 == 1) t *= a;
  return t % mod;
}
int main() {
  int n;
  cin >> n;
  vector<pair<ll, int>> a(n);
  vector<bool> b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
    b[i] = true;
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll pos = a[i].second;
    ll max = 0;
    for (ll j = 0; j < n; j++) {
      int tmp = abs(pos - j);
      if (tmp > abs(pos - max) && b[j]) {
        max = j;
      }
    }
    b[max] = false;
    ans += a[i].first * abs(max - pos);
  }
  cout << ans - 2 << endl;
  return 0;
}
