Code golfing. 195 bytes.
DP with memoization. 4 out of 8 test cases passed.