Added dynamic programming code