Solved 29, 30 and 31 (Dynamic Programming)