#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 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;
  }
  print(current);
}
