Doubt in CNOTE

I submitted the solution for CNOTE and found that the solution was accepted when I tried to print the output outside the loop. Printing and breaking the loop works just as fine for one of the condition. What am I missing here ?

AC : CodeChef: Practical coding for everyone
WA : CodeChef: Practical coding for everyone

Ps : I’m a newbie to solving CP. Any improvements or suggestions for my code would be appreciated.