Runtime Error on Problem 'Cleaning Up'

I am trying to solve the ‘Cleaning Up’ problem. I am solving it using Python 2.7. I have tried with all the sample test cases, and they seem to be working fine on my system. On submission, I am getting a runtime error.
My Solution:
http://www.codechef.com/viewsolution/1668427

What am I doing wrong here?

Thanks in Advance