### BUGGY CODE ###

# -*- coding: utf-8 -*-
# 整数の入力
a = int(input())
# スペース区切りの整数の入力
strs = input().split()

nums = []
only_nums = []
for i in range(a):
    v = int(strs[i])
    nums.append((i, v, True))
    only_nums.append(v)

sorted_nums = sorted(only_nums, reverse=True)
# print(sorted_nums)

# print(nums)

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

for i in range(a + 1):
    # print("i in range(a)")
    # print(i)
    # print(a)
    # print("i in range(a)")
    if i == 0:
        continue
    if i == 1:
        max_num = sorted_nums[0]
        sorted_nums = sorted_nums[1:]
        # x,y = 0,1にした場合
        max_num_idx_min = -1
        nums_clone = nums.copy()
        for x in nums_clone:
            if x[1] == max_num:
                max_num_idx_min = x[0]
                break
        nums_clone[max_num_idx_min] = (nums_clone[max_num_idx_min][0], nums_clone[max_num_idx_min][1], False)
        res[0][1] = (max_num * (a - 1 - max_num_idx_min), nums_clone)

        # x,y = 1,0にした場合
        max_num_idx_max = -1
        nums_clone = nums.copy()
        for y in range(len(nums_clone)):
            x = nums_clone[len(nums_clone) - 1 - y]
            if x[1] == max_num:
                max_num_idx_max = x[0]
                break
        nums_clone[max_num_idx_max] = (nums_clone[max_num_idx_max][0], nums_clone[max_num_idx_max][1], False)
        res[1][0] = (max_num * max_num_idx_max, nums_clone)

    else:
        # print(sorted_nums)
        max_num = sorted_nums[0]
        sorted_nums = sorted_nums[1:]
        # x,y = i-j,j(0 ≤ j ≤ i)にした場合を考える
        # i - 1のresについてそれぞれ次の状態を考えていく
        # res[i-1][0], res[i-2][1], res[i-3][2], ... , res[i-1-j][j], ... , res[0][i-1]
        ls_j = []
        for j in range(i):
            # from i-j-1,j to i-j,j (最大活性のものを左に移動)
            max_num_idx_max = -1
            nums_clone = res[i - j - 1][j][1].copy()
            for y in range(len(nums_clone)):
                x = nums_clone[len(nums_clone) - 1 - y]
                if x[1] == max_num and x[2]:
                    max_num_idx_max = x[0]
                    break
            score_left = max_num * abs(max_num_idx_max - (i - j - 1)) + res[i - j - 1][j][0]
            nums_clone[max_num_idx_max] = (nums_clone[max_num_idx_max][0], nums_clone[max_num_idx_max][1], False)
            left = (score_left, nums_clone)
            # res[j-1][i-j+1] = (score_left, nums_clone)

            # from i-j-1,j to i-j-1,j+1 (最大活性のものを右に移動)
            max_num_idx_min = -1
            nums_clone = res[i - j - 1][j][1].copy()
            for x in nums_clone:
                if x[1] == max_num and x[2]:
                    max_num_idx_min = x[0]
                    break
            score_right = max_num * abs(a - 1 - max_num_idx_min - j) + res[i - j - 1][j][0]
            nums_clone[max_num_idx_min] = (nums_clone[max_num_idx_min][0], nums_clone[max_num_idx_min][1], False)
            right = (score_right, nums_clone)
            # res[j-1][i-j+1] = (score_right, nums_clone)

            ls_j.append((left, right))

        # iのres (res[i-j][j]) についてそれぞれleftとrightのどちらがより高いスコアかを考えて値を決定する
        for j in range(i + 1):
            # i,0は決定済
            if j == 0:
                res[i - j][j] = ls_j[0][0]
            # 0,iは決定済
            elif j == i:
                res[i - j][j] = ls_j[i - 1][1]
            else:
                # 木のイメージで左側から伸びてきた枝
                from_left = ls_j[j - 1][1]
                # 木のイメージで右側から伸びてきた枝
                from_right = ls_j[j][0]
                if from_left[0] < from_right[0]:
                    res[i - j][j] = from_right
                else:
                    res[i - j][j] = from_left

    if i == a:
        result = []
        for j in range(a + 1):
            result.append(res[j][i - j][0])
        # print(result)
        print(max(result))


### FIXED CODE ###

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


def main():

    # 整数の入力
    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 = []
    for i in range(a):
        dp.append([0]*(a-i))

    # dp = [[0] * a for i in range(a)]

    # cnt = 0
    # dp[i][j]を考える
    for k in range(a-1):
        vi = vilist.pop()
        v = vi[0]
        p = vi[1]
        vi_z = vilist[0]
        vz = vi_z[0]
        pz = vi_z[1]

        for i in range(k + 2):
            # cnt += 1
            j = k - i + 1
            # if i == 0 and j == 0:
            #     continue

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

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


main()