#include <bits/stdc++.h>
void swap(int *a, int *b) {
  int temp = *a;
  *a = *b, *b = temp;
}
void QuickSort(int A[], int B[], int left, int right) {
  int Left = left, Right = right, pivot = A[(left + right) / 2];
  while (1) {
    while (A[Left] < pivot) Left++;
    while (pivot < A[Right]) Right--;
    if (Left >= Right) break;
    swap(&A[Left], &A[Right]);
    swap(&B[Left], &B[Right]);
    Left++, Right--;
  }
  if (left < Left - 1) QuickSort(A, B, left, Left - 1);
  if (Right + 1 < right) QuickSort(A, B, Right + 1, right);
}
int main(void) {
  int n, k, i, cou = 0, tmp;
  long long mod = 1000000007, happy = 0, dpl, dpr, dpmax = 0;
  scanf(" %d", &n);
  int a[n], ax[n], b[n];
  long long dp[n + 1][n + 1];
  for (i = 0; i < n; i++) {
    scanf(" %d", &a[i]);
    ax[i] = a[i], b[i] = i;
  }
  QuickSort(a, b, 0, n - 1);
  dp[1][0] = a[n - 1] * b[n - 1], dp[0][1] = a[n - 1] * (n - 1 - b[n - 1]);
  for (i = 2; i <= n; i++) {
    for (k = 0; k <= i; k++) {
      if (k == 0)
        dp[i][0] = dp[i - 1][0] + abs(i - b[n - i] - 1) * a[n - i];
      else if (k == i)
        dp[0][i] = dp[0][i - 1] + abs(n - i - b[n - i]) * a[n - i];
      else {
        dpl = dp[i - k - 1][k] + abs(i - k - 1 - b[n - i]) * a[n - i];
        dpr = dp[i - k][k - 1] + abs(n - k - b[n - i]) * a[n - i];
        if (dpl > dpr)
          dp[i - k][k] = dpl;
        else
          dp[i - k][k] = dpr;
      }
    }
  }
  for (i = 0; i <= n; i++)
    if (dpmax < dp[i][n - i]) dpmax = dp[i][n - i];
  printf("%lld", dpmax);
  return 0;
}
