Solved problem 15 using DP