N=int(input())
actlist=list(map(int,input().split()))
As = sorted([(A, i) for i, A in enumerate(actlist)])[::-1]

left=1
right=N
sumnum=0

for i in As:
    move=0
    moveleft=abs(i[1]+1-left)
    moveright=abs(i[1]+1-right)
    if moveleft>moveright:
        move=moveleft
        left+=1
    else:
        move=moveright
        right=right-1
    sumnum+=move*i[0]
    
print(sumnum)