codeforces prob, requires concept of DP, to avoid TLE

this code of mine, for prob ( gave TLE on test case 11,here is my code :
i could’nt find the bug hence posted this on codechef forum.
got no replys,was disappointing but thanx to god i was able to think of a way to deploy DP in order to avoid uneccessery iterations…
heres my submission
thanx for ur time…

Now your submission is accepted. So, please close this topic.