N=int(input())
A=list(map(int,input().split()))

A=[(A[i],i) for i in range(0,N)]
A.sort(reverse=True)


dp=[[0 for i in range(0,N)] for j in range(0,N)]

for i in range(0,N-1):
    a,index=A[-1]
    dp[N-1][i]=a*abs(index-i)

for _ in range(1,N):
    i=N-1-_
    for j in range(0,i+1):
        left=j
        right=i-j
        a,index=A[i]
        ans=0
        if index>left-1:
            ans=max(ans,dp[i+1][j+1]+a*(index-left))

        if N-right>index:
            ans=max(ans,dp[i+1][j]+a*(N-right-1-index))

        dp[i][j]=ans

print(dp[0][0])