N = int(input())
*A, = map(int,input().split())

ans = 0
done = [False]*N
for i in range(N//2):
    j = N-1-i
    L = [(a*abs(k-i)*(1 - done[k]*2)-done[k],k) for k,a in enumerate(A)]
    R = [(a*abs(k-j)*(1 - done[k]*2)-done[k],k) for k,a in enumerate(A)]
    L.sort(reverse=True)
    R.sort(reverse=True)
    if L[0][1] != R[0][1]:
        ans += L[0][0] + R[0][0]
        done[L[0][1]] = True
        done[R[0][1]] = True
    else:
        if L[0][0] + R[1][0] > L[1][0] + R[0][1]:
            ans += L[0][0] + R[1][0]
            done[L[0][1]] = True
            done[R[1][1]] = True
        else:
            ans += L[1][0] + R[0][0]
            done[L[1][1]] = True
            done[R[0][1]] = True
if N%2==1:
    i = N//2
    k = done.index(False)
    ans += A[k]*abs(i-k)
print(ans)