Buggy python submission (verdict: Runtime Error):
f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")


N, T = map(int, f.readline().split())
ab = [list(map(int, f.readline().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
        else:
            kari2 = Dp[j] +ab[i][1]
            if Dp[T] < kari2:
                Dp[T] = kari2
                
print(max(Dp))

Fixed submission:
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 python submission (verdict: Runtime Error):
f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")


N, T = map(int, f.readline().split())
ab = [list(map(int, f.readline().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 submission:
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 python submission (verdict: Runtime Error):
def function():
    N, T = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(N)]
    
    Dp = [0 for _ in range(T+1)]
    used_ab = [set() for _ in range(T+1)]
    
    
    for i in range(N):
        for j in range(T-1,-1, -1):
            if j+AB[i][0] <= T-1:
                kari1 = Dp[j+AB[i][0]]
                kari2 = Dp[j] + AB[i][1]
                if  kari1< kari2:
                    Dp[j+AB[i][0]] = kari2
                    kari = used_ab[j].copy()
                    kari.add(i)
                    used_ab[j+AB[i][0]] = kari
    
    answer = 0
    for i in range(T):
        max_amari = 0
        for j in range(N):
            if j not in used_ab[i] and max_amari < AB[j][1]:
                max_amari = AB[j][1]
        if Dp[i]+max_amari > answer:
            answer = Dp[i]+max_amari
    
    print(answer)
    
if __name__ = "__main__":
    function()

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