NZEC error

i have devoted my whole day in solving this but getting NZEC error .i checked for so many testcases Plz check it at below link

Mysolution :- http://ideone.com/kfeqgc http://www.codechef.com/viewsolution/4120282

Problem link:-http://www.codechef.com/problems/D1

I just implemented the editorial of this question.