UVA Problem #562 solved.