Code giving correct output for problem rgame but for some reason all tests fail

Hi, this is my solution for the problem rgame:
https://www.codechef.com/viewsolution/25965163

I tried various large inputs on it and compared the output from my program with the output from the accepted solutions on the same input, and they all match. But for some reason all of the subtasks fail. I can’t find out what’s wrong with my code since the input is formatted correctly and it seems to be giving the correct numbers for all the inputs I try. Can anyone look at this and help figure out what’s wrong?

Problem: