a Qualifying round problem from Google codejams