Help getting WA

Hey everyone. So I’ve been trying this problem

My solution:
https://www.codechef.com/viewsolution/38186121
Coded it, debugged it, ran it successfully on various testcases both on the online IDE and on BlueJay but i don’t know why it is giving WA. I am prone to making silly mistakes but not good at spotting them, please help.