solution to UVA 164