Watch the 0/1 knapsack DP table fill cell by cell for items A, B and C, then trace back the best pick. Change the capacity, and see why a greedy choice fails.
About this algorithm
The 0/1 knapsack problem asks which items to pack into a bag with a weight limit to get the most total value, when each item is either taken whole or left behind. Dynamic programming solves it in O(n · W) by filling a table where each cell answers a smaller question: the best value using only the first few items with a smaller limit. Trying every subset instead doubles the work with each extra item.
Use this table whenever you choose a subset under a budget and items cannot be split: picking projects within a budget, loading containers by weight, choosing exam questions within a time limit. Subset sum and partition equal subset sum are the same table with true and false in the cells. If items can be cut into fractions, the much faster greedy fractional knapsack is exact. If each item can be used any number of times, you have unbounded knapsack, whose recurrence looks like coin change. The table grows with the weight limit, so for very large limits with few items, a search with pruning can be cheaper.
dp[i][w] is the best value you can get from the first i items with a weight limit of w. Row 0 (no items) and column 0 (no capacity) are all zeros.
For item i with weight wt and value v: if wt is more than w, the item cannot fit, so copy the cell above, dp[i - 1][w]. Otherwise take the larger of skipping it, dp[i - 1][w], and taking it, dp[i - 1][w - wt] + v.
Every cell only reads from the row above, so filling rows top to bottom, left to right, means its two inputs are always ready. The answer is the bottom-right cell.
Start at the bottom-right cell. If it differs from the cell above, item i was taken: record it and move left by its weight. Either way move up one row. Stop when you reach row 0 or column 0.
The page loads A (weight 2, value 6), B (weight 2, value 10) and C (weight 3, value 12) with a capacity of 5. Row A reads 0 0 6 6 6 6: A fits from weight 2 up. Row B reads 0 0 10 10 16 16; at w = 4 taking B gives dp[A][2] + 10 = 16, better than skipping it for 6. Row C reads 0 0 10 12 16 22. At w = 4 the page shows SKIP, since taking C gives dp[B][1] + 12 = 12, less than 16. At w = 5 taking C gives dp[B][2] + 12 = 22, more than 16, so the answer is 22. Backtracking: 22 differs from the 16 above it, so C was taken and the walk moves to w = 2; there 10 differs from 6 above it, so B was taken. The bag holds B and C, weight 5, value 22.
Move the slider to capacity 3. Ranking by value per weight puts B first (5 per unit), then C (4), then A (3). Greedy takes B, and with 1 unit left nothing else fits: value 10. The table finds C alone, worth 12. Choosing the best ratio first can waste space that a whole item needed.
Each row only reads the row above, so a single array of size W + 1 works if you fill it from right to left; going left to right would let an item be taken twice. That cuts space to O(W) but loses the history needed to list the chosen items.
Time: there are (n + 1) × (W + 1) cells and each takes one comparison, which is pseudo-polynomial because it grows with the value of W, not the number of digits in it. Space: the full table is kept so the chosen items can be traced back; the one-row version needs only O(W).