#include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
	int f=1;char k=getchar();x=0;
	for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
	for(;k>='0'&&k<='9';k=getchar()) x=(x<<3)+(x<<1)+k-'0';
	x*=f;
}
template<class T>il _print(T x){
	if(x/10) _print(x/10);
	putchar(x%10+'0');
}
template<class T>il print(T x){
	if(x<0) putchar('-'),x=-x;
	_print(x);
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int m,int mod){
	int res=1,bas=x;
	while(m){
		if(m&1) res=(1ll*res*bas)%mod;
		bas=(1ll*bas*bas)%mod,m>>=1;
	}
	return res;
}
const int N = 4e3+5;
int n,s,t,val[N],num_edge=-1,head[N];
struct Edge{ ll next,to,w,c; }edge[N*N];
il add_edge(int u,int v,int w,ll c){
	edge[++num_edge]=(Edge){ head[u],v,w,c },head[u]=num_edge;
	edge[++num_edge]=(Edge){ head[v],u,0,-c },head[v]=num_edge;
}
ll mn_cost,flow[N],dis[N],pre[N],lst[N];
char vis[N];
inl bool SPFA(int s,int t){
	for(ri i=s;i<=t;++i) dis[i]=1e18;
	del(flow,0x3f),dis[s]=0,pre[t]=-1;
	queue<int> q;q.push(s),vis[s]=1;
	while(!q.empty()){
		int pos=q.front();q.pop(),vis[pos]=0;
		for(ri i=head[pos];i!=-1;i=edge[i].next)
			if(dis[edge[i].to]>dis[pos]+edge[i].c&&edge[i].w>0){
				dis[edge[i].to]=dis[pos]+edge[i].c;
				pre[edge[i].to]=pos,lst[edge[i].to]=i;
				flow[edge[i].to]=min(flow[pos],edge[i].w);
				if(!vis[edge[i].to]) q.push(edge[i].to),vis[edge[i].to]=1;
			}
	}
	return pre[t]!=-1;
}
il MCMF(int s,int t){
	while(SPFA(s,t)){
		mn_cost+=dis[t]*flow[t];
		for(ri u=t;u^s;u=pre[u]) edge[lst[u]].w-=flow[t],edge[lst[u]^1].w+=flow[t];
	}
}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	read(n),del(head,-1),t=n+n+1;
	for(ri i=1;i<=n;++i) read(val[i]);
	for(ri i=1;i<=n;++i) add_edge(s,i,1,0);
	for(ri i=1;i<=n;++i) add_edge(i+n,t,1,0);
	for(ri i=1;i<=n;++i) for(ri j=1;j<=n;++j) add_edge(i,j+n,1,-1ll*val[i]*abs(i-j));
	MCMF(s,t);
	print(-mn_cost);
	return 0;
}