n = int(input())
A = list(map(int, input().split()))
newA=[]
for i,a in enumerate(A):
    newA.append((a,i))
newA=sorted(newA, key=lambda x:x[1])
newA=sorted(newA, reverse=True)
#print(newA)
l1=1
r1=n-1
l2=0
r2=n-2
ans1=0
ans2=0
for i in range(len(newA)):
    if i==0:
        ans1+=newA[i][0]*abs(newA[i][1])
    else:
        if abs(newA[i][1]-l1)>abs(newA[i][1]-r1):
            ans1+=newA[i][0]*abs(newA[i][1]-l1)
            l1+=1
        else:
            ans1+=newA[i][0]*abs(newA[i][1]-r1)
            r1-=1

for i in range(len(newA)):
    if i==0:
        ans2+=newA[i][0]*abs(newA[i][1]-(n-1))
    else:
        if abs(newA[i][1]-l2)>=abs(newA[i][1]-r2):
            ans2+=newA[i][0]*abs(newA[i][1]-l2)
            l2+=1
        else:
            ans2+=newA[i][0]*abs(newA[i][1]-r2)
            r2-=1

print(max(ans1,ans2))