The 0/1 knapsack recurrence (type it)
The 0/1 knapsack recurrence (type it)
Answer
dp[0][w] = 0 dp[i][w] = dp[i-1][w] if w_i > w dp[i][w] = max(dp[i-1][w], dp[i-1][w - w_i] + v_i) if w_i <= w
Two choices per item: skip it (carry the row above) or take it (value v_i plus the best of the remaining capacity in the row above). The whole-or-nothing rule is why we read row i-1, never row i.