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

# 整数の入力
a = int(input())
# # スペース区切りの整数の入力
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])

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

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

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

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