N = int(input())
Atmp=list(map(int,input().split(' ')))
A=[]
for i in range(N):
    A.append([Atmp[i],i])

Afin=[]
if N%2 == 0:
    Aleft=A[0:N//2]
    Aleft.sort()
    Aright=A[N//2:N]
    Aright.sort(reverse=True)

    Afin = Aright
    Afin.extend(Aleft)
    cnt=0
    for i in range(N):
        cnt+=Afin[i][0]*abs(Afin[i][1]-i)
    print(cnt)
else:
    Aleft=A[0:int(N//2)]
    Aleft.sort()
    Aright=A[int(N//2):N]
    Aright.sort(reverse=True)

    Afin = Aright
    Afin.extend(Aleft)
    cnt1=0
    for i in range(N):
        cnt1+=Afin[i][0]*abs(Afin[i][1]-i)

    Aleft=A[0:int(N//2)+1]
    Aleft.sort()
    Aright=A[int(N//2)+1:N]
    Aright.sort(reverse=True)

    Afin = Aright
    Afin.extend(Aleft)
    cnt2=0
    for i in range(N):
        cnt2+=Afin[i][0]*abs(Afin[i][1]-i)

    print(max(cnt1,cnt2))
    