n=int(input())
import numpy as np
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append((a[i],i))
l.sort(reverse=True) 
left=0
right=n-1
ans=0
for i in l:
    if abs(right-i[1])==abs(i[1]-left):
        if n-1-right<left:
            ans+=i[0]*abs(right-i[1])
            right-=1
            
        else:
            ans+=i[0]*abs(i[1]-left)
            left+=1
    elif abs(right-i[1])<abs(i[1]-left):
        ans+=i[0]*abs(i[1]-left)
        left+=1
    else:
        ans+=i[0]*abs(right-i[1])
        right-=1 
    
print(ans)