Added solution, implemented dynamic programming