modified binary search, AC