dynamic programming worked