Optimal Game Strategy-2, TLE in testcase 0

I am getting a TLE in test case 0. I have tried dynamic programming. It’s giving me a run error. Please suggest the necessary changes in the code.

The link for my code - https://ide.codingblocks.com/s/230481

hey, you current logic uses expensive recursive calls, which still takes it upto exponential time overall in some cases


this approach takes makes use of a 2d dp and is much more efficient, u can have a look at this approach and try fixing ur code