
# -*- 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]
As=As[inds]

ds=np.empty((N+1,N+1),dtype=np.int64)

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

for xy in range(2,N+1):
    a=As[xy-1]
    i=inds[xy-1]
    for x in range(1,xy):
        y=xy-x
        c1=ds[x-1,y]+a*(i-(x-1))
        c2=ds[x,y-1]+a*(N-y-i)
        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)


