#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define Rep(i,n) for(int i=1;i<=(n);++i)
using LL = long long;
using ULL = unsigned long long;

LL dp[2001][2001];

int main()
{
	int n, a;
	cin >> n;
	vector<pair<int, int>> x;
	Rep(i, n) cin >> a, x.emplace_back(a, i);
	sort(x.begin(), x.end(), greater<pair<LL, int>>());
	Rep(i, n) {
		LL val = x[i-1].first;
		int pos = x[i-1].second;
		dp[i][0] = dp[i-1][0] + abs(n-i+1-pos) * val;
		for(int j = 1; j < i; ++j) dp[i][j] = max(dp[i-1][j-1] + abs(j-pos)*val, dp[i-1][j] + abs(n-i+j+1-pos)*val);
		dp[i][i] = dp[i-1][i-1] + abs(i-pos) * val;
	}
	LL ans = 0;
	for(int i = 0; i <= n; ++i) ans = max(ans, dp[n][i]);
	cout << ans << endl;
	return 0;
}
