#include <bits/stdc++.h>
#pragma warning(disable : 4996)
const long long MOD = 1000000007;
using namespace std;
long long dp[2005][2005];
void solve() {
  int n;
  scanf("%d", &n);
  map<long long, vector<int> > z;
  int i, j;
  for (i = 0; i < n; i++) {
    long long tmp;
    scanf("%lld", &tmp);
    z[tmp].push_back(i);
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++) {
      dp[i][j] = -9223300000000000000;
    }
  }
  dp[0][0] = 0;
  int cnt = 0;
  int allnum = 0;
  auto it = z.rbegin();
  for (; it != z.rend(); ++it) {
    long long val = it->first;
    vector<int>& v = it->second;
    int num = (int)v.size();
    for (i = 0; i <= n; i++) {
      if (dp[cnt][i] == -9223300000000000000) continue;
      vector<long long> v0, v1;
      for (j = 0; j < num; j++) {
        v0.push_back(v[j] - (i + j));
        v1.push_back(-v[num - 1 - j] + (n - 1 - (allnum - i) - j));
      }
      vector<long long> sv0(num + 1), sv1(num + 1);
      for (j = 0; j < num; j++) {
        sv0[j + 1] = sv0[j] + v0[j];
        sv1[j + 1] = sv1[j] + v1[j];
      }
      for (j = 0; j <= num; j++) {
        dp[cnt + 1][i + j] =
            ((dp[cnt + 1][i + j]) < (dp[cnt][i] + (sv0[j] + sv1[num - j]) * val)
                 ? (dp[cnt][i] + (sv0[j] + sv1[num - j]) * val)
                 : (dp[cnt + 1][i + j]));
      }
    }
    allnum += num;
    cnt++;
  }
  long long ans = 0;
  for (i = 0; i <= n; i++) {
    ans = ((ans) < (dp[cnt][i]) ? (dp[cnt][i]) : (ans));
  }
  printf("%lld\n", ans);
  return;
}
int main(int argc, char* argv[]) {
  solve();
  return 0;
}
