TLE in AMSGAME2

I have been trying the solve this question using DP but receives a TLE.
https://www.codechef.com/viewsolution/34712871
How could i further optimise this solution ??