#include "bits/stdc++.h"
using namespace std;

int main() {
	int N;
	long long INF = 1000000000000000000;
	cin >> N;
	vector<long long> A(N);
	vector<pair<long long, int> > VP(N);
	for (int i = 0; i < N; i++) cin >> A[i], VP[i] = { A[i], i };
	sort(VP.begin(), VP.end(), greater<pair<long long, int> >());
	vector<vector<long long> > DP(N + 1, vector<long long>(N + 1, -INF));
	DP[0][N] = 0;
	for (int i = 0; i < N; i++) {
		for (int L = 0; L <= i; L++) {
			int R = N - i + L;
			DP[L + 1][R] = max(DP[L + 1][R], DP[L][R] + VP[i].first * abs(VP[i].second - L));
			DP[L][R - 1] = max(DP[L][R - 1], DP[L][R] + VP[i].first * abs(VP[i].second - (R - 1)));
		}
	}
	long long ANS = -INF;
	for (int i = 0; i <= N; i++) ANS = max(ANS, DP[i][i]);
	cout << ANS << endl;
}