From Programming contest challenge book