
# -*- coding: utf-8 -*-
import numpy as np
N=int(input())
As=list(map(int, input().split()))

As=np.array(As)
inds=np.argsort(As)[::-1]

ds=np.zeros((N+1,N+1),dtype=int)

ds[0][0]=0
for i in range(1,N+1):
    ds[i][0]=ds[i-1][0]+As[inds[i-1]]*(inds[i-1]-(i-1))
    ds[0][i]=ds[0][i-1]+As[inds[i-1]]*(N-i-inds[i-1])

for xy in range(2,N+1):
    for x in range(1,xy):
        y=xy-x
        c1=ds[x-1,y]+As[inds[xy-1]]*(inds[xy-1]-(x-1))
        c2=ds[x,y-1]+As[inds[xy-1]]*(N-y-inds[xy-1])
        ds[x,y]=max(c1,c2)

ans=0
for x in range(0,N+1):
    y=N-x
    c=ds[x,y]
    ans=max(c,ans)
print(ans)
#print(ds)
#print(inds)


