TopCoder SRM 599 Division two 250/500 solved.