Completed http://poj.org/problem?id=1001