WA IN PROM02

Why this code http://ideone.com/ibCNbn gives wrong answer for the question http://www.codechef.com/PRMT2014/problems/PROM02.

I think I am missing something, pretty sure about my logic.