s = input().split()
a=int(s[0])
b=int(s[1])
v = [0] * a
w = [0] * a
for i in range(a):
s1 = input().split()
w[i] = int(s1[0])
v[i] = int(s1[1])
dp = [[0 for _ in range(b+1)]for _ in range(a)]
初始化dp[i][j]
for j in range(b+1):
if j>=w[0]:
dp[0][j] = v[0]
for i in range(1,a):
for j in range(1,b+1):
if j < w[i]:
dp[i][j] = dp[i - 1][j]
else:
# 不放物品i
x=dp[i-1][j]
# 放物品i
y=dp[i-1][j-w[i]]+v[i]
dp[i][j]=max(x,y)
max_value = dp[a-1][b]
print(max_value)
就是每次打算放一个东西时,首先要考虑它放不放得下,放不下的话就直接不放;放得下的话,就要看放他划算还是不放它划算
j - weight[i],这里可以理解为背包需要留出这个物品i的容量才可以放物品i