DSA Learning - Subtraction Game 2 (DP) - TLE

I am getting TLE with the following code,( https://pastebin.com/LBGHUJsY ) someone please check if the approach is wrong .