1026. Questions and Answers – Solution http://acm.timus.ru/problem.aspx?space=1&num=1026 View the code on Gist.