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