n=int(input())
a=list(map(int,input().split()))
y=n-1
x=0
s=0
for i in range(n):
    b=max(a)
    m=a.index(b)
    if abs(m-x)>abs(y-m):
        s+=b*abs(m-x)
        x+=1
    else:
        s+=b*abs(y-m)
        y-=1


    a[m]=0
    
print(s)