# -*- coding: utf-8 -*-

# 整数の入力
a = int(input())
# # スペース区切りの整数の入力
# strs = input().split()

nums = list(map(int, input().split()))

vilist = []
for i in range(a):
    vilist.append((nums[i], i))
vilist = sorted(vilist, key=lambda vi: vi[0], reverse=True)

# dp[x][y]をx人を左から活性度順に並べて、y人を右から活性度順に並べた状態のスコア最大値とする
dp = [[0] * a for i in range(a)]
# for i in range(a + 1):
#     ls = ([0] * a) * a
#     ls *
#     dp.append(ls)

cnt = 0
# dp[i][j]を考える
for k in range(a-2):
    for i in range(k + 2):
        cnt += 1
        j = k - i + 1
        if i == 0 and j == 0:
            continue

        if j == 0:
            dp[i][0] = dp[i-1][0] + vilist[k][0] * abs(vilist[k][1] - i + 1)
        elif i == 0:
            dp[0][j] = dp[0][j-1] + vilist[k][0] * abs(a - j - vilist[k][1])
        else:
            dp[i][j] = max(dp[i-1][j] + vilist[k][0] * abs(vilist[k][1] - i + 1),
                           dp[i][j-1] + vilist[k][0] * abs(a - j - vilist[k][1]))

# 最後の値は場所が特定できるのでそのまま計算させる
k = a - 2
for i in range(k + 2):
    cnt += 1
    j = k - i + 1
    if j == 0:
        dp[i][0] = dp[i-1][0] + vilist[-2][0] * abs(vilist[-2][1] - i + 1) + vilist[-1][0] * abs(vilist[-1][1] - i)
        # dp[i+1][0] = dp[i][0] + vilist[-1][0] * abs(vilist[-1][1] - i)
    elif i == 0:
        dp[0][j] = dp[0][j-1] + vilist[-2][0] * abs(a - j - vilist[-2][1]) + vilist[-1][0] * abs(a - j - 1 - vilist[-1][1])
        # dp[0][j+1] = dp[0][j]
    else:
        dp[i][j] = max(dp[i-1][j] + vilist[-2][0] * abs(vilist[-2][1] - i + 1) + vilist[-1][0] * abs(vilist[-1][1] - i),
                       dp[i][j-1] + vilist[-2][0] * abs(a - j - vilist[-2][1]) + vilist[-1][0] * abs(vilist[-1][1] - i))


# print(cnt)
print(max([max(x) for x in dp]))