27/40
Part 5 of 7 · The answer and a limitLearn

Add the count to the cell, and take the best over every count up to the limit

So write the four things again, with the count in the cell.

  1. The cell. best[j][u] is the highest total of a route from the start to j that rode exactly u expert stretches.
  2. The recurrence. A stretch from j to lower worth score moves a route from best[j][u] to best[lower][u + 1] if it's expert, and to best[lower][u] if not, as long as the count stays within the limit.
  3. The base case. best[start][0] = 0, and every other cell starts at minus infinity.
  4. The order. Junctions in topological order, all of a junction's counts at once. Every stretch leads to a junction later in the order, so nothing in a later column feeds an earlier one.

The answer is the best cell at any exit, in any row up to the limit: at most the limit, not exactly. With a limit of 1:

best[j][u] with a limit of 1
best
topbirchashdellclifffallsendu = 002·86·10u = 1··3·7118

· is minus infinity: no route. An expert stretch moves a total down one row: top's 0 to ash's 3, and cliff's 6 to falls's 11.

cliff keeps both totals, 6 with the expert stretch unspent and 7 with it spent, and falls gets its 11 from the 6. With a limit of 0 the answer is 10, with 1 it's 11, and with 2 it's 12, the unlimited best.