Added dynamic programming algorithm