Sorting Device Solution..

Anyone solved this question of Sorting Device… (Sorting device Problem Code: MX). I am more interested in the Algorithm than getting an AC on CodeChef. I looked up the AC solution that was posted 8 to 9 years back and wrote a code in Python to give the same answer that the AC code generate, and still I am getting Wrong Answer. Any kind of ques or help will be appreciated as why my solution is not accepted.

//