#include <bits/stdc++.h>
using namespace std;
long long arr[666666];
long long l, r, n;
struct str {
  long long num, id;
} st[666666];
bool cmp(str a, str b) {
  return a.num * max(abs(a.id - l), abs(a.id - r)) >
         b.num * max(abs(b.id - l), abs(b.id - r));
}
signed main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &arr[i]);
    st[i].num = arr[i];
    st[i].id = i;
  }
  l = 1, r = n;
  long long cnt = 1;
  long long ans = 0;
  while (l <= r) {
    sort(st + cnt, st + 1 + n, cmp);
    if (abs(l - st[cnt].id) >= abs(r - st[cnt].id)) {
      ans += st[cnt].num * abs(l - st[cnt].id);
      l++;
    } else {
      ans += st[cnt].num * abs(r - st[cnt].id);
      r--;
    }
    cnt++;
  }
  cout << ans;
  return 0;
}
