#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<int, vector<int> > z;
  int i, j;
  for (i = 0; i < n; i++) {
    int tmp;
    scanf("%d", &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) {
    int 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> s(num + 1);
      for (j = 0; j < num; j++) {
        s[j + 1] = s[j] + v[j];
      }
      int jlim0 = num + 1;
      for (j = 1; j <= num; j++) {
        if (i > v[num - 1 - (j - 1)]) {
          jlim0 = j;
          break;
        }
      }
      int jlim1 = -1;
      for (j = num - 1; j >= 0; j--) {
        if (n - 1 - (cnt - i) < v[j]) {
          jlim1 = j;
          break;
        }
      }
      for (j = 0; j <= num; j++) {
        if (j >= jlim0) continue;
        if (j <= jlim1) continue;
        long long tmp0 = s[num] - s[num - j] - (i + (i + j - 1)) * j / 2;
        long long tmp1 =
            -s[num - j] +
            ((n - 1 - (cnt - i)) + (n - 1 - (cnt - i)) - (allnum - j - 1)) *
                (allnum - j) / 2;
        dp[cnt + 1][i + j] =
            ((dp[cnt + 1][i + j]) < (dp[cnt][i] + (tmp0 + tmp1) * val)
                 ? (dp[cnt][i] + (tmp0 + tmp1) * 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;
}
