/*
    JAI JAGANNATH!
*/
//@Author : zanj0
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
#define ff              first
#define ss              second
#define pb              push_back
#define mp              make_pair
#define pii             pair<lli,lli>
#define vi              vector<lli>
#define mii             map<lli,lli>
#define pqb             priority_queue<lli>
#define pqs             priority_queue<lli,vi,greater<lli> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            lli x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update> pbds;


void zanj0()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
bool cmp(pair <lli, lli>& a, pair <lli, lli>& b) {
	return a.ff > b.ff;
}
void out(vector < pair <lli, lli> >& v) {
	for (auto& x : v) {
		cout << x.first << " " << x.second << endl;
	}
}
void solve() {
	lli n;
	cin >> n;
	vector < pair <lli, lli> > v(n + 1);
	for (lli i = 1; i <= n; i++) {
		cin >> v[i].ff;
		v[i].ss = i;
	}
	sort(v.begin() + 1, v.end(), cmp);
	vector < vector <lli> >dp (n + 1, vector <lli>(n + 1));
	for (lli i = 1; i <= n; i++) {
		for (lli j = 0; j < i; j++) {
			lli l = j;
			lli r = i - 1 - j;
			dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + v[i].ff * (abs(l + 1 - v[i].ss)));
			r = j;
			l = i - 1 - j;
			dp[l][r + 1] = max(dp[i][r + 1], dp[l][r] + abs(n - r - v[i].ss) * v[i].ff);
		}
	}
	lli ret = 0;
	for (lli i = 0; i <= n; i++) {
		ret = max(ret, dp[i][n - i]);
	}
	cout << ret << endl;
}
int main()
{
	zanj0();
	solve();
	return 0;
}