Every non-base DP cell compares two exact choices: leave the current item or take it. This lesson works one cell completely. The highlighted table shows the result cell and its two predecessors.

highlighted = computed this step

Leave option

Leaving the item gives value 5. Why: this reuses the previous row at the same capacity.

leave=5\text{leave}=5
Take or leave cellThe highlighted cell and predecessors are recomputed from the DP table.Worked recurrence01234567cap-00000000(1,1)01111111(3,4)01145555(4,5)01145669(5,7)01145789resultleave (skip item i)take (item i)

Take option

Taking the item uses previous value 4 plus item value 5, giving 9. Why: capacity is reduced by the item weight before adding its value.

take=4+5=9\text{take}=4+{}5=9
Take or leave cellThe highlighted cell and predecessors are recomputed from the DP table.Worked recurrence01234567cap-00000000(1,1)01111111(3,4)01145555(4,5)01145669(5,7)01145789resultleave (skip item i)take (item i)

Result cell

The cell value is 9. Why: the recurrence keeps the better of take and leave.

dp[3,7]=9dp[3,7]=9
Take or leave cellThe highlighted cell and predecessors are recomputed from the DP table.Worked recurrence01234567cap-00000000(1,1)01111111(3,4)01145555(4,5)01145669(5,7)01145789resultleave (skip item i)take (item i)

Diagram note

The legend identifies the result cell, the leave predecessor, and the take predecessor. Pixel positions are rounded for layout; every number shown is exact.

one cell is a max of two exact integers\text{one cell is a max of two exact integers}
Take or leave cellThe highlighted cell and predecessors are recomputed from the DP table.Worked recurrence01234567cap-00000000(1,1)01111111(3,4)01145555(4,5)01145669(5,7)01145789resultleave (skip item i)take (item i)