Implement algorithm for codejam 2014 qual c.