The state
Five coins in a row: 2, 7, 9, 3, 1. Take any you like, but never two side by side. What is the largest total? Checking every pick works but explodes: 13 legal picks here, and about 1.6 times more for each extra coin.
Dynamic programming starts by naming a state: a small question whose answer we store. Here: best(i) = the largest total using only the first i coins. One cell per i, from best(0) to best(5). The answer we want is best(5).
Some states are answered straight away. best(0) = 0: no coins, nothing to take. best(1) = 2: with one coin, take it. These are the base cases; every other cell is built from them.
The transition
A transition builds one state from smaller ones. At coin i: skip it and keep best(i−1), or take it, which bans coin i−1, giving coin i + best(i−2). Keep the larger. For i = 2: max(2, 7) = 7.
Coin 3 is worth 9. Skip it: best(2) = 7. Take it: 9 + best(1) = 11. Take wins, so best(3) = 11. Each cell reads only two cells to its left, and both are already filled.
Coin 4 is worth 3. Take it: 3 + best(2) = 10. Skip it: best(3) = 11. Skip wins, so best(4) = 11. The state does not remember which coins made 11, and it does not need to.
Coin 5 is worth 1. Skip: best(4) = 11. Take: 1 + best(3) = 12. So best(5) = 12: the most you can take from all five coins without two neighbours. return best[n] hands it back.
Order and answer
Every state needs only smaller states, so filling left to right never reads an empty cell: a topological order. Cost = states × work per transition: 6 cells, one max each. O(n) time, not an exponential search.
The table stores totals, not picks, but we can walk back. best(5) ≠ best(4), so coin 5 was taken: jump to best(3). 11 ≠ 7: coin 3 taken, jump to best(1). Coin 1 taken. 2 + 9 + 1 = 12.
What happens
;; step 1/9 · problem coins v = [2, 7, 9, 3, 1] rule: never take two neighbours subsets of 5 coins: 2^5 = 32 legal picks (no two neighbours): 13 the count for n coins is F(n + 2), a Fibonacci number: n = 5, 10, 20, 30 → 13, 144, 17711, 2178309 it grows about 1.618 times per extra coin
The steps follow the SRT BOT recipe from MIT 6.006 (Spring 2020, lecture 15): Subproblem, Relate, Topological order, Base cases, Original problem, Time. CLRS chapter 14 gives the same plan: characterise an optimal solution, define its value recursively, compute it bottom up, then rebuild the solution. The code is real Python 3: pick([2, 7, 9, 3, 1]) returns 12. It assumes at least one coin; for an empty list, return 0 first. max(skip, take) returns the larger value (docs.python.org).