SNAKY: Getting WA

http://www.codechef.com/viewsolution/4722735
It seems my solution is correct when I try it with every sample case. But codechef is saying ‘wrong answer’. I am trying for hours. Please Help!
Input format is correct I guess.