recursion with memorization, equivalent to DP.