AAwIdx = [[0]*2 for _ in range(N)]
for i in range(N):
    AAwIdx[i][0] = i
    AAwIdx[i][1] = AA[i]
AA_sorted = sorted(AAwIdx, key=lambda x: x[1], reverse=True)
#print(AA_sorted)
Idx_i = 0
Idx_f = N-1
AnsIdx = [0]*N
#print(Idx_f)
answer = 0
for i in range(N):
    if Idx_f - AA_sorted[i][0] > AA_sorted[i][0] - Idx_i:
        AnsIdx[AA_sorted[i][0]] = Idx_f
        #answer += abs(AA_sorted[i][0]-Idx_f)*AA_sorted[i][1]
        Idx_f -= 1
    else:
        AnsIdx[AA_sorted[i][0]] = Idx_i
        #answer += abs(AA_sorted[i][0]-Idx_i)*AA_sorted[i][1]
        Idx_i += 1
    #print(AA_sorted[i][0],Idx_i,Idx_f)
for i in range(N):
    answer += abs(AAwIdx[i][0] - AnsIdx[i])*AAwIdx[i][1]
answerb = 0
Idx_i = 0
Idx_f = N-1
AnsIdx = [0]*N
if Idx_f - AA_sorted[0][0] > AA_sorted[0][0] - Idx_i:
    AnsIdx[AA_sorted[i][0]] = Idx_i
    #answer += abs(AA_sorted[i][0]-Idx_i)*AA_sorted[i][1]
    Idx_i += 1
else:
    AnsIdx[AA_sorted[i][0]] = Idx_f
    #answer += abs(AA_sorted[i][0]-Idx_f)*AA_sorted[i][1]
    Idx_f -= 1
for i in range(1,N):
    if Idx_f - AA_sorted[i][0] > AA_sorted[i][0] - Idx_i:
        AnsIdx[AA_sorted[i][0]] = Idx_f
        #answer += abs(AA_sorted[i][0]-Idx_f)*AA_sorted[i][1]
        Idx_f -= 1
    else:
        AnsIdx[AA_sorted[i][0]] = Idx_i
        #answer += abs(AA_sorted[i][0]-Idx_i)*AA_sorted[i][1]
        Idx_i += 1
    #print(AA_sorted[i][0],Idx_i,Idx_f)
for i in range(N):
    answerb += abs(AAwIdx[i][0] - AnsIdx[i])*AAwIdx[i][1]
#print(AnsIdx)
print(max(answer, answerb))