adding dynamic programming version