I am getting TLE unnecesarilly Even though it fit within time constraints

http://p.ip.fi/A592

https://cses.fi/problemset/task/1639/

due to many recursive calls the top down dp is giving TLE for very large inputs

use Top Down dp here it will pass all testcases

Reference Code

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.