Code works better with custom input.But showing wrong answer while submitting

My code gives wrong answer on submission but when give sample input through custom input. I get the same sample output as shown in the question. I can’t understand what’s wrong.Please help
My solution is https://www.codechef.com/viewsolution/38784831

what problem are you asking for? We do not have access to your submission

Could possibly be from long challenge since we can’t see the submission.

october challenge