UVA Problem 102 solved