codeforces prob, requires concept of DP, to avoid TLE

this code of mine, for prob (Problem - 374C - Codeforces) gave TLE on test case 11,here is my code : 4uKM7I - Online C++0x Compiler & Debugging Tool - Ideone.com
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 Personal submissions - Codeforces
thanx for ur time…

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