#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i,a,b) for(register int i=a;i<=b;++i)
#define repd(i,a,b) for(register int i=a;i>=b;--i)
#define reps(s) for(int i=head[s];i;i=e[i].nxt)
#define ce(i,r) i==r?'\n':' '
#define pb push_back 
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
typedef long long ll;
const int MAX_N=2e3+5;
struct node{
	ll val;
	int id;
}a[MAX_N];
bool cmp(node a,node b)
{
	return a.val>b.val;
}
ll dp[MAX_N][MAX_N];
int main()
{
	ms(dp);
	int n; si(n);
	repi(i,1,n) sl(a[i].val),a[i].id=i;
	sort(a+1,a+1+n,cmp);
	ll ans=0;
	repi(l,0,n-1)repi(r,0,n-1)if(l+r+1<=n){
		int now=l+r+1;
		dp[l+1][r]=max(dp[l+1][r],dp[l][r]+a[i].val*abs(l-a[now].id));
		dp[l][r+1]=max(dp[l][r+1],dp[l][r]+a[i].val*abs((n-r)-a[now].id));
		ans=max(ans,max(dp[l+1][r],dp[l][r+1]));
	}
	printf("%lld\n",ans);
	return 0;
} 