Need help in knowing where i went wrong in this problem

This is my solution to the problem:

https://www.codechef.com/viewsolution/53660403

Need help in understanding why I am getting wrong answer (WA). I have tried for a while, but with no respite.

Here’s a random test input on which your solution fails:

1
1 3 3
3 39
2 Likes