I attempted running your code for the sample testcase and it prints the correct answer only for the first input. The reason is that you have the
return 0 statement in the wrong nested depth. By swapping the statement with the line below it, it prints the correct output for the sample testcase.
However, if you submit this you should get TLE. This is because you execute the same code for completing the
mark table for each input, while you could do this only once in the beginning (for the maximum value of N and keep the sums). This is sufficient to get AC.