Solved UVA problem, but need to let it read from stdin.