n=int(input())
ls=list(map(int,input().split()))
d={}
for i in range(n):
    d[i]=ls[i]
start_i=0
end_i=n-1
sums=0
left_sum=0
right_sum=sum(ls)
for i in range(n):
    mx=0
    mx_pair=[]
    for idx,infant in d.items():
        if infant==-1:continue
        left_sum+=infant
        right_sum-=infant
        left, right = infant*(abs(idx-start_i)), infant*(abs(idx-end_i))
        if left>=mx:
            mx=left
            mx_pair=[idx,"left"]
        if right>=mx:
            mx=right
            mx_pair=[idx,"right"]
    sums+=mx
    if mx_pair[1]=="left":
        start_i+=1
    elif mx_pair[1]=="right":
        end_i-=1
    d[mx_pair[0]]=-1
print(sums)