Module 10 · Dynamic Programming · DP: Inclusion–Exclusion

0-1 knapsack

Every item is either left behind or taken — if it still fits. A call (currIndex, remCap) tries both and keeps the better value; that's the inclusion–exclusion tree, and dp[currIndex][remCap] remembers each answer so a repeated call returns at once. In the tree a box shows remCap and its row is currIndex; each call's bag shows how full it is; on the way back, a take branch shows the value it adds and a coin marks every call that returns some value.