Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n = int(input())
    wsv = LLI(n)
    wsv.sort(key=lambda x: x[0] + x[1], reverse=True)
    m = sum(w for w, s, v in wsv) + 1
    dp = [0] * m
    # print(wsv)
    # print(dp)
    for w, s, v in wsv:
        for j in range(w, m):
            nj = min(j - w, s)
            dp[nj] = max(dp[nj], dp[j] + v)
    # print(dp)
    print(max(dp))

main()


Fixed submission:
def f(n):
    dp = [0] * (n * 10 ** 4 + 1)
    bs = [list(map(int, input().split())) for _ in range(n)]
    bs.sort(key=lambda x: x[0] + x[1])
    for w, s, v in bs:
        for i in range(w + s, w - 1, -1):
            dpw = dp[i - w] + v
            if dpw > dp[i]:
                dp[i] = dpw
    print(max(dp))


n = int(input())
f(n)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n = int(input())
    wsv = LLI(n)
    wsv.sort(key=lambda x: x[0] + x[1], reverse=True)
    m = sum(w for w, s, v in wsv) + 1
    dp = [0] * m
    # print(wsv)
    # print(dp)
    for w, s, v in wsv:
        for j in range(w, m):
            nj = min(j - w, s)
            dp[nj] = max(dp[nj], dp[j] + v)
    # print(dp)
    print(max(dp))

main()


Fixed submission:
def f(n):
    dp = [0] * (n * 10 ** 4 + 1)
    bs = [list(map(int, input().split())) for _ in range(n)]
    bs.sort(key=lambda x: x[0] + x[1])
    for w, s, v in bs:
        for i in range(w + s, w - 1, -1):
            dpw = dp[i - w] + v
            if dpw > dp[i]:
                dp[i] = dpw
    print(max(dp))


n = int(input())
f(n)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n = int(input())
    wsv = LLI(n)
    wsv.sort(key=lambda x: x[0] + x[1], reverse=True)
    dp = {100000000: 0}
    # print(wsv)
    # print(dp)
    for w, s, v in wsv:
        for cap, value in sorted(dp.items()):
            nc = min(cap - w, s)
            if nc<0:continue
            if nc in dp:
                dp[nc] = max(dp[nc], value + v)
            else:
                dp[nc] = value + v
    # print(dp)
    print(max(dp.values()))

main()


Fixed submission:
def f(n):
    dp = [0] * (n * 10 ** 4 + 1)
    bs = [list(map(int, input().split())) for _ in range(n)]
    bs.sort(key=lambda x: x[0] + x[1])
    for w, s, v in bs:
        for i in range(w + s, w - 1, -1):
            dpw = dp[i - w] + v
            if dpw > dp[i]:
                dp[i] = dpw
    print(max(dp))


n = int(input())
f(n)

Buggy python submission (verdict: Time Limit Exceeded):
def f(n):
    mx = 0
    dp = [0] * (n * 10 ** 4 + 1)
    bs = [list(map(int, input().split())) for _ in range(n)]
    bs.sort(key=lambda x: x[0] + x[1])
    for w, s, v in bs:
        for i, dpv in enumerate(dp[s::-1]):
            nv = dpv + v
            ni = s + w - i
            if nv > dp[ni]:
                dp[ni] = nv
                if nv > mx:
                    mx = nv
    print(mx)


n = int(input())
f(n)


Fixed submission:
def f(n):
    dp = [0] * (n * 10 ** 4 + 1)
    bs = [list(map(int, input().split())) for _ in range(n)]
    bs.sort(key=lambda x: x[0] + x[1])
    for w, s, v in bs:
        for i in range(w + s, w - 1, -1):
            dpw = dp[i - w] + v
            if dpw > dp[i]:
                dp[i] = dpw
    print(max(dp))


n = int(input())
f(n)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
dp = [0] * (n * 10 ** 4 + 1)
bs = [list(map(int, input().split())) for _ in range(n)]
bs.sort(key=lambda x: x[0] + x[1])
for w, s, v in bs:
    for i in range(w + s, w - 1, -1):
        dpw = dp[i - w] + v
        if dpw > dp[i]:
            dp[i] = dpw
print(max(dp))


Fixed submission:
def f(n):
    dp = [0] * (n * 10 ** 4 + 1)
    bs = [list(map(int, input().split())) for _ in range(n)]
    bs.sort(key=lambda x: x[0] + x[1])
    for w, s, v in bs:
        for i in range(w + s, w - 1, -1):
            dpw = dp[i - w] + v
            if dpw > dp[i]:
                dp[i] = dpw
    print(max(dp))


n = int(input())
f(n)