uva 591 impl in java