lighthouse - WA

ques seems simple, my code works fine for tests, but gives WA on codechef, can someone please have a look at my code and point out if i am doing any silly mistake.

link to code - http://ideone.com/dGmLcM

//