codeforces prob, requires concept of DP, to avoid TLE

this code of mine, for prob (http://codeforces.com/problemset/problem/374/C) gave TLE on test case 11,here is my code : http://ideone.com/4uKM7I
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 http://codeforces.com/submissions/rajbir
thanx for ur time…

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