#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 2e6 + 10;
const double eps = 1e-1;
long long n;
long long l, r;
long long max(long long x, long long y) { return x > y ? x : y; }
struct Node {
  long long x, id;
  bool operator<(const Node &p) const {
    return x * max(id - l, r - id) > p.x * max(p.id - l, r - p.id);
  }
} a[maxn];
int main() {
  long long x;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i].x);
    a[i].id = i;
  }
  l = 1, r = n;
  long long ans = 0, now = 0;
  for (long long i = 1; i <= n; i++) {
    sort(a + i, a + 1 + n);
    if (a[i].id - l > r - a[i].id) {
      ans += abs(a[i].id - l) * a[i].x;
      l++;
      if (now) {
        r--;
        now = 0;
      }
    } else if (a[i].id - l < r - a[i].id) {
      ans += abs(r - a[i].id) * a[i].x;
      r--;
      if (now) {
        l++;
        now = 0;
      }
    } else {
      ans += abs(a[i].id - l) * a[i].x, now++;
      if (now == 2) {
        l++;
        r--;
        now = 0;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
