cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
cdef main():
    cdef int n,i,j,k,a
    cdef list A,d
    n,*A=map(int,open(0).read().split())
    d=[0]
    for j,(a,i)in enumerate(sorted((a,i)for i,a in enumerate(A))[::-1]):
        d=[d[0]+a*(~i-j+n)]+[max(d[k+1]+a*(n+k-i-j),d[k]+a*abs(i-k))for k in range(j)]+[d[j]+a*abs(i-j)]
    print(max(d))
main()