#include <bits/stdc++.h>

#define rep(i, n) for ( int i = 0; i < (n); ++i )
using namespace std;
using ll =  long long;
using P = pair<ll, ll>;

ll n;

ll dfs( ll s, ll e, ll a[] ) {
	if ( s > e ) {
		return 0;
	}

	ll _max = 0;
	ll p = 0;
	rep( i, n ) {
		if ( _max < max(a[i] * (i - s), a[i] * (e - i)) ) {
			_max = max(a[i] * (i - s), a[i] * (e - i));
			p = i;
		}
	}

	a[p] = 0;
	if ( (p - s) > (e - p) ) {
		ll _a[n];
		memcpy(_a, a, sizeof(ll) * n);
		return _max + dfs(s + 1, e, _a);
	}
	else if ( (p - s) < (e - p) ) {
		ll _a[n];
		memcpy(_a, a, sizeof(ll) * n);
		return _max + dfs(s, e - 1, _a);
	}

	ll _a[n];
	memcpy(_a, a, sizeof(ll) * n);
	ll x = dfs(s + 1, e, _a);

	memcpy(_a, a, sizeof(ll) * n);
	ll y = dfs(s, e - 1, _a);

	return _max + max(x, y);
}

int main( void )
{
	cin >> n;

	ll a[n] = {};
	rep( i, n ) {
		cin >> a[i];
	}

	cout << dfs(0, n - 1, a) << endl;

	return 0;
}
