#include <bits/stdc++.h>
using namespace std;
long long n, m, cnt, ans, first, last, sum;
struct node {
  long long d, id, e;
} a[2000100];
bool cmp(node x, node y) { return x.d > y.d; }
int main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++)
    scanf("%lld", &a[i].d), a[i].id = i, sum += a[i].d;
  long long sum1 = 0, sum2 = 0;
  for (long long i = 1; i <= n; i++) {
    if (sum1 + a[i].d <= (sum + 1) / 2) {
      sum1 += a[i].d;
      a[i].e = 1;
    }
  }
  sort(a + 1, a + n + 1, cmp);
  first = 1;
  last = n;
  for (long long i = 1; i <= n; i++) {
    if (a[i].e == 1) {
      ans += abs(last - a[i].id) * a[i].d;
      last--;
    } else
      ans += abs(first - a[i].id) * a[i].d, first++;
  }
  printf("%lld\n", ans);
  return 0;
}
