Why am i getting wrong answer?

I have been trying the problem WALK and codechef is telling that iam submitting a wrong answer. Can you correct my code.

//Code Deleted as it is from an ongoing contest

@yashuanth This is a question from Live Contest and its against the code of conduct of CodeChef to discuss it here or anywhere else. Try to think of cases where your algorithm is failing and ofcourse after the contest ends you can have a look at the solutions/editorial.