1def pick(v):2 n = len(v)3 best = [0] * (n + 1)4 best[1] = v[0]5 for i in range(2, n + 1):6 skip = best[i-1]7 take = best[i-2] + v[i-1]8 best[i] = max(skip, take)9 return best[n]coin 12coin 27coin 39coin 43coin 51best(0)best(1)best(2)best(3)best(4)best(5)
Back row: the coins. Front row: the table, one cell per state best(i)Blue token: skip coin i, keep best(i−1). Pink token: take coin i, add best(i−2)Green token: the walk back that recovers the picksThe state best(i) is the largest total from the first i coins, one table cell per state. The transition fills best(i) from best(i−1) (skip coin i) and coin i + best(i−2) (take it), whichever is larger, so the cells fill left to right: 0, 2, 7, 11, 11, 12. Walking back from best(5) recovers the picks: coins 1, 3, 5.

The state

  1. 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.

The transition

Order and answer

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).