Good dynamic programming problem.