#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
int k[MAXN];
long long a[MAXN];
int id[MAXN];
int n;
long long ans;
bool cmp(int x, int y) { return a[x] > a[y]; }
void go() {
  for (int i = 1; i <= n; i++) {
    int cur = id[i];
    for (int j = 1; j <= n; j++) {
      long long w = a[cur] * abs(cur - k[j]) + a[j] * abs(j - k[cur]) -
                    a[cur] * abs(cur - k[cur]) - a[j] * abs(j - k[j]);
      if (w > 0) {
        ans += w;
        swap(k[cur], k[j]);
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    k[i] = i;
    id[i] = i;
  }
  sort(id + 1, id + 1 + n, cmp);
  long long w = ans;
  while (true) {
    go();
    if (ans == w) {
      break;
    }
    w = ans;
  }
  printf("%lld", ans);
  return 0;
}
