prob 31 dynamic programming