from sys import stdin

N = int(stdin.readline().rstrip())
As = [int(x) for x in stdin.readline().rstrip().split()]

#greedyに解けるか?
#greedyで解けるならば計算量はN**2
#N<2000なので行ける1

selected = []

happy_sum = 0
for i in range(N):

    #まず左端から処理をする
    max_happy = 0
    max_happy_pos = -1
    for j in range(N):
        if j in selected:
            continue
        #移動距離 |i-j|*Ajが最高になるjを選ぶ
        distance = abs(i-j)
        if max_happy_pos>=0:
            if max_happy + As[max_happy_pos] < As[j]*distance  + As[j]:
                max_happy = As[j]*distance
                max_happy_pos = j
        else:
            if max_happy < distance*As[j]: 
                max_happy = distance*As[j]
                max_happy_pos=j
    selected.append(max_happy_pos)
    happy_sum += max_happy
    if len(selected) == N :
        break
    max_happy = 0
    max_happy_pos = -1

    #次に右端から見ていく
    for j in range(N):
        if j in selected:
            continue
        #移動距離 |i-j|*Ajが最高になるjを選ぶ
        distance = abs((N-i-1)-j)
        if max_happy_pos>=0:
            if max_happy + As[max_happy_pos] < As[j]*distance  + As[j]:
                max_happy = As[j]*distance
                max_happy_pos = j
        else:
            if max_happy < distance*As[j]: 
                max_happy = distance*As[j]
                max_happy_pos=j
    selected.append(max_happy_pos)
    happy_sum += max_happy
    if len(selected) == N :
        break

print(happy_sum) 