#include <bits/stdc++.h>
using namespace std;
long long dp[2020][2020];
int main() {
	int n; cin >> n;
	int a[n]; for (int &i : a) cin >> i;
	int p[n]; iota(p, p + n, 0);
	sort(p, p + n, [&] (int x, int y) {
		return a[x] > a[y];
	});
	for (int l = 0; l < n; l++) {
		for (int r = 0; l + r < n; r++) {
			int now = l + r;
			dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + 1LL * a[p[now]] * abs(p[now] - l));
			dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + 1LL * a[p[now]] * abs(p[now] - (n - 1 - r)));
		}
	}
	long long ans = 0;
	for (int i = 0; i <= n; i++) 
		ans = max(ans, dp[i][n - i]);
	cout << ans;
}