uva 11636 implementation in java