This is an algorithm created for the algo task of the 5th round of the programming contest of PC Magazine and Telerik 2012 / 2013.