import numpy as np

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

max_happiness = [a * max(N-i-1, i) for i, a in enumerate(A)]
left_right = [(N-i-1)-i for i in range(N)]
args = np.flip(np.argsort(max_happiness))

res = 0


while(True) :
  if(len(max_happiness)==0) :
    break
  else :
    ## 最大の嬉しさを抜き出す
    temp_max = max_happiness[args[0]]
    ## 最大の嬉しさになる子供をすべて見つける
    index = [i for i, x in enumerate(max_happiness) if x == temp_max]
    
    ## indexの中で、活発度が最大の子供を選ぶ
    max_index = 0
    v_ind = 0
    for i in index :
      if(v_ind < A[i]) :
        max_index = i
        v_ind = A[i]
    
    res += temp_max
    ## 左右の選択
    if(left_right[max_index] > 0) :
      ## 右のほうが近い場合は嬉しさが下がる
      max_happiness = list(map(lambda x : x[1]-A[x[0]]*int((np.sign(left_right[x[0]])+1)/2), enumerate(max_happiness)))
      left_right_flag = -1
    if(left_right[max_index] < 0) :
      ## 左のほうが近い場合は嬉しさが下がる
      max_happiness = list(map(lambda x : x[1]+A[x[0]]*int((np.sign(left_right[x[0]])-1)/2), enumerate(max_happiness)))
      left_right_flag = 1
    ## 等しければ、2番目に嬉しさが大きい子供を参照
    if(left_right[max_index] == 0) :
      if(len(args) == 1) :
        break
      temp_max_second = max_happiness[args[1]]
      index_second = [i for i, x in enumerate(max_happiness) if x == temp_max_second]
      second_index = 0
      v_second = 0
      for i in index_second :
        if((v_second < A[i]) and (i != max_index)) :
          second_index = i
          v_second = A[i]
      if(left_right[second_index] > 0) :
        ## 左のほうが近い場合は嬉しさが下がる
        max_happiness = list(map(lambda x : x[1]+A[x[0]]*int((np.sign(left_right[x[0]])-1)/2), enumerate(max_happiness)))
        left_right_flag = 1
      else :
        ## 右のほうが近い場合は嬉しさが下がる
        max_happiness = list(map(lambda x : x[1]-A[x[0]]*int((np.sign(left_right[x[0]])+1)/2), enumerate(max_happiness)))
        left_right_flag = -1
    ##繰り返しの処理
    max_happiness.pop(max_index)
    left_right.pop(max_index)
    left_right = list(map(lambda x : x+left_right_flag, left_right))
    A.pop(max_index)
    args = args[1:]
    args = np.array(list(map(lambda x : x-1 if (x>max_index) else x, args)))
    

print(res)