incorrect solution

This is a solution to google code jam 2008 qualification round Problem
A. Works for the sample testcases given in problem. Fails for an actual
attempt to solve. input file has the actual attempt that fails.
output.txt has the corresponding output