import numpy as np
N = int(input())
A = np.array([int(x) for x in input().split()])
B = range(N)
left = 0
right = N-1
score = 0
for j in range(N):
  C = [max(B[i]-left,right-B[i])*A[i] for i in range(N-j)]
  score += max(C)
  if (B[np.argmax(C)]-left)>(right-B[np.argmax(C)]):
    left+=1
  else:
    right-=1
  A = np.delete(A,np.argmax(C))
  B= np.delete(B,np.argmax(C))
print(score)