#include <bits/stdc++.h>
using namespace std;
int n;
int a[2005];
long long f[2005][2005];
long long dp[2005];
int h[2005], l[2005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) f[i][j] = 1ll * a[i] * abs(i - j);
  }
  for (int i = 1; i <= n; i++) dp[i] = h[i] = l[i] = 0;
  h[1] = 1, l[1] = 1;
  for (int i = 2; i <= n; i++) {
    int nowl = 0, nowh = 0;
    for (int j = 1; j < i; j++) {
      if (dp[i - 1] + f[l[j]][i] - f[l[j]][j] + f[i][j] > dp[i]) {
        dp[i] = dp[i - 1] + f[l[j]][i] - f[l[j]][j] + f[i][j];
        nowl = j;
      }
    }
    l[i] = l[nowl];
    h[l[nowl]] = i;
    l[nowl] = i;
    h[i] = nowl;
    for (int j = 1; j < i; j++) {
      if (h[j] == i) continue;
      if (dp[i] - f[l[i]][i] + f[l[i]][h[j]] + f[j][i] - f[j][h[j]] > dp[i]) {
        dp[i] = dp[i] - f[l[i]][i] + f[l[i]][h[j]] + f[j][i] - f[j][h[j]];
        nowh = j;
      }
    }
    if (nowh == 0) continue;
    h[i] = h[nowh];
    l[h[nowh]] = i;
    h[nowh] = i;
    l[i] = nowh;
  }
  printf("%lld\n", dp[n]);
  return 0;
}
