Implementation of Dynamic Programming