TopCoder TCO 2013 Round 2B, task 500