I have solved the problem successfully, however when I tried top down approach, it gave TLE in testcases 1 and 2, also when used bottom up DP with bool datatype, it gave TLE too. It works for the current version of the code.
I am unable to figure out what works and what caused the previous versions to give TLE?