Solving a simple DP problem (student stuff)