N=int(input())
A=list(map(int,input().split()))
tmp=[(0,0)]
for i in range(N):
    tmp.append((A[i],i+1))

for _ in range(N):
    for i in range(1,N):
        Before=tmp[i][0]*abs(i-tmp[i][1])+tmp[i+1][0]*abs(i+1-tmp[i+1][1])
        After=tmp[i][0]*abs(i+1-tmp[i][1])+tmp[i+1][0]*abs(i-tmp[i+1][1])
        
        if After>Before:
            tmp[i],tmp[i+1]=tmp[i+1],tmp[i]
ans=0
for i in range(1,N+1):
    ans+=tmp[i][0]*abs(i-tmp[i][1])
print(ans)