n = int(input())
l = list(map(int, input().split()))

r = 0
left, right = 0, n-1
while left <= right:
  m = max(l)
  p = l.index(m)
  l[p] = -1
  if abs(p-left) > abs(p-right):
    r += m*abs(p-left)
    left += 1
  else:
    r += m*abs(p-right)
    right -= 1

    
print(r)