#include <bits/stdc++.h>

#define pp push_back
#define pf push_front
#define mp make_pair
#define fs first
#define sc second
#define sf scanf

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

const int N = (int) 2e3;
const int INF = (int) 1e9 + 7;

ii p[N + 7];
ll dp[N + 7][N + 7][N + 7];
int n;

ll f(int i, int l, int r) {
	if (i == 0) return 0;
	if (dp[i][l][r] != -1) return dp[i][l][r];

	ll x = f(i - 1, l + 1, r) + p[i].fs * abs(l - p[i].sc);
	ll y = f(i - 1, l, r - 1) + p[i].fs * abs(r - p[i].sc);

	return dp[i][l][r] = max(x, y);
}

int main(int argc, char *argv[])
{
	// ios_base::sync_with_stdio(false);
	// cin.tie(NULL);

	sf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		sf("%d", &p[i].fs);
		p[i].sc = i;
	}

	sort(p + 1, p + n + 1);

	memset(dp, -1, sizeof(dp));
	printf("%lld", f(n, 1, n));

	return 0;
}
