I have done the above program it is showing TLE in the two test cases

Hey, the time complexity of your solution is exponential, maybe try to optimize it using dp.