#include <bits/stdc++.h>
using namespace std;
long long mabs(long long a) { return a > 0 ? a : -a; }
int main(void) {
  long long i, j, k, n, ans = 0, cnt = 0, now;
  scanf("%lld", &n);
  bool flg[n];
  pair<long long, long long> p[n];
  for (i = 0; i < n; ++i) scanf("%lld", &p[i].first), p[i].second = i;
  sort(p, p + n);
  for (i = 0; i < n; ++i) ans += mabs(p[i].second - i) * p[i].first;
  for (k = n - 1; k >= 0; --k) {
    now = (mabs(cnt - p[k].second) - mabs(k + cnt - p[k].second)) * p[k].first;
    for (i = 0; i < k; ++i) {
      if (i + cnt < p[i].second)
        now -= p[i].first;
      else
        now += p[i].first;
    }
    if (now > 0) cnt++, ans += now;
  }
  printf("%lld", ans);
  return 0;
}
