Need help with the dsa learning series problem

https://www.codechef.com/viewsolution/34730873
The above link is for my submission to the question:
https://www.codechef.com/LRNDSA02/submit/STUPMACH

I am getting partially correct answer though i believe the logic is sound. Could someone please help me with this?

Maybe take a look at this - https://www.codechef.com/viewsolution/33177075
I’m having a hard time reading the Python script.

Thanks a lot! Although i’m still unable to understand why mine isn’t accepted. I believe mine is implemented in the reverse order but it is the same thing.