topcoder; solve "NextNumber" from SRM #416 (Div. 2)