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=[{
    "rightEnd":N-1,
    "leftEnd":0,
    "grad":0
}]

for child in children:
    newState=[]
    for s in state:
        rightEnd=s["rightEnd"]
        leftEnd=s["leftEnd"]
        grad=s["grad"]
        rval=abs(rightEnd-child["x"])
        lval=abs(leftEnd-child["x"])
        if rval > lval:
            newState+=[{
                "rightEnd":rightEnd-1,
                "leftEnd":leftEnd,
                "grad":grad+rval*child["A"]
            }]
        elif rval < lval:
            newState+=[{
                "rightEnd":rightEnd,
                "leftEnd":leftEnd+1,
                "grad":grad+lval*child["A"]
            }]
        else:
            newState+=[{
                "rightEnd":rightEnd-1,
                "leftEnd":leftEnd,
                "grad":grad+rval*child["A"]
            }]
            newState+=[{
                "rightEnd":rightEnd,
                "leftEnd":leftEnd+1,
                "grad":grad+lval*child["A"]
            }]
    state=newState

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