Added code for Dynamic Programming