n=int(input())
nums=list(map(int,input().split()))
ans=0
r=0
l=n-1
for i in range(n):
    maxh=0
    pos=0
    lr=0
    c=(l+r)/2
    for j in range(n):
        happy=nums[j]*max(j-r,l-j)
        if happy>maxh or (happy==maxh and nums[j]>nums[pos]):
            maxh=happy
            pos=j
    if c.is_integer() and pos==c:
        forsum=0
        for j in range(int(c)):
            forsum+=nums[j]
        backsum=sum(nums)-forsum-nums[int(c)]
        if forsum>=backsum:
            lr=1
        else:
            lr=2
    if pos>c or lr==1:
        r+=1
    else:
        l-=1
    ans+=maxh
    nums[pos]=0
print(ans)