WA in MAXBTY

Tried solving the question without using trees. The approach seems to be working and passes some random cases.
https://www.codechef.com/viewsolution/30664165
Here is my solution.

Please Help
Thanks.