Google Code Jam 2010 qualifcation problem B
https://code.google.com/codejam/contest/351101/dashboard#s=p1