N=int(input())
a=list(map(int,input().split()))
ans=0
f_max=0
b_max=0
f_box=[0 for i in range(N)]
b_box=[0 for i in range(N)]
count=N
for i in range(N):
    f_box[i]=a[i]*i
    b_box[i]=a[i]*(N-i-1)

while count>0:
    f_max=max(f_box)
    b_max=max(b_box)
    count-=1
    if f_max>b_max:
        ans+=f_max
        k=f_box[::-1].index(f_max)
        a.pop(count-k)
        f_box.pop(count-k)
        b_box.pop(count-k)
        for i in range(count):
            f_box[i]-=a[i]
    elif b_max>f_max:
        ans+=b_max
        k=b_box.index(b_max)
        a.pop(k)
        f_box.pop(k)
        b_box.pop(k)
        for i in range(count):
            b_box[i]-=a[i]
    else:
        ans+=f_max
        kf=f_box[::-1].index(f_max)
        kb=b_box.index(b_max)
        if count-kf<kb:
            k=kb
            a.pop(k)
            f_box.pop(k)
            b_box.pop(k)
            for i in range(count):
                b_box[i]-=a[i]
        else:
            k=count-kf
            a.pop(k)
            f_box.pop(k)
            b_box.pop(k)
            for i in range(count):
                f_box[i]-=a[i]
print(ans)
