#include <bits/stdc++.h>
using namespace std;
long long int arr[200005];
long long int n;
long long int dp[3000][3000];
map<long long int, long long int> mp;
bool compare(long long int a, long long int b) { return a >= b; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
    mp[arr[i]] = i + 1;
  }
  sort(arr, arr + n, compare);
  dp[0][0] = 0;
  for (long long int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] + arr[i - 1] * (abs(mp[arr[i - 1]] - (i)));
  }
  for (long long int i = 1; i <= n; i++) {
    dp[0][i] = dp[0][i - 1] + arr[i - 1] * (abs(mp[arr[i - 1]] - (n - i + 1)));
  }
  long long int candi1, candi2;
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j + i <= n; j++) {
      candi1 = dp[i - 1][j] + arr[i + j - 1] * abs(mp[arr[i + j - 1]] - i);
      candi2 =
          dp[i][j - 1] + arr[i + j - 1] * abs(mp[arr[i + j - 1]] - (n - i + 1));
      dp[i][j] = max(candi1, candi2);
    }
  }
  long long int ans = LONG_LONG_MIN;
  for (long long int j = 0; j <= n; j++) {
    ans = max(ans, dp[j][n - j]);
  }
  cout << ans << endl;
}
