finish dynamic programming solution