#include <bits/stdc++.h>
using namespace std;
int n;
struct node {
  int pos, val;
} a[2005];
int b[2005] = {0};
long long sum = 0, ans = 0;
bool cmp(node n, node m) { return n.val > m.val; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].val), a[i].pos = i;
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    int l = 1, r = n;
    while (b[l]) l++;
    while (b[r]) r--;
    if (abs(l - a[i].pos) >= abs(r - a[i].pos))
      sum = sum + a[i].val * abs(a[i].pos - l), b[l]++;
    else
      sum = sum + a[i].val * abs(a[i].pos - r), b[r]++;
  }
  memset(b, 0, sizeof(b));
  for (int i = 1; i <= n; i++) {
    int l = 1, r = n;
    while (b[l]) l++;
    while (b[r]) r--;
    if (abs(l - a[i].pos) >= abs(r - a[i].pos))
      ans = ans + a[i].val * abs(a[i].pos - l), b[l]++;
    else
      ans = ans + a[i].val * abs(a[i].pos - r), b[r]++;
  }
  printf("%lld\n", max(ans, sum));
  return 0;
}
