ZhangCheng-zh / blog

记录一些code
0 stars 0 forks source link

[Python] Bag DP template #14

Open ZhangCheng-zh opened 1 year ago

ZhangCheng-zh commented 1 year ago
# if require is strictly equal the amount
f = [0] + [inf] * amount
# if require is largest value under amount ceil
f = [0] * (amount + 1)

# ZeroOne Pack: each stuff can only pick one
for i in range(n):
    for j in range(amount, weight[i] - 1):
        f[j] = max(f[j], f[j - weight[i]] + value[i])

# Complate Pack: each stuff can pick unlimit times
for i in range(n):
    for j in range(weight[i], amount + 1):
        f[j] = max(f[j], f[j - weight[i]] + value[i])