#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B>
inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
ll dp[2][2001];
map<ll, vector<ll>> mm;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = (0); i < (N); ++i) cin >> A[i];
  vector<pii> v(N);
  for (int i = (0); i < (N); ++i) v[i] = pii(A[i], i);
  ll ans = 0;
  while (true) {
    bool update = false;
    for (int i = (0); i < (N - 1); ++i) {
      ll bc = (ll)v[i].first * abs(v[i].second - i) +
              (ll)v[i + 1].first * abs(v[i + 1].second - i - 1);
      swap(v[i], v[i + 1]);
      ll ac = (ll)v[i].first * abs(v[i].second - i) +
              (ll)v[i + 1].first * abs(v[i + 1].second - i - 1);
      if (bc < ac) {
        ans += ac - bc;
        update = true;
      } else
        swap(v[i], v[i + 1]);
    }
    if (!update) break;
  }
  cout << ans << endl;
  return 0;
}
