Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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 = []
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]))


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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 = []
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]))


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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 = []
for i in range(a):
    dp.append([])
    for j in range(a-i):
        dp[i].append(0)
# 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]))

Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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]))

Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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], reverse=True)

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

cnt = 0
# dp[i][j]を考える
for k in range(a-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] + vilist[-2][0] * abs(vilist[-2][1] - i + 1) + 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])
            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))
        else:
            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]))

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


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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]))

Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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 = []
for i in range(a + 1):
    ls = []
    for j in range(a + 1):
        ls.append(0)
    dp.append(ls)

# dp[i][j]を考える
for k in range(a):
    for i in range(k + 2):
        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]))

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

Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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 = []
for i in range(a + 1):
    ls = []
    for j in range(a + 1):
        ls.append(0)
    dp.append(ls)

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

        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]))

max_v = 0
for x in dp:
    max_x = max(x)
    if max_v < max_x:
        max_v = max_x

print(max_v)


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

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

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

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

        if j == 0 and i > 0:
            dp[i][0] = dp[i-1][0] + vilist[k][0] * abs(vilist[k][1] - i + 1)
        elif i == 0 and j > 0:
            dp[0][j] = dp[0][j-1] + vilist[k][0] * abs(a - j - vilist[k][1])
        else:
            # dp[i][j] = max(dp[i][j],
            #                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]))
            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]))

max_v = 0
for x in dp:
    max_x = max(x)
    if max_v < max_x:
        max_v = max_x

print(max_v)

Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from collections import deque


def create_tmp_list(dq1: deque, dq2: deque):
    tmp1 = dq1.copy()
    for x in dq2:
        if tmp1[0] == x:
            tmp1.popleft()
        elif tmp1[-1] == x:
            tmp1.pop()
    return tmp1


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

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

# # dp[x][y]をx人を左から活性度順に並べて、y人を右から活性度順に並べた状態の(スコア最大値, 移動済のindexを記述したリスト)とする
# dp = []
# for i in range(a + 1):
#     ls = []
#     for j in range(a + 1):
#         ls.append((0, []))
#     dp.append(ls)
# 
# # 0番目の人
# # dp[1][0] 左につめる
# dp[1][0] = (vilist[0][0] * abs(vilist[0][1] - 0), [vilist[0][1]])
# # dp[0][1] 右につめる
# dp[0][1] = (vilist[0][0] * abs(a - 1 - vilist[0][1]), [vilist[0][1]])
# 
# # 1番目の人
# # dp[2][0] 左につめる
# dp[1][0] = (vilist[0][0] * abs(vilist[0][1] - 0), [vilist[0][1]])
# # dp[0][1] 右につめる
# dp[0][1] = (vilist[0][0] * abs(a - 1 - vilist[0][1]), [vilist[0][1]])

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

# # 0番目の人
# # dp[1][0] 左につめる
# dp[1][0] = max(dp[1][0], vilist[0][0] * abs(vilist[0][1] - 0))
# # dp[0][1] 右につめる
# dp[0][1] = max(dp[0][1], vilist[0][0] * abs(a - 1 - vilist[0][1]))
#
# # 1番目の人
# # dp[2][0] 左につめる
# dp[2][0] = max(dp[2][0], dp[1][0] + vilist[1][0] * abs(vilist[1][1] - 1))
# # dp[1][1] 左につめる or 右につめる
# dp[1][1] = max(dp[1][1],
#                dp[0][1] + vilist[1][0] * abs(vilist[1][1] - 0),
#                dp[1][0] + vilist[1][0] * abs(a - 1 - vilist[1][1]))
# # dp[0][2] 右につめる
# dp[0][2] = max(dp[0][2], dp[0][1] + vilist[1][0] * abs(a - 2 - vilist[1][1]))
#
# # 2番目の人
# # dp[3][0] 左につめる
# dp[3][0] = max(dp[3][0], dp[2][0] + vilist[2][0] * abs(vilist[2][1] - 2))
# # dp[2][1] 左につめる or 右につめる
# dp[2][1] = max(dp[2][1],
#                dp[1][1] + vilist[2][0] * abs(vilist[2][1] - 1),
#                dp[2][0] + vilist[2][0] * abs(a - 1 - vilist[2][1]))
# # dp[1][2] 左につめる or 右につめる
# dp[1][2] = max(dp[1][2],
#                dp[0][2] + vilist[2][0] * abs(vilist[2][1] - 0),
#                dp[1][1] + vilist[2][0] * abs(a - 2 - vilist[2][1]))
# # dp[0][3] 右につめる
# dp[0][3] = max(dp[0][3], dp[0][2] + vilist[2][0] * abs(a - 3 - vilist[2][1]))
#
# # 3番目の人
# # dp[4][0] 左につめる
# dp[4][0] = max(dp[4][0], dp[3][0] + vilist[3][0] * abs(vilist[3][1] - 3))
# # dp[3][1] 左につめる or 右につめる
# dp[3][1] = max(dp[3][1],
#                dp[2][1] + vilist[3][0] * abs(vilist[3][1] - 2),
#                dp[3][0] + vilist[3][0] * abs(a - 1 - vilist[3][1]))
# # dp[2][2] 左につめる or 右につめる
# dp[2][2] = max(dp[2][1],
#                dp[1][2] + vilist[3][0] * abs(vilist[3][1] - 1),
#                dp[2][1] + vilist[3][0] * abs(a - 2 - vilist[3][1]))
# # dp[1][3] 左につめる or 右につめる
# dp[1][3] = max(dp[1][2],
#                dp[0][3] + vilist[3][0] * abs(vilist[3][1] - 0),
#                dp[1][2] + vilist[3][0] * abs(a - 3 - vilist[3][1]))
# # dp[0][4] 右につめる
# dp[0][4] = max(dp[0][3], dp[0][3] + vilist[3][0] * abs(a - 4 - vilist[3][1]))
#
# print(max(dp[4][0], dp[3][1], dp[2][2], dp[1][3], dp[0][4]))

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

        if j == 0 and i > 0:
            dp[i][0] = max(dp[i][0], dp[i-1][0] + vilist[k][0] * abs(vilist[k][1] - i + 1))
        elif i == 0 and j > 0:
            dp[0][j] = max(dp[0][j], dp[0][j-1] + vilist[k][0] * abs(a - j - vilist[k][1]))
        else:
            dp[i][j] = max(dp[i][j],
                           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]))

max_v = 0
for x in dp:
    max_x = max(x)
    if max_v < max_x:
        max_v = max_x

print(max_v)

Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from collections import deque


def create_tmp_list(dq1: deque, dq2: deque):
    tmp1 = dq1.copy()
    for x in dq2:
        if tmp1[0] == x:
            tmp1.popleft()
        elif tmp1[-1] == x:
            tmp1.pop()
    return tmp1


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

# nums = []
only_nums = []
dic = {}
for i in range(a):
    v = int(strs[i])
    v_list = dic.setdefault(v, deque())
    v_list.append(i)
    only_nums.append(v)

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

# print(nums)

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

max_num_changed = True
max_num = sorted_nums[0]
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:
        sorted_nums = sorted_nums[1:]
        # x,y = 0,1にした場合
        max_num_idx_min = dic[max_num][0]
        res[0][1] = (max_num * (a - 1 - max_num_idx_min), deque([max_num_idx_min]))

        # x,y = 1,0にした場合
        max_num_idx_max = dic[max_num][-1]
        res[1][0] = (max_num * max_num_idx_max, deque([max_num_idx_max]))

    else:
        target_val = sorted_nums[0]
        sorted_nums = sorted_nums[1:]
        if target_val == max_num:
            max_num_changed = False
        else:
            max_num_changed = True
            max_num = target_val

        # print(sorted_nums)

        score_right = -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):
            if max_num_changed:
                already_used_max_num_index = deque()
                max_num_idx_max = dic[max_num][-1]
                max_num_idx_min = dic[max_num][0]
            else:
                already_used_max_num_index = res[i-j-1][j][1]
                tmp_list = create_tmp_list(dic[max_num], already_used_max_num_index)
                max_num_idx_max = tmp_list[-1]
                max_num_idx_min = tmp_list[0]

            # from i-j-1,j to i-j,j (最大活性のものを左に移動)
            score_left = max_num * abs(max_num_idx_max - (i - j - 1)) + res[i - j - 1][j][0]

            # 左端はその値をそのまま利用。右側に移動したものよりも大きい場合はleftの値を設定
            if j == 0 or score_right < score_left:
                res_list = already_used_max_num_index.copy()
                res_list.append(max_num_idx_max)
                res[i - j][j] = (score_left, res_list)

            # from i-j-1,j to i-j-1,j+1 (最大活性のものを右に移動)
            score_right = max_num * abs(a - 1 - max_num_idx_min - j) + res[i - j - 1][j][0]
            # 右側に移動の場合は一旦その値を暫定値として設定してしまう（必要があれば次のループで上書きされるので問題ない）
            # res_list = already_used_max_num_index.copy()
            res_list = already_used_max_num_index
            res_list.append(max_num_idx_min)
            res[i - j - 1][j + 1] = (score_right, res_list)

    if i == a:
        max_v = 0
        for j in range(a + 1):
            v = res[j][i - j][0]
            if max_v < v:
                max_v = v
        # print(result)
        print(max_v)


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-


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

# nums = []
only_nums = []
dic = {}
for i in range(a):
    v = int(strs[i])
    v_list = dic.setdefault(v, set([]))
    v_list.add(i)
    only_nums.append(v)

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

# print(nums)

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

max_num_changed = True
max_num = sorted_nums[0]
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:
        sorted_nums = sorted_nums[1:]
        # x,y = 0,1にした場合
        max_num_idx_min = min(dic[max_num])
        res[0][1] = (max_num * (a - 1 - max_num_idx_min), {max_num_idx_min})

        # x,y = 1,0にした場合
        max_num_idx_max = max(dic[max_num])
        res[1][0] = (max_num * max_num_idx_max, {max_num_idx_max})

    else:
        target_val = sorted_nums[0]
        sorted_nums = sorted_nums[1:]
        if target_val == max_num:
            max_num_changed = False
        else:
            max_num_changed = True
            max_num = target_val

        # print(sorted_nums)

        score_right = -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):
            if max_num_changed:
                already_used_max_num_index = set([])
                max_num_idx_max = max(dic[max_num])
                max_num_idx_min = min(dic[max_num])
            else:
                already_used_max_num_index = res[i-j-1][j][1]
                tmp_list = dic[max_num] - already_used_max_num_index
                max_num_idx_max = max(tmp_list)
                max_num_idx_min = min(tmp_list)

            # from i-j-1,j to i-j,j (最大活性のものを左に移動)
            score_left = max_num * abs(max_num_idx_max - (i - j - 1)) + res[i - j - 1][j][0]

            # 左端はその値をそのまま利用。右側に移動したものよりも大きい場合はleftの値を設定
            if j == 0 or score_right < score_left:
                res_list = already_used_max_num_index.copy()
                res_list.add(max_num_idx_max)
                res[i - j][j] = (score_left, res_list)

            # from i-j-1,j to i-j-1,j+1 (最大活性のものを右に移動)
            score_right = max_num * abs(a - 1 - max_num_idx_min - j) + res[i - j - 1][j][0]
            # 右側に移動の場合は一旦その値を暫定値として設定してしまう（必要があれば次のループで上書きされるので問題ない）
            # res_list = already_used_max_num_index.copy()
            res_list = already_used_max_num_index
            res_list.add(max_num_idx_min)
            res[i - j - 1][j + 1] = (score_right, res_list)

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


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-


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

# nums = []
only_nums = []
dic = {}
for i in range(a):
    v = int(strs[i])
    v_list = dic.setdefault(v, set([]))
    v_list.add(i)
    only_nums.append(v)

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

# print(nums)

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

max_num_changed = True
max_num = sorted_nums[0]
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:
        sorted_nums = sorted_nums[1:]
        # x,y = 0,1にした場合
        max_num_idx_min = min(dic[max_num])
        res[0][1] = (max_num * (a - 1 - max_num_idx_min), {max_num_idx_min})

        # x,y = 1,0にした場合
        max_num_idx_max = max(dic[max_num])
        res[1][0] = (max_num * max_num_idx_max, {max_num_idx_max})

    else:
        target_val = sorted_nums[0]
        sorted_nums = sorted_nums[1:]
        if target_val == max_num:
            max_num_changed = False
        else:
            max_num_changed = True
            max_num = target_val

        # print(sorted_nums)

        score_right = -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):
            if max_num_changed:
                already_used_max_num_index = set([])
                max_num_idx_max = max(dic[max_num])
                max_num_idx_min = min(dic[max_num])
            else:
                already_used_max_num_index = res[i-j-1][j][1]
                tmp_list = dic[max_num] - already_used_max_num_index
                max_num_idx_max = max(tmp_list)
                max_num_idx_min = min(tmp_list)

            # from i-j-1,j to i-j,j (最大活性のものを左に移動)
            score_left = max_num * abs(max_num_idx_max - (i - j - 1)) + res[i - j - 1][j][0]

            # 左端はその値をそのまま利用。右側に移動したものよりも大きい場合はleftの値を設定
            if j == 0 or score_right < score_left:
                res_list = already_used_max_num_index.copy()
                res_list.add(max_num_idx_max)
                res[i - j][j] = (score_left, res_list)

            # from i-j-1,j to i-j-1,j+1 (最大活性のものを右に移動)
            score_right = max_num * abs(a - 1 - max_num_idx_min - j) + res[i - j - 1][j][0]
            # 右側に移動の場合は一旦その値を暫定値として設定してしまう（必要があれば次のループで上書きされるので問題ない）
            res_list = already_used_max_num_index.copy()
            res_list.add(max_num_idx_min)
            res[i - j - 1][j + 1] = (score_right, res_list)

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


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-


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

# nums = []
only_nums = []
dic = {}
for i in range(a):
    v = int(strs[i])
    v_list = dic.setdefault(v, [])
    v_list.append(i)
    only_nums.append(v)

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

# print(nums)

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

max_num_changed = True
max_num = sorted_nums[0]
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:
        sorted_nums = sorted_nums[1:]
        # x,y = 0,1にした場合
        max_num_idx_min = dic[max_num][0]
        res[0][1] = (max_num * (a - 1 - max_num_idx_min), [max_num_idx_min])

        # x,y = 1,0にした場合
        max_num_idx_max = dic[max_num][-1]
        res[1][0] = (max_num * max_num_idx_max, [max_num_idx_max])

    else:
        target_val = sorted_nums[0]
        sorted_nums = sorted_nums[1:]
        if target_val == max_num:
            max_num_changed = False
        else:
            max_num_changed = True
            max_num = target_val

        # print(sorted_nums)

        score_right = -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):
            if max_num_changed:
                already_used_max_num_index = []
                max_num_idx_max = dic[max_num][-1]
                max_num_idx_min = dic[max_num][0]
            else:
                already_used_max_num_index = res[i-j-1][j][1]
                tmp_list = [x for x in dic[max_num][:] if x not in already_used_max_num_index]
                max_num_idx_max = tmp_list[-1]
                max_num_idx_min = tmp_list[0]

            # from i-j-1,j to i-j,j (最大活性のものを左に移動)
            score_left = max_num * abs(max_num_idx_max - (i - j - 1)) + res[i - j - 1][j][0]

            # 左端はその値をそのまま利用。右側に移動したものよりも大きい場合はleftの値を設定
            if j == 0 or score_right < score_left:
                res_list = already_used_max_num_index[:]
                res_list.append(max_num_idx_max)
                res[i - j][j] = (score_left, res_list)

            # from i-j-1,j to i-j-1,j+1 (最大活性のものを右に移動)
            score_right = max_num * abs(a - 1 - max_num_idx_min - j) + res[i - j - 1][j][0]
            # 右側に移動の場合は一旦その値を暫定値として設定してしまう（必要があれば次のループで上書きされるので問題ない）
            res_list = already_used_max_num_index[:]
            res_list.append(max_num_idx_min)
            res[i - j - 1][j + 1] = (score_right, res_list)

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


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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 submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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()
        nums_clone.reverse()
        for x in nums_clone:
            if x[1] == max_num:
                max_num_idx_max = x[0]
                break
        nums_clone.reverse()
        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)
        # print(res[1][0][0])
        # print(res[0][1][0])
    # elif i == 2:
    #     # x,y = 2,0にした場合
    #     nums_clone = res[1][0][1].copy()
    #     # for x in res[1][0][1]:
    #     #     nums_clone.remove(x)
    #     max_num = max([x[1] for x in nums_clone])
    #     max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
    #     nums_clone.remove((max(max_num_idxes), max_num))
    #     res[2][0] = (max_num * abs(max(max_num_idxes) - 1) + res[1][0][0], nums_clone)
    #
    #     # x,y = 0,2にした場合
    #     nums_clone = res[0][1][1].copy()
    #     max_num = max([x[1] for x in nums_clone])
    #     max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
    #     nums_clone.remove((min(max_num_idxes), max_num))
    #     res[0][2] = (max_num * abs(a - 1 - min(max_num_idxes) - 1) + res[0][1][0], nums_clone)
    #
    #     # print(res[2][0][0])
    #     # print(res[0][2][0])
    #
    #     # x,y = 1,1にした場合
    #     # from 1,0
    #     nums_clone = res[1][0][1].copy()
    #     max_num = max([x[1] for x in nums_clone])
    #     max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
    #     nums_clone.remove((min(max_num_idxes), max_num))
    #     from_1_0 = (max_num * abs(a - 1 - min(max_num_idxes)) + res[1][0][0], nums_clone)
    #     # from 0,1
    #     nums_clone = res[0][1][1].copy()
    #     max_num = max([x[1] for x in nums_clone])
    #     max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
    #     nums_clone.remove((max(max_num_idxes), max_num))
    #     from_0_1 = (max_num * abs(max(max_num_idxes)) + res[0][1][0], nums_clone)
    #     if from_1_0[0] < from_0_1[0]:
    #         res[1][1] = from_0_1
    #     else:
    #         res[1][1] = from_1_0
    #
    #     # print(res[1][1][0])

    else:
        # # x,y = i,0にした場合
        # nums_clone = res[i-1][0][1].copy()
        # max_num = max([x[1] for x in nums_clone])
        # target_idx = max([x[0] for x in nums_clone if x[1] == max_num])
        # nums_clone.remove((target_idx, max_num))
        # res[i][0] = (max_num * abs(target_idx - (i - 1)) + res[i-1][0][0], nums_clone)
        #
        # # print(res[i][0][0])
        #
        # # x,y = 0,iにした場合
        # nums_clone = res[0][i-1][1].copy()
        # max_num = max([x[1] for x in nums_clone])
        # target_idx = min([x[0] for x in nums_clone if x[1] == max_num])
        # nums_clone.remove((target_idx, max_num))
        # res[0][i] = (max_num * abs(a - 1 - target_idx - (i - 1)) + res[0][i-1][0], nums_clone)
        #
        # # print(res[0][i][0])

        # 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()
            nums_clone.reverse()
            for x in nums_clone:
                if x[1] == max_num and x[2]:
                    max_num_idx_max = x[0]
                    break
            nums_clone.reverse()
            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
            #
            # nums_clone = res[j-1][i-j][1].copy()
            # max_num = max([x[1] for x in nums_clone])
            # target_idx = max([x[0] for x in nums_clone if x[1] == max_num])
            # nums_clone.remove((target_idx, max_num))
            # # from_a = (max_num * abs(a - 1 - min(max_num_idxes) - (i-j-1)) + res[j-1][i-j][0], res[j-1][i-j][1] + [(min(max_num_idxes), max_num)])
            # from_a = (max_num * abs(target_idx - (j-1)) + res[j-1][i-j][0], nums_clone)
            # # from j,i-j-1
            # nums_clone = res[j][i-j-1][1].copy()
            # max_num = max([x[1] for x in nums_clone])
            # target_idx = min([x[0] for x in nums_clone if x[1] == max_num])
            # nums_clone.remove((target_idx, max_num))
            # # from_b = (max_num * abs(max(max_num_idxes) - (j-1)) + res[j][i-j-1][0], res[j][i-j-1][1] + [(max(max_num_idxes), max_num)])
            # from_b = (max_num * abs(a - 1 - target_idx - (i-j-1)) + res[j][i-j-1][0], nums_clone)
            # if from_a < from_b:
            #     res[j][i-j] = from_b
            # else:
            #     res[j][i-j] = from_a
            # # print(res[j][i-j][0])

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

Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
# 整数の入力
a = int(input())
# スペース区切りの整数の入力
strs = input().split()

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

# 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 = max([x[1] for x in nums])
        max_num_idxes = [x[0] for x in nums if x[1] == max_num]
        # x,y = 1,0にした場合
        nums_clone = nums.copy()
        nums_clone.remove((max(max_num_idxes), max_num))
        res[1][0] = (max_num * max(max_num_idxes), nums_clone)
        # x,y = 0,1にした場合
        nums_clone = nums.copy()
        nums_clone.remove((min(max_num_idxes), max_num))
        res[0][1] = (max_num * (a - 1 - min(max_num_idxes)), nums_clone)

        # print(res[1][0][0])
        # print(res[0][1][0])
    elif i == 2:
        # x,y = 2,0にした場合
        nums_clone = res[1][0][1].copy()
        # for x in res[1][0][1]:
        #     nums_clone.remove(x)
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        nums_clone.remove((max(max_num_idxes), max_num))
        res[2][0] = (max_num * abs(max(max_num_idxes) - 1) + res[1][0][0], nums_clone)

        # x,y = 0,2にした場合
        nums_clone = res[0][1][1].copy()
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        nums_clone.remove((min(max_num_idxes), max_num))
        res[0][2] = (max_num * abs(a - 1 - min(max_num_idxes) - 1) + res[0][1][0], nums_clone)

        # print(res[2][0][0])
        # print(res[0][2][0])

        # x,y = 1,1にした場合
        # from 1,0
        nums_clone = res[1][0][1].copy()
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        nums_clone.remove((min(max_num_idxes), max_num))
        from_1_0 = (max_num * abs(a - 1 - min(max_num_idxes)) + res[1][0][0], nums_clone)
        # from 0,1
        nums_clone = res[0][1][1].copy()
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        nums_clone.remove((max(max_num_idxes), max_num))
        from_0_1 = (max_num * abs(max(max_num_idxes)) + res[0][1][0], nums_clone)
        if from_1_0[0] < from_0_1[0]:
            res[1][1] = from_0_1
        else:
            res[1][1] = from_1_0

        # print(res[1][1][0])

    else:
        # x,y = i,0にした場合
        nums_clone = res[i-1][0][1].copy()
        max_num = max([x[1] for x in nums_clone])
        target_idx = max([x[0] for x in nums_clone if x[1] == max_num])
        nums_clone.remove((target_idx, max_num))
        res[i][0] = (max_num * abs(target_idx - (i - 1)) + res[i-1][0][0], nums_clone)

        # print(res[i][0][0])

        # x,y = 0,iにした場合
        nums_clone = res[0][i-1][1].copy()
        max_num = max([x[1] for x in nums_clone])
        target_idx = min([x[0] for x in nums_clone if x[1] == max_num])
        nums_clone.remove((target_idx, max_num))
        res[0][i] = (max_num * abs(a - 1 - target_idx - (i - 1)) + res[0][i-1][0], nums_clone)

        # print(res[0][i][0])

        # x,y = j,i-jにした場合
        for j in range(1, i):
            # from j-1,i-j
            nums_clone = res[j-1][i-j][1].copy()
            max_num = max([x[1] for x in nums_clone])
            target_idx = max([x[0] for x in nums_clone if x[1] == max_num])
            nums_clone.remove((target_idx, max_num))
            # from_a = (max_num * abs(a - 1 - min(max_num_idxes) - (i-j-1)) + res[j-1][i-j][0], res[j-1][i-j][1] + [(min(max_num_idxes), max_num)])
            from_a = (max_num * abs(target_idx - (j-1)) + res[j-1][i-j][0], nums_clone)
            # from j,i-j-1
            nums_clone = res[j][i-j-1][1].copy()
            max_num = max([x[1] for x in nums_clone])
            target_idx = min([x[0] for x in nums_clone if x[1] == max_num])
            nums_clone.remove((target_idx, max_num))
            # from_b = (max_num * abs(max(max_num_idxes) - (j-1)) + res[j][i-j-1][0], res[j][i-j-1][1] + [(max(max_num_idxes), max_num)])
            from_b = (max_num * abs(a - 1 - target_idx - (i-j-1)) + res[j][i-j-1][0], nums_clone)
            if from_a < from_b:
                res[j][i-j] = from_b
            else:
                res[j][i-j] = from_a
            # print(res[j][i-j][0])

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

Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
# 整数の入力
a = int(input())
# スペース区切りの整数の入力
strs = input().split()

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

# print(nums)

# res[x][y]をx人を左から活性度順に並べて、y人を右から活性度順に並べた状態の(スコア最大値, 移動済のもの)をタプル化したものとする
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 = max([x[1] for x in nums])
        max_num_idxes = [x[0] for x in nums if x[1] == max_num]
        # x,y = 1,0にした場合
        res[1][0] = (max_num * max(max_num_idxes), [(max(max_num_idxes), max_num)])
        # x,y = 0,1にした場合
        res[0][1] = (max_num * (a - 1 - min(max_num_idxes)), [(min(max_num_idxes), max_num)])

        # print(res[1][0][0])
        # print(res[0][1][0])
    elif i == 2:
        # x,y = 2,0にした場合
        nums_clone = nums.copy()
        for x in res[1][0][1]:
            nums_clone.remove(x)
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        res[2][0] = (max_num * abs(max(max_num_idxes) - 1) + res[1][0][0], res[1][0][1] + [(max(max_num_idxes), max_num)])

        # x,y = 0,2にした場合
        nums_clone = nums.copy()
        for x in res[0][1][1]:
            nums_clone.remove(x)
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        res[0][2] = (max_num * abs(a - 1 - min(max_num_idxes) - 1) + res[0][1][0], res[0][1][1] + [(min(max_num_idxes), max_num)])

        # print(res[2][0][0])
        # print(res[0][2][0])

        # x,y = 1,1にした場合
        # from 1,0
        nums_clone = nums.copy()
        for x in res[1][0][1]:
            nums_clone.remove(x)
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        from_1_0 = (max_num * abs(a - 1 - min(max_num_idxes)) + res[1][0][0], res[1][0][1] + [(min(max_num_idxes), max_num)])
        # from 0,1
        nums_clone = nums.copy()
        for x in res[0][1][1]:
            nums_clone.remove(x)
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        from_0_1 = (max_num * abs(max(max_num_idxes)) + res[0][1][0], res[0][1][1] + [(max(max_num_idxes), max_num)])
        if from_1_0[0] < from_0_1[0]:
            res[1][1] = from_0_1
        else:
            res[1][1] = from_1_0

        # print(res[1][1][0])

    else:
        # x,y = i,0にした場合
        nums_clone = nums.copy()
        for x in res[i-1][0][1]:
            nums_clone.remove(x)
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        res[i][0] = (max_num * abs(max(max_num_idxes) - (i - 1)) + res[i-1][0][0], res[i-1][0][1] + [(max(max_num_idxes), max_num)])

        # print(res[i][0][0])

        # x,y = 0,iにした場合
        nums_clone = nums.copy()
        for x in res[0][i-1][1]:
            nums_clone.remove(x)
        max_num = max([x[1] for x in nums_clone])
        max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
        res[0][i] = (max_num * abs(a - 1 - min(max_num_idxes) - (i - 1)) + res[0][i-1][0], res[0][i-1][1] + [(min(max_num_idxes), max_num)])

        # print(res[0][i][0])

        # x,y = j,i-jにした場合
        for j in range(1, i):
            # from j-1,i-j
            nums_clone = nums.copy()
            for x in res[j-1][i-j][1]:
                nums_clone.remove(x)
            max_num = max([x[1] for x in nums_clone])
            max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
            # from_a = (max_num * abs(a - 1 - min(max_num_idxes) - (i-j-1)) + res[j-1][i-j][0], res[j-1][i-j][1] + [(min(max_num_idxes), max_num)])
            from_a = (max_num * abs(max(max_num_idxes) - (j-1)) + res[j-1][i-j][0], res[j-1][i-j][1] + [(max(max_num_idxes), max_num)])
            # from j,i-j-1
            nums_clone = nums.copy()
            for x in res[j][i-j-1][1]:
                nums_clone.remove(x)
            max_num = max([x[1] for x in nums_clone])
            max_num_idxes = [x[0] for x in nums_clone if x[1] == max_num]
            # from_b = (max_num * abs(max(max_num_idxes) - (j-1)) + res[j][i-j-1][0], res[j][i-j-1][1] + [(max(max_num_idxes), max_num)])
            from_b = (max_num * abs(a - 1 - min(max_num_idxes) - (i-j-1)) + res[j][i-j-1][0], res[j][i-j-1][1] + [(min(max_num_idxes), max_num)])
            if from_a < from_b:
                res[j][i-j] = from_b
            else:
                res[j][i-j] = from_a
            # print(res[j][i-j][0])

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

Fixed submission:
# -*- 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()