// IOI 2021
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
 
////////////////////////////////////////////////////////////////////

const int N = 2e3 + 5;

int A[N], P[N];
ll ans, DP[N][N];

bool CMP(int i, int j) { return A[P[i]] > A[P[j]]; }

int main() {

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	mt19937 Rnd(time(0));

	int n; cin >> n;
	for (int i = 0; i < n; i++) cin >> A[i], P[i] = i;
	sort(P, P + n, CMP);
	for (int x = 0; x <= n; x++) for (int y = 0; x + y <= n; y++) {
		if (!x && !y) continue;
		int i = x + y - 1;
		if (x) DP[x][y] = max(DP[x][y], DP[x - 1][y] + 1LL * A[P[i]] * ((P[i] + 1) - x));
		if (y) DP[x][y] = max(DP[x][y], DP[x][y - 1] + 1LL * A[P[i]] * ((n - y + 1) - (P[i] + 1)));
		if (x + y == n) ans = max(ans, DP[x][y]);
	}
	cout << ans << endl;

	return 0;
}
