
//    ____       ____                 _           
//   |  _ \ _ __|  _ \  ___ _ __   __| |_ __ ___  
//   | | | | '__| | | |/ _ \ '_ \ / _` | '__/ _ \ 
//   | |_| | |_ | |_| |  __/ | | | (_| | | | (_) |
//   |____/|_(_)|____/ \___|_| |_|\__,_|_|  \___/ 
//                                                

 
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;


#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define f first
#define s second
#define pb push_back
#define ll long long

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll LLOO = 1e18+7;
const ll OO = 1e9+7;
const int MOD = 1e9+7;
const int N = 2e3+7;

int n;
int a[N];
ll mem[N][N];
vector<pair<int,int> > vec;

ll dp(int l,int r)
{
	ll &ret = mem[l][r];
	if (ret != -1) return ret;
	if (l > r) return 0;
	ll ch1 = dp(l+1,r)+vec[l+n-1-r].f*abs(vec[l+n-1-r].s-l);
	ll ch2 = dp(l,r-1)+vec[l+n-1-r].f*abs(vec[l+n-1-r].s-r);
	return ret = max(ch1,ch2);
}

int main()
{
	memset(mem,-1,sizeof mem);
	scanf("%d",&n);
	rep(i,n) scanf("%d",a+i);
	rep(i,n) vec.pb({a[i],i});
	sort(vec.begin(),vec.end());
	reverse(vec.begin(),vec.end());
	printf("%lld\n",dp(0,n-1));
    return 0;
}

