As Eugene pointed out in my blog post, the algorithm seems to produce a wrong output. https://andrew-algorithm.blogspot.com/2014/11/uva-problem-481-what-goes-up.html