import math

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

l, r = 0, N

ans = [0] * N
end = [0] * N
for i in range(N):
  x2 = 0
  p = "a"
  for j in range(N):
    if end[j] == 1:
      continue
    else:
      if abs(j - l) > abs(r - j - 1):
        x = abs(j - l) * A[j]
      else:
        x = abs(r - j - 1) * A[j]
      x2 = max(x2, x)
      
      if x2 == x:
        p = j

  ans[p] = x2
  end[p] = 1
  
  if abs(p - l) > abs(r - p - 1):
    l += 1
  else:
    r -= 1
  #print(i, ans, end, l, r)    
  
print(sum(ans))
      
      