AcWing 2. 01背包问题
原题链接
简单
作者:
Doris
,
2019-01-17 18:39:59
,
所有人可见
,
阅读 5688
python3 代码
import sys
def solve(n, t, w, v):
ans = [[0 for i in range(t + 1)] for j in range(n + 1)]
for i in range(n + 1):
ans[i][0] = 0
for i in range(t + 1):
ans[0][i] = 0
for i in range(1, n + 1):
for j in range(1, t + 1):
if j >= w[i]:
ans[i][j] = max(ans[i - 1][j], ans[i - 1][j - w[i]] + v[i])
else:
ans[i][j] = ans[i - 1][j]
return ans
if __name__ == '__main__':
n, t = map(int, input().strip().split())
w = [0 for i in range(n + 1)]
v = [0 for j in range(n + 1)]
for i in range(1, n + 1):
w[i], v[i] = map(int, input().strip().split())
ans = solve(n, t, w, v)
print(ans[n][t])
w = [0] * (n + 1)