Need help with the dsa learning series problem

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

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 - CodeChef: Practical coding for everyone
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.