qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, T = map(int, input().split())
    dp = [0] * (T + 1)
    AB = []
    for k in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))

    AB.sort()
    maxdp = 0
    temp = 1
    for i in range(N):
        a, b = AB[i]  # a:time  b:value
        temp += a
        dp[T] = max(maxdp + b, dp[T])
        for j in range(min(T - 1, temp), a - 1, -1):
            if dp[j] < dp[j - 1] + b:
                dp[j] = dp[j - a] + b
                maxdp = max(dp[j], maxdp)

    print(dp[T])


if __name__ == "__main__":
    main()


Fixed code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


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

    dp = [0] * (T + 1)

    AB = []

    for k in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))

    AB.sort()
    maxdp = 0

    for i in range(N):
        a, b = AB[i]  # a:time  b:value

        dp[T] =max( maxdp + b,dp[T])
        for j in range(T - 1, a - 1, -1):
            dp[j] = max(dp[j], dp[j - a] + b)
            maxdp= max(dp[j],maxdp)
    print(dp[T])

if __name__ == "__main__":
    main()
Buggy code:
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)



def solve():
    n, t = getList()
    li = []
    for i in range(n):
        li.append(getList())

    li.sort(key=lambda x: 30000 * x[0] - x[1])
    dp = [-1] * (t + 1)
    dp[0] = 0
    for a, b in li[:-1]:
        tmp = copy.copy(dp)
        for idx, dd in enumerate(dp):
            # tmp[idx] = max(tmp[idx], dd)
            if dd != -1 and idx + a <= t:
                tmp[idx + a] = max(dp[idx + a], dp[idx] + b)
        dp = tmp
        for j in range(len(dp) - 1):
            dp[j+1] = max(dp[j], dp[j+1])
        # print(dp)
    # print(li)
    print((dp[t-1]) + li[-1][1])





def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()







Fixed code:
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)



def solve():
    n, t = getList()
    li = []
    for i in range(n):
        li.append(getList())

    li.sort(key=lambda x: 30000 * x[0] + x[1])
    dp = [0] * t
    ans = 0
    for a, b in li:
        tmp = copy.copy(dp)
        ans = max(ans, dp[-1] + b)
        for idx, dd in enumerate(dp):
            # tmp[idx] = max(tmp[idx], dd)
            if idx + a < t:
                tmp[idx + a] = max(dp[idx + a], dp[idx] + b)
        dp = tmp
        for j in range(len(dp) - 1):
            dp[j+1] = max(dp[j], dp[j+1])
    #     print(dp)
    # print(li)
    print(ans)





def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()
Buggy code:
n,t = map(int,input().split())
ab = []
for i in range(n):
    a,b = map(int, input().split())
    ab += [(a,b)]
ab.sort()
m = ab[-1][0]
dp = [[0]*(t+m) for _ in range(n)]
for i in range(t):
    dp[0][i+ab[0][0]] = ab[0][1]

for i in range(1, n):
    a, b = ab[i]
    for j in range(t):
        dp[i][j+a] = max(dp[i-1][j] + b, dp[i-1][j+a])

ans = 0
for i in range(n):
    ans = max(ans, dp[i][t-1] + ab[i][1])
print(ans)

Fixed code:
n,t=map(int,input().split())
dp=[[0]*(t+3001)for _ in range(n+1)]
ans=0
ab=[list(map(int,input().split()))for _ in range(n)]
ab.sort()
for i in range(1,n+1):
    a,b=ab[i-1]
    for j in range(t):
        dp[i][j]=max(dp[i-1][j],dp[i][j])
        dp[i][j+a]=dp[i-1][j]+b
        ans=max(dp[i][j],dp[i][j+a],ans)
print(ans)
Buggy code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort()
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]

    dp1 = [[0]*(T) for _ in range(N)]
    for i in range(N-1):
        for j in range(T):
            pt = j - A[i]
            if 0 <= pt:
                dp1[i+1][j] = max(dp1[i][j], dp1[i][pt] + B[i])
            else:
                dp1[i+1][j] = dp1[i][j]

    ans = dp1[N-1][T-1] + B[-1]
    print(ans)


if __name__ == '__main__':
    main()


Fixed code:
def main():
    N, T = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort()
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]

    dp1 = [[0]*(T+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(T):
            dp1[i+1][j] = max(dp1[i+1][j], dp1[i][j])

            t = j - A[i]
            if 0 <= t:
                dp1[i+1][j] = max(dp1[i+1][j], dp1[i][t] + B[i])
    ans = 0
    for i in range(N):
        ans = max(ans, dp1[i][T-1] + B[i])
    print(ans)


if __name__ == '__main__':
    main()
Buggy code:

N, T = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]

Dp = [0 for _ in range(T+1)]

ab = sorted(ab, key = lambda x: x[0])

for i in range(N-1):
    for j in range(T-1,-1, -1):
        if j+ab[i][0] <= T-1:
            kari2 = Dp[j] + ab[i][1]
            if  Dp[j+ab[i][0]] < kari2:
                Dp[j+ab[i][0]] = kari2
print(Dp[T-1]+ab[-1][1])

Fixed code:
N, T = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]

Dp = [0 for _ in range(T+1)]

ab = sorted(ab, key = lambda x: x[0])

for i in range(N):
    for j in range(T-1,-1, -1):
        if j+ab[i][0] <= T-1:
            kari2 = Dp[j] + ab[i][1]
            if  Dp[j+ab[i][0]] < kari2:
                Dp[j+ab[i][0]] = kari2
        else:
            kari2 = Dp[j] +ab[i][1]
            if Dp[T] < kari2:
                Dp[T] = kari2
                
print(max(Dp))
Buggy code:
n,t = map(int,input().split())
orders = []
for i in range(n):
    a,b = map(int,input().split())
    orders.append([a,b])

orders.sort(key=lambda x:(x[0],-x[1]))
# print(orders)

dp = [[-1e10 for i in range(t+1)] for j in range(n+1)]
for i in range(n+1):
    dp[i][0] = 0

for i,od in enumerate(orders):
    for j in range(t):
        nt = j+od[0]
        if nt > t-1:
            nt = t
        temp = max(dp[i][j]+od[1],dp[i][nt])
        dp[i+1][nt] =temp

# for l in dp:
#     print(l[:11])
# print(dp[-1])
print(max(dp[-1]))

Fixed code:
n,t = map(int,input().split())
orders = []
for i in range(n):
    a,b = map(int,input().split())
    orders.append([a,b])

orders.sort(key=lambda x:(x[0],-x[1]))
# print(orders)

dp = [[-1e10 for i in range(t+1)] for j in range(n+1)]
dp[0][0] = 0

for i,od in enumerate(orders):
    for j in range(t+1):
        dp[i+1][j] = dp[i][j]
    for j in range(t):
        nt = j+od[0]
        if nt > t-1:
            nt = t
        if dp[i+1][nt] < dp[i][j]+od[1]:
            dp[i+1][nt] = dp[i][j]+od[1]

# for l in dp:
#     print(l[:11])
# print(dp[-1])
print(max(dp[-1]))
Buggy code:
n,t = map(int,input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
point = ab[-1][1]
n -= 1
dp = [[0 for i in range(t+1)]for j in range(n+1)]
for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])
print(dp[-1][t-1]+point)

Fixed code:
n,t = map(int,input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])

ma = 0
for i,(ti,vi) in enumerate(ab):
    ma = max(ma,dp[i][t-1]+vi)
print(ma)
Buggy code:
n,t = map(int,input().split())
ab = sorted([list(map(int, input().split())) for i in range(n)])
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        else:
            dp[i+1][j] = max(dp[i][j],dp[i+1][j])

ans = 0
for i, (ti, vi) in enumerate(ab):
    ans = max(ans, dp[i][t - 1] + vi)
print(ans)


Fixed code:
n,t = map(int,input().split())
ab = sorted([list(map(int, input().split())) for i in range(n)])
dp = [[0 for i in range(t+1)]for j in range(n+1)]

for i in range(n):
    ti,vi = ab[i]
    for j in range(t+1):
        if j + ti <= t:
            dp[i+1][j+ti] = max(dp[i+1][j+ti],dp[i][j]+vi)
        dp[i+1][j] = max(dp[i][j],dp[i+1][j])

ans = 0
for i, (ti, vi) in enumerate(ab):
    ans = max(ans, dp[i][t - 1] + vi)
print(ans)
Buggy code:
n,t = map(int,input().split())
ab = [[0,0] for _ in range(n)]
for i in range(n):
    ab[i] = list(map(int,input().split()))
dp = [[0]*t for i in range(n+1)]
ab.sort()
ans = 0
for i in range(n):
    a,b = ab[i]
    for j in range(t)[::-1]:
        v = dp[i][j]
        ans = max(ans,v+b)
        dp[i+1][j] = max(dp[i+1][j], dp[j][i])
        if j + a >= t:continue
        dp[i+1][j+a] = max(dp[i][j+a], v+b)
print(ans)


Fixed code:
n,t = map(int,input().split())
ab = [[0,0] for _ in range(n)]
for i in range(n):
    ab[i] = list(map(int,input().split()))
dp = [[0]*t for i in range(n+1)]
ab.sort()
ans = 0
for i in range(n):
    a,b = ab[i]
    for j in range(t)[::-1]:
        v = dp[i][j]
        ans = max(ans,v+b)
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        if j + a >= t:continue
        dp[i+1][j+a] = max(dp[i][j+a], v+b)
print(ans)
Buggy code:
def main():
    import sys
    input = sys.stdin.readline
    N, T = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(N)]
    ab.sort(key=lambda x: x[1])
    dp = [-1 for i in range(T*2 + 1)]
    dp[0] = 0
    for a, b in ab:
        for i in range(T*2 + 1, 0, -1):
            if i >= a:
                if dp[i-a] >= 0:
                    dp[i] = max(dp[i], dp[i-a] + b)
    for i in range(1, T*2+1):
        dp[i] = max(dp[i], dp[i-1])
    ans = dp[-1]

    # print(dp)
    print(ans)


main()


Fixed code:
def main():
    import sys
    input = sys.stdin.readline
    N, T = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(N)]
    ab.sort(key=lambda x: (x[0], x[1]))
    dp = [-1 for i in range(T*2 + 1)]
    dp[0] = 0
    for a, b in ab:
        for i in range(T*2, 0, -1):
            if i >= a:
                if dp[i-a] >= 0 and i - a < T:
                    dp[i] = max(dp[i], dp[i-a] + b)

    for i in range(1, T*2+1):
        dp[i] = max(dp[i], dp[i-1])
    ans = dp[-1]

    ab.sort(key=lambda x: x[1])
    tmp = ab.pop()
    dp = [-1 for i in range(T)]
    dp[0] = 0
    for a, b in ab:
        for i in range(T-1, 0, -1):
            if i >= a:
                if dp[i-a] >= 0:
                    dp[i] = max(dp[i], dp[i-a] + b)

    for i in range(1, T):
        dp[i] = max(dp[i], dp[i-1])
    ans = max(ans, tmp[1] + dp[-1])

    # print(dp)
    print(ans)


main()
Buggy code:
n,t = map(int,input().split())
a = [list(map(int,input().split())) for i in range(n)]
a.sort()
DP = [[0 for i in range(t+3001)] for j in range(n+1)]
for i in range(1,n+1):
  for j in range(1,t+3001):
    if j-a[i-1][0] >= 0:
      DP[i][j] = max(DP[i-1][j],DP[i-1][j-a[i-1][0]]+a[i-1][1])
    else:
      DP[i][j] = DP[i-1][j]
ans = 0
for i in range(n):
  ans = max(ans,DP[i][t-1]+a[i][1])
  print(ans)

Fixed code:
n,t = map(int,input().split())
a = [list(map(int,input().split())) for i in range(n)]
a.sort()
DP = [[0 for i in range(t+3001)] for j in range(n+1)]
for i in range(1,n+1):
  for j in range(1,t+3001):
    if j-a[i-1][0] >= 0:
      DP[i][j] = max(DP[i-1][j],DP[i-1][j-a[i-1][0]]+a[i-1][1])
    else:
      DP[i][j] = DP[i-1][j]
ans = 0
for i in range(n):
  ans = max(ans,DP[i][t-1]+a[i][1])
print(ans)
Buggy code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l.sort()
dp=[[0]*(t+1) for i in range(n+1)]
for i in range(1,n+1):
  for j in range(t):
    if j+l[i-1][0]<t:
      dp[i][j+l[i-1][0]]=max(dp[i][j+l[i-1][0]],dp[i-1][j]+l[i-1][1])
    else:
      dp[i][t]=max(dp[i][t],dp[i-1][j]+l[i-1][1])
    dp[i][j]=max(dp[i-1][j],dp[i][j])
print(max(dp[n]))

Fixed code:
n,t=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
l.sort()
dp=[[0]*(t+1) for i in range(n+1)]
for i in range(1,n+1):
  for j in range(t+1):
    if j!=t:
      if j-l[i-1][0]>=0:
        dp[i][j]=max(dp[i-1][j],dp[i-1][j-l[i-1][0]]+l[i-1][1])
    else:
      dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+l[i-1][1])
    dp[i][j]=max(dp[i-1][j],dp[i][j])
print(max(dp[n]))
Buggy code:
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda x: x[1])
#print(AB)

W = T + 1
def dp(AB,W):
    N = len(AB)
    DP = [[0]*W for i in range(N+1)]
    for i in range(N):
        for w in range(W):
            if w != W:
                if AB[i][0] <= w:
                    DP[i+1][w] = max(DP[i][w-AB[i][0]]+AB[i][1],DP[i][w])
                else
                    DP[i+1][W] = DP[i][w]
            else:
                DP[i+1][w] = max(DP[i][w-1]+AB[i][1],DP[i][w])
    return DP

DP1 = dp(AB,W)

ans = DP1[N][W]
print(ans)

Fixed code:
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda x: x[0])
#print(AB)

W = T + 1
def dp(AB,W):
    N = len(AB)
    DP = [[0]*W for i in range(N+1)]
    for i in range(N):
        for w in range(W):
            if w != W-1:
                if AB[i][0] <= w:
                    DP[i+1][w] = max(DP[i][w-AB[i][0]]+AB[i][1],DP[i][w])
                else:
                    DP[i+1][w] = DP[i][w]
            else:
                DP[i+1][w] = max(DP[i][w-1]+AB[i][1],DP[i][w])
    return DP

DP1 = dp(AB,W)

ans = DP1[N][T]
print(ans)
Buggy code:
N,T = map(int,input().split())
tlist=[0]*N
vlist=[0]*N
tvlist=[[0,0]]*N
dp=[[0]*T for i in range(N+1)]

for i in range(N):
    tvlist[i] = list(map(int,input().split()))
tvlist.sort(key=lambda x: x[0])
for i in range(N):
    tlist[i] = tvlist[i][0]
    vlist[i] = tvlist[i][1]

for i in range(N):
    for t in range(T):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

        print(i+1,t,dp[i+1][t])

ans=0
for i in range(N):
    ans = max(ans,dp[i][T-1]+tvlist[i][1])

print(ans)

Fixed code:
N,T = map(int,input().split())
tlist=[0]*N
vlist=[0]*N
tvlist=[[0,0]]*N
dp=[[0]*T for i in range(N+1)]

for i in range(N):
    tvlist[i] = list(map(int,input().split()))
tvlist.sort(key=lambda x: x[0])
for i in range(N):
    tlist[i] = tvlist[i][0]
    vlist[i] = tvlist[i][1]

for i in range(N):
    for t in range(T):
        if t>=tlist[i]:
            dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
        else:
            dp[i+1][t] = dp[i][t]

ans=0
for i in range(N):
    ans = max(ans,dp[i][T-1]+vlist[i])

print(ans)
Buggy code:
n,t = map(int,input().split())
dpa = [[0 for i in range(t)] for j in range(n+1)]
dpb = [[0 for i in range(t)] for j in range(n+1)]

for i in range(t):
   dpa[0][i] = 0
   dpb[0][i] = 0
abl = []
for _ in range(n):
   a,b = map(int,input().split())
   abl.append((a,b))

for i in range(n-1):
   for j in range(t):
      if j - abl[i][0] >= 0:
         dpa[i+1][j] = max(dpa[i][j], dpa[i][j-abl[i][0]] + abl[i][1])

abl = abl[::-1]
for i in range(n-1):
   for j in range(t):
      if j - abl[i][0] >= 0:
         dpb[i+1][j] = max(dpb[i][j], dpb[i][j-abl[i][0]] + abl[i][1])
abl = abl[::-1]
ans = 0
for i in range(t):
   for j in range(n):
      ans = max(ans, dpa[j][i]+dpb[n-j-1][t-i-1]+abl[j][1])

print(ans)

Fixed code:
n,t = map(int,input().split())
dpa = [[0 for i in range(t)] for j in range(n+1)]
dpb = [[0 for i in range(t)] for j in range(n+1)]

for i in range(t):
   dpa[0][i] = 0
   dpb[0][i] = 0
abl = []
for _ in range(n):
   a,b = map(int,input().split())
   abl.append((a,b))

for i in range(n-1):
   for j in range(t):
      if j - abl[i][0] >= 0:
         dpa[i+1][j] = max(dpa[i][j], dpa[i][j-abl[i][0]] + abl[i][1])
      else:
         dpa[i + 1][j] = max(dpa[i][j], dpa[i + 1][j])

abl = abl[::-1]
for i in range(n-1):
   for j in range(t):
      if j - abl[i][0] >= 0:
         dpb[i+1][j] = max(dpb[i][j], dpb[i][j-abl[i][0]] + abl[i][1])
      else:
         dpb[i + 1][j] = max(dpb[i][j], dpb[i + 1][j])

abl = abl[::-1]
ans = 0
for i in range(t):
   for j in range(n):
      ans = max(ans, dpa[j][i]+dpb[n-j-1][t-i-1]+abl[j][1])

print(ans)
Buggy code:
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

AB.sort()
dp = [0] * T
#print("AB:", AB)

answer = 0
for a, b in AB:
  for t in range(T-1, -1, -1):
    if dp[t]:
      if  a + t < T:
        dp[a + t] = max(dp[a + t], dp[t] + b)
        answer = max(answer, dp[a + t])
      else:
        answer = max(answer, dp[t] + b)
  if a < T:
    dp[a] = max(dp[a], b)
  answer = max(answer, b)
  #print("dp:", dp)
print(answer)      N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

AB.sort()
dp = [0] * T
#print("AB:", AB)

answer = 0
for a, b in AB:
  for t in range(T-1, -1, -1):
    if dp[t]:
      if  a + t < T:
        dp[a + t] = max(dp[a + t], dp[t] + b)
        answer = max(answer, dp[a + t])
      else:
        answer = max(answer, dp[t] + b)
  if a < T:
    dp[a] = max(dp[a], b)
  answer = max(answer, b)
  #print("dp:", dp)
print(answer)      

Fixed code:
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

AB.sort()
dp = [0] * T
#print("AB:", AB)

answer = 0
for a, b in AB:
  for t in range(T-1, -1, -1):
    if dp[t]:
      if  a + t < T:
        dp[a + t] = max(dp[a + t], dp[t] + b)
        answer = max(answer, dp[a + t])
      else:
        answer = max(answer, dp[t] + b)
  if a < T:
    dp[a] = max(dp[a], b)
  answer = max(answer, b)
  #print("dp:", dp)
print(answer)
Buggy code:
import sys
input = sys.stdin.readline
n, t = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
dp = [[0 for j in range(t+1)] for i in range(n+1)]

for i in range(n):
    for j in range(t):
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    for j in range(t):
        if j < t:
            next = min(j + ab[i][0], t)
            dp[i+1] [next] = max(dp[i+1][next], dp[i][j] + ab[i][1])
print(max(dp[n]))


Fixed code:
import sys
input = sys.stdin.readline
n, t = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()

dp = [[0 for j in range(t+1)] for i in range(n+1)]

for i in range(n):
    for j in range(t+1):
        dp[i+1][j] =  dp[i][j]
    for j in range(t):
        next = min(j + ab[i][0], t)
        dp[i+1] [next] = max(dp[i+1][next], dp[i][j] + ab[i][1])
print(max(dp[n]))
Buggy code:
def solve():
    from sys import stdin
    #f_i = stdin
    f_i = open('ABC_145_E_in_s4')#
    
    N, T = map(int, f_i.readline().split())
    AB = [tuple(map(int, f_i.readline().split())) for i in range(N)]
    AB.sort()
    
    dp = [[0] * T for i in range(N + 1)]
    
    for i, AB_i in enumerate(AB, start=1):
        A_i, B_i = AB_i
        dp_i = dp[i]
        dp_pre = dp[i-1]
        for j, t in enumerate(zip(dp_pre[A_i:], dp_pre), start=A_i):
            x, y = t
            if x < y + B_i:
                dp_i[j] = y + B_i
            else:
                dp_i[j] = x
    
    ans = max(dp[k][-1] + AB[k][1] for k in range(N))
    ans = max(ans, dp[-1][-1])
    print(ans)

solve()

Fixed code:
def solve():
    from sys import stdin
    f_i = stdin
    
    N, T = map(int, f_i.readline().split())
    AB = [tuple(map(int, f_i.readline().split())) for i in range(N)]
    AB.sort()
    
    dp = [[0] * T for i in range(N + 1)]
    
    for i, AB_i in enumerate(AB, start=1):
        A_i, B_i = AB_i
        dp_i = dp[i]
        dp_pre = dp[i-1]
        dp_i[:A_i] = dp_pre[:A_i]
        for j, t in enumerate(zip(dp_pre[A_i:], dp_pre), start=A_i):
            x, y = t
            if x < y + B_i:
                dp_i[j] = y + B_i
            else:
                dp_i[j] = x
    
    ans = max(dp[k][-1] + AB[k][1] for k in range(N))
    print(ans)

solve()
Buggy code:
n,t=map(int,input().split())
dp=[[-1 for j in range(2)] for i in range(t+1)]
dp[0][0]=0
d=[]
for i in range(n):
    d.append(list(map(int,input().split())))
for i in range(n):
    for j in range(t,-1,-1):
        for k in range(1,-1,-1):
            if k==0:
                if dp[j][k]>=0 and j+d[i][0]<=t:
                    dp[j+d[i][0]][k]=max(dp[j+d[i][0]][k],dp[j][k]+d[i][1])
            if k==1:
                if dp[j][0]>=0:
                    dp[j][k]=max(dp[j][k],dp[j][0]+d[i][1])
ans=0
for i in range(t+1):
    for j in range(2):
        ans=max(ans,dp[i][j])
print(ans)


Fixed code:
n,t=map(int,input().split())
dp=[[-1 for j in range(2)] for i in range(t+1)]
dp[0][0]=0
d=[]
for i in range(n):
    d.append(list(map(int,input().split())))
d.sort()
for i in range(n):
    for j in range(t,-1,-1):
        for k in range(1,-1,-1):
            if k==0:
                if dp[j][k]>=0 and j+d[i][0]<t:
                    dp[j+d[i][0]][k]=max(dp[j+d[i][0]][k],dp[j][k]+d[i][1])
            if k==1:
                if dp[j][0]>=0:
                    dp[j][k]=max(dp[j][k],dp[j][0]+d[i][1])
ans=0
for i in range(t+1):
    for j in range(2):
        ans=max(ans,dp[i][j])
print(ans)
Buggy code:
n, t = map(int, input().split())
w = []
v = []
for i in range(n):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
dp = [[0] * (t + 1) for i in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
    if j >= w[i]:
        dp[i + 1][t] = dp[i + 1][t] = max(dp[i][t - 1] + v[i], dp[i][t])
    else:
        dp[i + 1][t] = dp[i][t]
print(dp[n][t])


Fixed code:
n, t = map(int, input().split())
w = []
v = []
for i in range(n):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w, v = zip(*sorted(zip(w, v)))
dp = [[0] * (t + 1) for i in range(n + 1)]
for i in range(n):
    for j in range(t):
        if j >= w[i]:
            dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
    dp[i + 1][t] = dp[i + 1][t] = max(dp[i][t - 1] + v[i], dp[i][t])
print(dp[n][t])