n = int(input())
lst = [int(i) for i in input().split()]

total = 0
index_1 = 0
index_2 = n - 1
while lst != [0] * n:
  max_number_1 = -1
  max_index_1 = 0
  max_number_2 = -1
  max_index_2 = 0
  #print(index_1)
  #print(index_2)
  for index in range(n):
    if lst[index] != 0:
      max_1 = lst[index] * (index - index_1)
      max_2 = lst[index] * (index_2 - index)
      if max_1 > max_2:
        if max_1 > max_number_1:
          max_number_1 = max_1
          max_index_1 = index
      else:
        if max_2 > max_number_2:
          max_number_2 = max_2
          max_index_2 = index
  if max_number_1 >= max_number_2:
    total += max_number_1
    index_1 += 1
    lst[max_index_1] = 0
  else:
    total += max_number_2
    index_2 -= 1
    lst[max_index_2] = 0
  #print(total)
  #print(lst)
print(total)
