N=int(input())
A=[int(i) for i in input().split(" ")]

children=[{"A":A[i],"x":i} for i in range(len(A))]
children.sort(key=lambda x:x["A"],reverse=True)

state=[(
    N-1,#RightEnd
    0,#leftEnd
    0 #grad
)]

for child in children:
    newState=[]
    for s in state:
        rightEnd=s[0]
        leftEnd=s[1]
        grad=s[2]
        rval=abs(rightEnd-child["x"])
        lval=abs(leftEnd-child["x"])
        newState+=[(
            rightEnd-1,
            leftEnd,
            grad+rval*child["A"]
        ),
        (
            rightEnd,
            leftEnd+1,
            grad+lval*child["A"]
        )]
    state=newState

state.sort(key=lambda x:x[2],reverse=True)
print(state[0][2])
