tmp = input().split()
NChild = int(tmp[0])

tmp = input().split()
Active_List = [int(s) for s in tmp]

ArrayD = [0] * NChild

for i in range(NChild):
    ArrayD[i] = [i+1, Active_List[i], max(abs(i),abs(NChild-i-1)), Active_List[i]*max(abs(i),abs(NChild-i-1))]

ArrayC = sorted(ArrayD, key=lambda x: (x[3], x[1],x[2]), reverse=True)

NArray = [0] * NChild
EndP = NChild - 1
StartP = 0

for i in range(NChild):
    P = ArrayC[i][0]
    if abs(P - StartP - 1) < abs(P - EndP - 1):
        NArray[EndP] = P
        EndP = EndP - 1
    else:
        NArray[StartP] = P
        StartP = StartP + 1
        
TotalResult = 0

for i in range(NChild):
    KeyA = ArrayC.index(ArrayD[NArray[i]-1])
    TotalResult = TotalResult + ArrayC[KeyA][1]*abs(NArray[i]-i-1)

print(TotalResult)