Updated uva 11461 implementation