Close #6. Solve POJ 1005