#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll t1, t2, t3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  vector<pair<ll, ll>> vec_sorted(n);
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
    vec_sorted[i] = make_pair(vec[i], i);
  }
  sort((vec_sorted).begin(), (vec_sorted).end());
  vector<ll> visited(n, 0);
  ll piv = -1;
  ll maxi;
  ll piv1 = 0;
  ll piv2 = n - 1;
  ll ans = 0;
  vector<pair<ll, ll>> vec1, vec2;
  for (int k = 0; k < n; k++) {
    ll sum1 = 0;
    ll sum2 = 0;
    t1 = vec_sorted[k].second;
    for (int i = 0; i < t1; i++) {
      if (!visited[i]) sum1 += vec[i];
    }
    for (int i = t1 + 1; i < n; i++) {
      if (!visited[i]) sum2 += vec[i];
    }
    if (sum1 > sum2) {
      vec1.emplace_back(make_pair(vec_sorted[k].first, vec_sorted[k].second));
    } else {
      vec2.emplace_back(make_pair(vec_sorted[k].first, vec_sorted[k].second));
    }
  }
  sort((vec1).begin(), (vec1).end());
  sort((vec2).begin(), (vec2).end());
  ull len1 = vec1.size();
  ull len2 = vec2.size();
  for (int i = 0; i < len1; i++) {
    ans += abs(vec1[len1 - i - 1].second - i) * vec1[len1 - i - 1].first;
  }
  for (int(i) = (int)(len2)-1; (i) >= 0; --(i)) {
    ans +=
        abs(n - 1 - i - vec2[len2 - i - 1].second) * vec2[len2 - i - 1].first;
  }
  cout << ans << endl;
}
