Type the 0/1 knapsack DP recurrence
Type the 0/1 knapsack DP recurrence
Answer
dp[i][w] = max(dp[i-1][w], dp[i-1][w - w_i] + v_i) if w_i <= w = dp[i-1][w] otherwise
dp[i][w] is the best value using the first i items within weight w. Either skip item i (dp[i-1][w]) or take it (value v_i plus the best for the remaining capacity w - w_i). Fill the table in O(nW); pseudo-polynomial because W is a numeric value, not a count.