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

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