alexjercan/bugnet huggingface dataset:
Problem p02703 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

INF = 10**20
A = 50  # max A


def inpl():
    return list(map(int, input().split()))


def main():
    N, M, S = inpl()
    S = min(S, M * A)

    lines = [[] for _ in range(N * (M * A + 1))]
    for _ in range(M):
        in_s, in_t, cost, time = inpl()
        in_s -= 1
        in_t -= 1

        for n in range(cost, M * A + 1):
            s = in_s + n * N
            t = in_t + (n - cost) * N
            lines[s].append((t, time))

            s = in_s + (n - cost) * N
            t = in_t + n * N
            lines[t].append((s, time))

    for i in range(N):
        cost, time = inpl()
        for n in range(0, M * A + 1):
            s = i + n * N
            if n + cost > M * A:
                t = i + (M * A) * N
            else:
                t = i + (n + cost) * N
            lines[s].append((t, time))

    def dijkstra(N, S):
        weight = [INF] * N
        weight[S] = 0
        q = [[0, S]]
        heapq.heapify(q)
        while q:
            w0, s = heapq.heappop(q)
            for t, w in lines[s]:
                w += w0
                if weight[t] > w:
                    heapq.heappush(q, [w, t])
                    weight[t] = w

        return weight

    weight = dijkstra(N * (M * A + 1), 0 + S * N)

    ansl = [INF] * N
    for i, w in enumerate(weight):
        n = i % N
        if n == 0:
            continue
        else:
            ansl[n] = min(w, ansl[n])

    print("\\n".join(map(str, ansl[1:])))


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import heapq


def inpl():
    return list(map(int, input().split()))


N, M, S = inpl()
A = 51  # max A
S = min(S, M * A - 1)

lines = [[] for _ in range(N * (M * A))]
for _ in range(M):
    in_s, in_t, cost, time = inpl()
    in_s -= 1
    in_t -= 1

    for n in range(cost, M * A):
        s = in_s + n * N
        t = in_t + (n - cost) * N
        lines[s].append((t, time))

        s = in_s + (n - cost) * N
        t = in_t + n * N
        lines[t].append((s, time))


for i in range(N):
    cost, time = inpl()
    for n in range(0, M * A):
        s = i + n * N
        if n + cost >= M * A:
            t = i + (M * A - 1) * N
        else:
            t = i + (n + cost) * N
        lines[s].append((t, time))


INF = 10**20


def dijkstra(N, S):
    weight = [INF] * N
    weight[S] = 0
    q = [[0, S]]
    heapq.heapify(q)
    while q:
        w0, s = heapq.heappop(q)
        for t, w in lines[s]:
            w += w0
            if weight[t] > w:
                heapq.heappush(q, [w, t])
                weight[t] = w

    return weight


weight = dijkstra(N * (M * A), 0 + S * N)

for i in range(1, N):
    ans = INF
    for n in range(M * A):
        ans = min(ans, weight[i + n * N])
    print(ans)
Problem p02703 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

N, M, S = map(int, input().split())

uvab = [list(map(int, input().split())) for _ in range(M)]
cd = [list(map(int, input().split())) for _ in range(N)]

for i in range(M):
    uvab[i][0] -= 1
    uvab[i][1] -= 1

railways = [[] for _ in range(N)]
max_a = 0
for u, v, a, b in uvab:
    railways[u].append((v, a, b))
    railways[v].append((u, a, b))
    max_a = max(max_a, a)
del uvab

max_silver = max_a * (N - 1)

ans = [-1] * N
d = dict()
d[(S, 0)] = 0  # silver, station
Q = [(0, S, 0)]  # time, silver, station
sum_reached = 0
reached = [False] * N

while Q:
    t, sv, st = heapq.heappop(Q)
    if not reached[st]:
        ans[st] = t
        reached[st] = True
        sum_reached += 1
        if sum_reached == N:
            break

    new_key = (min(max_silver, sv + cd[st][0]), st)
    new_value = t + cd[st][1]
    if new_key not in d or d[new_key] > new_value:
        d[new_key] = t + cd[st][1]
        heapq.heappush(Q, (new_value, *new_key))

    for dist, a, b in railways[st]:
        if sv >= a:
            new_key = (sv - a, dist)
            new_value = t + b
            if new_key not in d or d[new_key] > new_value:
                d[new_key] = new_value
                heapq.heappush(Q, (new_value, *new_key))

print(*ans[1:], sep="\\n")


Error: TLE

Fixed submission:

import heapq

N, M, S = map(int, input().split())

uvab = [list(map(int, input().split())) for _ in range(M)]
cd = [list(map(int, input().split())) for _ in range(N)]

for i in range(M):
    uvab[i][0] -= 1
    uvab[i][1] -= 1

railways = [[] for _ in range(N)]
max_a = 0
for u, v, a, b in uvab:
    railways[u].append((v, a, b))
    railways[v].append((u, a, b))
    max_a = max(max_a, a)
del uvab

max_silver = max_a * (N - 1)

ans = [-1] * N
d = dict()
d[(S, 0)] = 0  # silver, station
Q = [(0, S, 0)]  # time, silver, station
sum_reached = 0
reached = [False] * N

while Q:
    t, sv, st = heapq.heappop(Q)
    if not reached[st]:
        ans[st] = t
        reached[st] = True
        sum_reached += 1
        if sum_reached == N:
            break

    new_key = (sv + cd[st][0], st)
    new_value = t + cd[st][1]
    if new_key not in d or d[new_key] > new_value:
        d[new_key] = t + cd[st][1]
        heapq.heappush(Q, (new_value, *new_key))

    for dist, a, b in railways[st]:
        if sv >= a:
            new_key = (sv - a, dist)
            new_value = t + b
            if new_key not in d or d[new_key] > new_value:
                d[new_key] = new_value
                heapq.heappush(Q, (new_value, *new_key))

print(*ans[1:], sep="\\n")
Problem p02705 (Python) — buggy submission, verdict: Runtime Error

import math

r = int(input())
print(2 * r * math.pi)


Error: ValueError: not enough values to unpack (expected 2, got 1)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02705/Python/s499723334.py", line 1, in <module>
    n, m = map(int, input().split())
ValueError: not enough values to unpack (expected 2, got 1)


Fixed submission:

n, m = map(int, input().split())
a = list(map(int, input().split()))
if n - sum(a) >= 0:
    print(n - sum(a))
else:
    print(-1)
Problem p02705 (Python) — buggy submission, verdict: Runtime Error

import math

print(int(input()) * 2 * math.pi)


Error: TypeError: 'float' object is not callable
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02705/Python/s976752979.py", line 3, in <module>
    print(int(input()) * 2 * math.pi())
TypeError: 'float' object is not callable


Fixed submission:

import math

print(int(input()) * 2 * math.pi())
Problem p02705 (Python) — buggy submission, verdict: Runtime Error

# coding: utf-8


def main():
    R = int(input())

    return R * 2 * 3.1415


print(main())


Error: TypeError: can't multiply sequence by non-int of type 'float'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02705/Python/s656493829.py", line 9, in <module>
    print(main())
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02705/Python/s656493829.py", line 6, in main
    return R * 2 * 3.1415
TypeError: can't multiply sequence by non-int of type 'float'


Fixed submission:

# coding: utf-8


def main():
    R = input(int())
    return R * 2 * 3.1415


print(main())
Problem p02706 (Python) — buggy submission, verdict: Runtime Error

summer_vacation, amount_of_homework = map(int, input().split())
a = map(int, input().split())
homework_list = list(a)

total_days = 0

for i in homework_list:
    total_days += i

if summer_vacation >= total_days:
    print(summer_vacation - total_days)
else:
    print(-1)


Error: TypeError: 'types.GenericAlias' object is not iterable
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02706/Python/s080879362.py", line 7, in <module>
    for i in homework_list:
TypeError: 'types.GenericAlias' object is not iterable


Fixed submission:

summer_vacation, amount_of_homework = map(int, input().split())
a = map(int, input().split())
homework_list = list[a]

total_days = 0

for i in homework_list:
    total_days += i

if summer_vacation >= total_days:
    print(summer_vacation - total_days)
else:
    print(-1)
Problem p02706 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())
A = list(map(int, input().split()))

if n >= sum(A):
    print(n - sum(A))
else:
    print("-1")


Error: TypeError: 'int' object is not iterable
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02706/Python/s791039988.py", line 2, in <module>
    print(max(n - sum(list(map(int, input().split())))), -1)
TypeError: 'int' object is not iterable


Fixed submission:

n, m = map(int, input().split())
print(max(n - sum(list(map(int, input().split())))), -1)
Problem p02706 (Python) — buggy submission, verdict: Runtime Error

n, m = map(int, input().split())
a = list(map(int, input().split()))
ct = 0
for i in range(m):
    ct += a[i]

if n - ct >= 0:
    print(n - ct)
else:
    print(-1)


Error: TypeError: 'builtin_function_or_method' object is not iterable
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02706/Python/s941071070.py", line 1, in <module>
    n, m = map(int, input().split)
TypeError: 'builtin_function_or_method' object is not iterable


Fixed submission:

n, m = map(int, input().split)
a = list(map(int, input().split()))
ct = 0
for i in range(m):
    ct += a[i]

if n - ct >= 0:
    print(n - ct)
else:
    print(-1)
Problem p02706 (Python) — buggy submission, verdict: Runtime Error

# abc163B
# https://atcoder.jp/contests/abc163/tasks/abc163_b

# 夏休みはN日間
# 夏休みの宿題はM個
# 宿題は同じ日に複数できない、宿題の時は遊ぶことはできない
# 最大何日間遊ぶことが出来るか
# 夏休み中に終わらせられない場合は、"-1"

# 入力
n, m = map(int, input().split())
a = list(map(int, input().split()))

# 処理
# 宿題にかかる合計日数
answer = n - sum(a)

if answer < 0:
    print("-1")
else:
    print(answer)


Error: ValueError: invalid literal for int() with base 10: '41 2'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02706/Python/s871946506.py", line 11, in <module>
    n = int(input())
ValueError: invalid literal for int() with base 10: '41 2'


Fixed submission:

# abc163B
# https://atcoder.jp/contests/abc163/tasks/abc163_b

# 夏休みはN日間
# 夏休みの宿題はM個
# 宿題は同じ日に複数できない、宿題の時は遊ぶことはできない
# 最大何日間遊ぶことが出来るか
# 夏休み中に終わらせられない場合は、"-1"

# 入力
n = int(input())
m = int(input())
a = list(map(int, input().split()))

# 処理
answer = n - sum(a)

if answer > 0:
    print(answer)
else:
    print("-1")
Problem p02706 (Python) — buggy submission, verdict: Runtime Error

# ABC163B

n, m = map(int, input().split())
a = list(map(int, input().split()))

ans = n - sum(a)
if ans >= 0:
    print(ans)
else:
    print(-1)


Error: ValueError: invalid literal for int() with base 10: '5 6'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02706/Python/s373162453.py", line 4, in <module>
    a[i] = int(input())
ValueError: invalid literal for int() with base 10: '5 6'


Fixed submission:

n, m = map(int, input().split())
a = [0] * m
for i in range(m):
    a[i] = int(input())

ans = n - sum(a)
if ans > 0:
    print(ans)
else:
    print(-1)
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = Counter(A)

    for i in range(N):
        print(count[i + 1])


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))

    for i in range(N):
        print(Counter(A)[i + 1])


if __name__ == "__main__":
    main()
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
a = list(map(int, input().split()))

ans = [0] * n

for i in a:
    ans[i - 1] += 1

for i in range(n):
    print(ans[i])


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))

for i in range(n):
    print(a.count(i + 1))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
a = list(map(int, input().split()))
result = [0] * n
for x in a:
    result[x - 1] += 1
for v in result:
    print(v)


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    print(a.count(i + 1))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
a = list(int(i) for i in input().split())
lst = [0] * (n + 1)

for i in a:
    lst[i] += 1

for i in range(1, n + 1):
    print(lst[i])


Error: TLE

Fixed submission:

n = int(input())
a = list(int(i) for i in input().split())

for i in range(1, n + 1):
    print(a.count(i))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
A = list(map(int, input().split()))

# m = 0
# t = 0

# for i in range(1, n):
#     count = A.count(i)
#     print(count)
#     m += count
#     t = i
#     if m == n - 1:
#         break
# for _ in range(t, n):
#     print(0)

B = [0] * n
for i in A:
    B[i - 1] += 1
for i in B:
    print(i)


Error: TLE

Fixed submission:

n = int(input())
A = list(map(int, input().split()))

m = 0
t = 0

for i in range(1, n):
    count = A.count(i)
    print(count)
    m += count
    t = i
    if m == n - 1:
        break
for _ in range(t, n):
    print(0)
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
A = list(map(int, input().split()))

lis = [0] * N
for i in range(N - 1):
    lis[(A[i] - 1)] += 1

for i in range(N):
    print(lis[i])


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))
for i in range(1, N + 1):
    print(A.count(i))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
a = list(map(int, input().split()))
b = [0 for i in range(n)]

for i in a:
    b[i - 1] += 1

for i in b:
    print(i)


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    print(a.count(i + 1))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
A = list(map(int, input().split()))
B = [0 for i in range(N)]
for i in A:
    B[i - 1] += 1
for i in B:
    print(i)


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))
B = []
for i in range(1, N + 1):
    print(A.count(i))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))

    c = Counter(A)

    for i in range(1, N + 1):
        print(c[i])


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

def main():
    N = int(input())
    A = list(map(int, input().split()))

    for i in range(1, N + 1):
        print(A.count(i))


if __name__ == "__main__":
    main()
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
ls = list(map(int, input().split()))

ls2 = [0 for x in range(n)]
# 0がn個のリストを用意する

for x in ls:
    ls2[x - 1] += 1

for x in range(n):
    print(ls2[x])


Error: TLE

Fixed submission:

n = int(input())

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

rg = range(1, n + 1)

for i in rg:
    c = ls.count(i)
    print(c)
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
A_i = list(map(int, input().split()))
C = [0] * N
for i in A_i:
    C[i - 1] += 1
print(*C)


Error: TLE

Fixed submission:

N = int(input())
A_i = list(map(int, input().split()))
uq_A_i = list(set(A_i))
C = ["0\\n"] * N
for i in uq_A_i:
    C[i - 1] = "{}".format(A_i.count(i)) + "\\n"
print("".join(C))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
a = map(int, input().split())

a = list(a)
result = [0] * n
for i in range(n - 1):
    result[a[i] - 1] += 1

print(*result, sep="\\n")


Error: TLE

Fixed submission:

n = int(input())
a = map(int, input().split())

a = list(a)
for i in range(n):
    count = 0
    for j in range(i, n - 1):
        if a[j] == i + 1:
            count += 1
    print(count)
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter

n = int(input())
a = list(map(int, input().split()))

c = Counter(a)

for i in range(1, n + 1):
    if i in c:
        print(c[i])
    else:
        print(0)


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))

for i in range(1, n + 1):
    print(a.count(i))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

n = int(input())
a = list(map(int, input().split()))

c = collections.Counter(a)

for i in range(1, n + 1):
    if i in c:
        print(c[i])
    else:
        print(0)


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))

for i in range(1, n + 1):
    print(a.count(i))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
a = list(map(int, input().split()))
li = [0] * N

for i in range(N - 1):
    li[a[i] - 1] = li[a[i] - 1] + 1

for i in range(N):
    print(li[i])


Error: TLE

Fixed submission:

N = int(input())
a = list(input().split())
for i in range(N):
    s = str(i + 1)
    print(a.count(s))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
li_a = list(map(int, input().split()))

dic = {}
for a in li_a:
    dic[a] = dic.get(a, 0) + 1

for i in range(1, n + 1):
    print(dic.get(i, 0))


Error: TLE

Fixed submission:

n = int(input())
li_a = list(map(int, input().split()))

set_a = set(li_a)

for i in range(1, n + 1):
    if i in set_a:
        print(li_a.count(i))
    else:
        print(0)
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())

line = list(map(int, input().strip().split(" ")))

ans = [0 for s in range(n)]

for i in line:
    ans[i - 1] += 1

for i in range(n):
    print(ans[i])


Error: TLE

Fixed submission:

n = int(input())

line = list(map(int, input().strip().split(" ")))

for i in range(1, n + 1):
    print(line.count(i))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
for i in range(1, N + 1):
    if c[i]:
        print(c[i])
    else:
        print(0)


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))
for r in [A.count(i) for i in range(1, N + 1)]:
    print(r)
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
s = [0 for i in range(N)]
A = list(map(int, input().split()))
for i in A:
    s[i - 1] += 1
print(*s, sep="\\n")


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))
for i in range(N):
    print(A.count(i + 1))
Problem p02707 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

num_of_employee = int(input())
employeenos = list(map(int, input().split()))

c = collections.Counter(employeenos)
for i in range(1, num_of_employee + 1):
    print(c[i])


Error: TLE

Fixed submission:

num_of_employee = int(input())
employeenos = list(map(int, input().split()))

for i in range(1, num_of_employee + 1):
    print(employeenos.count(i))
Problem p02709 (Python) — buggy submission, verdict: Time Limit Exceeded

from sys import stdin


def main():
    n = int(stdin.readline())
    a = list(map(int, stdin.readline().split()))

    a2 = [[a[i], i] for i in range(n)]
    a2.sort(reverse=True)

    dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]

    ans = 0

    for i in range(n + 1):
        for j in range(n + 1 - i):
            s1 = s2 = 0
            if i > 0:
                s1 = dp[i - 1][j] + a2[i + j - 1][0] * (a2[i + j - 1][1] - (i - 1))
            if j > 0:
                s2 = dp[i][j - 1] + a2[i + j - 1][0] * ((n - j) - a2[i + j - 1][1])
            dp[i][j] = max(s1, s2)
        ans = max(ans, dp[i][n - i])

    print(ans)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import numpy as np

from sys import stdin


def main():
    n = int(stdin.readline())
    a = list(map(int, stdin.readline().split()))

    a2 = [[a[i], i] for i in range(n)]
    a2.sort(reverse=True)

    dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]

    ans = 0

    for i in range(n + 1):
        for j in range(n + 1 - i):
            s1 = s2 = 0
            if i > 0:
                s1 = dp[i - 1][j] + a2[i + j - 1][0] * (a2[i + j - 1][1] - (i - 1))
            if j > 0:
                s2 = dp[i][j - 1] + a2[i + j - 1][0] * ((n - j) - a2[i + j - 1][1])
            dp[i][j] = np.max([s1, s2])
        ans = np.max([ans, dp[i][n - i]])

    print(ans)


if __name__ == "__main__":
    main()
Problem p02709 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
A = list(map(int, input().split()))

A = sorted([(val, pos) for pos, val in enumerate(A)], reverse=True)

dp = [0]
for i, (val, pos) in enumerate(A):
    ldp = [e + val * abs(pos - (i - r)) for r, e in enumerate(dp)]
    rdp = [e + val * abs(N - 1 - r - pos) for r, e in enumerate(dp)]
    dp = [max(L, R) for L, R in zip(ldp + [0], [0] + rdp)]

print(max(dp))


Error: 0

Fixed submission:

from copy import deepcopy

N = int(input())
A = list(map(int, input().split()))

index = [i for i in range(N)]
index = sorted(index, key=lambda i: A[i], reverse=True)


def LorR(index, res):
    i = index[0]
    dist = max(i, N - 1 - i)
    end = False
    cand1 = cand2 = 0
    while not end:
        j = i + dist
        if 0 <= j and j < N and not res[j]:
            r = deepcopy(res)
            r[j] = True
            cand1 = A[i] * dist + loop(index[1:], r)
            end = True

        j = i - dist
        if 0 <= j and j < N and not res[j]:
            r = deepcopy(res)
            r[j] = True
            cand2 = A[i] * dist + loop(index[1:], r)
            end = True

        dist -= 1
    return max(cand1, cand2)


def loop(index, res):
    if not index:
        return 0

    return LorR(index, res)


print(loop(index, [False] * N))
Problem p02709 (Python) — buggy submission, verdict: Time Limit Exceeded

# 解説AC
import sys

input = sys.stdin.buffer.readline

n = int(input())
A = list(map(int, input().split()))
B = []
for i, e in enumerate(A):
    B.append((e, i + 1))
B.sort(reverse=True)
# dp[i][j]: Aiまで入れた時、左にj個決めた時の最大値
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0

for i in range(n):
    for j in range(i + 1):  # 左の個数
        k = i - j  # 右の個数
        ni = i + 1
        val, idx = B[i]
        dp[ni][j] = max(dp[ni][j], dp[i][j] + abs(n - k - idx) * val)
        dp[ni][j + 1] = max(dp[ni][j + 1], dp[i][j] + abs(idx - (j + 1)) * val)
ans = 0
for i in range(n + 1):
    ans = max(ans, dp[n][i])
print(ans)


Error: TLE

Fixed submission:

# 解説AC
n = int(input())
A = list(map(int, input().split()))
B = []
for i, e in enumerate(A):
    B.append((e, i + 1))
B.sort(reverse=True)
# dp[i][j]: Aiまで入れた時、左にj個決めた時の最大値
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0

for i in range(n):
    for j in range(i + 1):  # 左の個数
        k = i - j  # 右の個数
        ni = i + 1
        val, idx = B[i]
        dp[ni][j] = max(dp[ni][j], dp[i][j] + abs(n - k - idx) * val)
        dp[ni][j + 1] = max(dp[ni][j + 1], dp[i][j] + abs(idx - (j + 1)) * val)
ans = 0
for i in range(n + 1):
    ans = max(ans, dp[n][i])
print(ans)