Problem: You have N items and a bag of strength W. The i-th item has a weight of w_i and a value of v_i.  You will select some of the items and put them in the bag. Here, the total weight of the selected items needs to be at most W.  Your objective is to maximize the total value of the selected items.

Submission attempts (in order):
N,W = map(int, input().split())
ws = []
vs = []
for i in range(N):
    w,v = map(int, input().split())
    ws.append(w)
    vs.append(v)
ws_m = [w-ws[0] for w in ws]


dp = [[[-1]*(3*N+1) for i in range(N+1)] for j in range(N+1)]
dp[0][0][0] = 0
for i,(w,v)  in enumerate(zip(ws_m,vs)):
    for n in range(i+1):
        for j in range(3*N):
            
            if j-w >= 0:
                dp[i+1][n+1][j] = max(dp[i][n+1][j], dp[i][n][j-w]+v)
            else:
                dp[i+1][n+1][j] = dp[i][j+1][k]
            
            # # if j + n*ws[0] > W: continue
            # # print("check",i,n,j,v)    )
            # dp[i+1][n][j] = max(dp[i+1][n][j], dp[i][n][j])
            # dp[i+1][n+1][j+w] = max(dp[i][n+1][j+w], dp[i][n][j] + v)
ans= 0
for i,As in enumerate(dp[N]):
    w = W - i*ws[0]
    if w > 0:
        ans = max(ans,max(As[:w+1]))
print(ans)   

N,W = map(int, input().split())
ws = []
vs = []
for i in range(N):
    w,v = map(int, input().split())
    ws.append(w)
    vs.append(v)
ws_m = [w-ws[0] for w in ws]


dp = [[[-1]*(3*N+1) for i in range(N+1)] for j in range(N+1)]
dp[0][0][0] = 0
for i,(w,v)  in enumerate(zip(ws_m,vs)):
    for n in range(i+1):
        for j in range(min(sum(ws_m,W-n*ws[0]))+1):
            if dp[i][n][j] == -1: continue
            if j + n*ws[0] > W: continue
            # print("check",i,n,j,v)
            dp[i+1][n+1][j+w] = max(dp[i][n][j+w], dp[i][n][j] + v)
            dp[i+1][n][j] = max(dp[i+1][n][j], dp[i][n][j])
ans= 0
for i,As in enumerate(dp[N]):
    w = W - i*ws[0]
    if w > 0:
        ans = max(ans,max(As[:w+1]))
print(ans)   

N,W = map(int, input().split())
ws = []
vs = []
for i in range(N):
    w,v = map(int, input().split())
    ws.append(w)
    vs.append(v)
ws_m = [w-ws[0] for w in ws]


dp = [[[-1]*(3*N+1) for i in range(N+1)] for j in range(N+1)]
dp[0][0][0] = 0
for i,(w,v)  in enumerate(zip(ws_m,vs)):
    for n in range(N):
        for j in range(3*N):
            if dp[i][n][j] == -1: continue
            # if j + n*ws[0] > W: continue
            # print("check",i,n,j,v)
            dp[i+1][n+1][j] = max(dp[i+1][n+1][j], dp[i][n+1][j])
            dp[i+1][n+1][j+w] = max(dp[i][n+1][j+w], dp[i][n][j] + v)
ans= 0
for i,As in enumerate(dp[N]):
    w = W - i*ws[0]
    if w > 0:
        ans = max(ans,max(As[:w+1]))
print(ans)   

N,W = map(int, input().split())
ws = []
vs = []
for i in range(N):
    w,v = map(int, input().split())
    ws.append(w)
    vs.append(v)
ws_m = [w-ws[0] for w in ws]



dp = [[[-1]*(3*N+1) for i in range(N+1)] for j in range(N+1)]
dp[0][0][0] = 0
for i,(w,v)  in enumerate(zip(ws_m,vs)):
    for n in range(i+1):
        for j in range(3*N):
            if dp[i][n][j] == -1:continue
            dp[i+1][n+1][j+w] = max(dp[i][n][j] + v, dp[i][n][j+w])
            dp[i+1][n][j] = max(dp[i+1][n][j], dp[i][n][j])

ans = 0
for i,a in enumerate(dp[N]):
    w = W - i*ws[0]
    if w >= 0:
        ans = max(ans,max(a[:w+1]))
print(ans)   


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Accepted