final version for poj 1028