N,A=open(0);N=int(N);A=sorted((int(x),i)for i,x in enumerate(A.split()));t=[0]*N**2
for w in range(1,N):
 a,j=A[w]
 for l in range(N-w):
  r=l+w;t[l*N+r]=max(a*abs(j-l)+t[(l+1)*N+r],a*abs(j-r)+t[l*N+r-1])
print(t[N-1])