#include <bits/stdc++.h>
using namespace std;
long long n, m, cnt, ans, first, last;
struct node {
  long long d, id;
} 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;
  sort(a + 1, a + n + 1, cmp);
  first = 1;
  last = n;
  for (long long i = 1; i <= n; i++) {
    if (last == first) {
      ans += abs(first - a[i].id) * a[i].d;
      break;
    }
    if (abs(first - a[i].id) > abs(last - a[i].id)) {
      ans += abs(first - a[i].id) * a[i].d;
      first++;
    } else if (abs(first - a[i].id) < abs(last - a[i].id)) {
      ans += abs(last - a[i].id) * a[i].d;
      last--;
    } else {
      ans += abs(first - a[i].id) * a[i].d;
      if (abs(first - a[i + 1].id) >= abs(first - a[i + 1].id))
        last--;
      else
        first++;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
