UVA 499 in scala