import numpy as np
N=int(input())
A=list(map(int, input().split()))
A=np.array(A)

B = list(zip(A, list(range(N))))

if N%2==0:left, right = N//2, N//2
else:left, right = N//2+1, N//2
  

B_l = B[:left]
B_r = B[right:]

C = sorted(B_r, key=lambda x: x[0], reverse=True) + sorted(B_l,key=lambda x: x[0], )

sum2 = 0
for i in range(N):
  sum2 += abs(C[i][1] - i)*C[i][0]

sum_ = 0
left = 0
right = 0
for i in range(N):
  seq1 = A*(np.arange(N)-left)
  seq2 = A*(np.arange(N)[::-1]-right)
  if max(seq1) >= max(seq2):
    sum_ += max(seq1)
    A[seq1.argmax()]=0
    left += 1
  else:
    sum_ += max(seq2)
    A[seq2.argmax()]=0
    right += 1

print(max(sum_, sum2))