Solved problem 78 using dynamic programming