Added a bunch of new code for dynamic programming.