Giving WA for the potato problem KTHCON

I tried solving this problem by following editorial. But getting WA. Not able to figure on which testcase it is failing.
Here is link to question:

Here is link to my solution.
https://www.codechef.com/viewsolution/33024999