We need to solve: Given X (1..100), N (0..100) and a list of N distinct integers p_i (1..100). Among all integers not in the list, find the integer nearest to X. If multiple (i.e., same distance), choose the smallest integer.
Key: Integers considered are all integers, not necessarily positive? The problem says "among the integers not contained in the sequence p_1, ..., p_N (not necessarily positive)". So we consider all integers, negative, zero, positive. But constraints: X is 1..100, p_i are 1..100. So the p_i are positive and between 1 and 100. But the integers not in the list can be any integer, including negative numbers and numbers >100.
Goal: find the integer not in the list with minimal absolute difference to X.
Since p_i are within 1..100, but we consider all integers, the solution might be outside that range. However, we can reason: The set of integers is infinite. The distance from X to any integer grows as we go away. The nearest integers not in the list will be around X. Since X is in [1,100], and p_i are within [1,100] but may include X or not. We need to consider candidates near X.
We can just search outward: Check X itself. If it's not in the list, answer is X (since distance 0 is minimal, and if multiple, but distance 0 only for X itself; if X is not in list, it's the nearest). If X is in the list, then we need to check X+1 and X-1, etc. Because integers not in the list with minimal distance will be the closest numbers to X not in the list. So we can just expand from X outward until we find integers not in the list. However, note that there are negative integers: e.g., X=1, if 1 is in the list, then check 0, 2, -1, 3, etc. But since negative integers are allowed, we must consider them.
But is there a possibility that the nearest integer not in the list is far away? For example, if X is 50 and the list contains all integers from -infinity to +infinity except some numbers? But the list only has at most 100 numbers, so infinitely many numbers are not in the list. So the nearest to X not in the list will be at distance at most something like ... Actually, if X itself is not in the list, distance 0. So only when X is in the list we need to check distances >=1. Since list size is limited (<=100), there is always some integer not in the list, obviously. The nearest will be found by checking X+d and X-d for d=1,2,3,... until we find one not in the list. Because the set of integers not in the list is infinite, so we will eventually find some. However, we must consider negative direction: X-d may become negative and that's fine. So algorithm: create a set S of p_i. If X not in S, output X. Else, for d=1,2,3,... infinity: check X-d and X+d. The first integer we find not in S is candidate. But because there might be two at same distance: For a given d, we check X-d and X+d. If both are not in S, we need to choose the smallest integer (i.e., min(X-d, X+d)) because if distances equal, pick smallest integer. But careful: If only one of them is not in S, we output that. If both are not in S, we output the smaller one. However, we should also consider the possibility that X-d or X+d might be equal? No, for d>0 they are distinct.
But we need to ensure we check both sides for each d in increasing d, and then stop as soon as at least one is not in S. Actually, if we are at distance d, the minimal possible distance is d. So if either X-d or X+d is not in S, then the minimal distance is d. Among those with distance d, we need to pick the smallest integer (if both present). So we can implement: for d=0 first, check X only. If not in S, done. Then for d from 1 to something: have found = false; candidates = []. If X-d not in S, add X-d; if X+d not in S, add X+d. If candidates not empty, answer = min(candidates) (since min integer, given that distances equal). Stop.
But must we consider that X-d or X+d could be beyond constraints? Not a problem.
Since X and p_i are limited to 1..100, the search will quickly find a number. But to be safe, we can search up to some bound, say -1000 to 1000, but infinite search is fine because d will be at most 101 perhaps? Actually worst case: X=50, list contains all integers from maybe 0 to 100? But list only contains at most 100 numbers from 1..100, so many numbers outside are not in the list. However, note that we also have negative numbers not in the list. The nearest not-in-list integer could be X-? For X=1, if 1 and 2,..., 100 are all in the list? But N <=100, and p_i are from 1 to 100, distinct. So it's possible that N=100, and the list contains all integers from 1 to 100. Then what about X=1? X is in the list. Then we check 0 (not in list because list only has 1..100, 0 is not in the list). So distance 1. Answer 0. If X=100, check 101? But 101 is not in list (since list only up to 100). So distance 1. If X=50, check 49? Possibly 49 is in list if list includes 1..100. Then check 51, etc. Actually if list includes all 1..100, then for any X in 1..100, the numbers 0 and 101 are not in the list, and they are at distances X and 101-X (or X-0) respectively. The nearest could be 0 if X <= 50.5? Actually distance from X to 0 is X, to 101 is 101-X. So the smaller distance is min(X, 101-X). For X=1, min(1,100)=1 -> both 0 and 101 are distance 1? Actually distance to 0 is 1, to 101 is 100, so nearest is 0. For X=50, distances: 50 and 51, so 50 is smaller => 0. For X=51, distances: 51 and 50 => 101. So the nearest might be either 0 or 101 depending on X. However, also consider negative numbers: -1 distance 52 from 51, etc. So indeed the search would find d=1 for X=1: X-1=0 not in list, so answer 0. For X=100: X+1=101 not in list, answer 101? But also X-1=99 is in list because list has 99? Actually if list has all 1..100, then 99 is in list, so only 101 is not. So answer 101. For X=51: check d=1: X-1=50 (in list), X+1=52 (in list). d=2: 49 (in list), 53 (in list). Continue up to d=50: X-50=1 (in list), X+50=101 (not in list). But also X-51=0 (not in list) at d=51. So we need to find the smallest d where either side is not in list. For X=51, d=50 gives X+50=101 not in list, but X-50=1 in list. So distance 50 is found. Also d=51 gives X-51=0 not in list at distance 51, but we already found d=50 with candidate 101. So answer should be 101? Check distances: 101-51=50. 0-51 = -51 absolute 51. So nearest is 101. However, is there any integer not in list with distance less than 50? Let's check all numbers from 1..100 are in list. So numbers not in list are ... -inf to 0 and 101 to inf. The smallest distance to any of those is min(51, 50) = 50, because distance to 0 is 51, to 101 is 50. So answer is 101. Good.
Our search algorithm: start d=1, check both sides. Increase until find a side not in S. So for X=51, d=1 to 49: both sides in S (since 1..100). At d=50: X-50=1 in S, X+50=101 not in S -> found candidate 101 (since only that side not in S). So answer 101. That matches.
Thus algorithm is fine.
Edge Cases: N=0, S empty. Then X not in S, output X. So example 3: 100 0 => output 100.
Example 1: X=6, N=5, p = [4,7,10,6,5]. S={4,7,10,6,5}. X=6 is in S. d=1: 5 and 7 are both in S? 5 yes, 7 yes. d=2: 4 and 8. 4 in S, 8 not in S. So candidate: 8. So output 8. matches.
Example 2: X=10, S same. X=10 in S? yes, because 10 in list. d=1: 9 and 11. 9 not in S? Actually S: 4,7,10,6,5. 9 is not in S. So candidate 9. Output 9. matches.
So solution is straightforward.
Implementation details: Read input. Possibly N could be 0, so second line may be missing? Input format: first line X N, second line p1 ... pN. If N=0, second line may be omitted or be empty. In many programming contest inputs, if N=0, there is no second line. So we need to handle that. Typically, we can read the whole line and split. Or we can read after checking N. We'll just read the first line, then if N>0 read second line else ignore.
We'll create a set from p list (or just a boolean array of size maybe 201 to include numbers up to 200? But we need to check membership for any integer; using a set is easier.)
Then algorithm as described.
Complexity: d might go up to maybe 100? Actually worst case: X=50, S contains all numbers from 1 to 100 except perhaps some numbers? But if S contains 1..100 except maybe X? But we need scenario where X is in S and both sides are filled densely for many steps. Since S max size 100 and numbers are between 1 and 100, the worst gap around X could be up to 100? For example, X=100, S contains all 1..100? Then check d=1: 99 in S, 101 not in S -> d=1. For X=1, d=1: 0 not in S -> d=1. For X=50, with S = all 1..100, we found d=50 for 101? Actually that's 50? Wait d=50 gives 101 not in S. But also there is 0 at d=50? For X=50, d=50: X-50=0 not in S, X+50=100 in S? 100 is in S, so we find 0 at d=50 as well. So d=50. That's the maximum possible distance if X=50 and S=1..100. But could there be a scenario where d > 50? Suppose X=100 and S = {1..100}? Then d=1 gives 101. So not. X=1 gives 0 at d=1. So max d is 50. Actually what about X=100 and S contains all numbers from 1 to 100 and also all numbers from maybe 101 to 150? But S can only contain numbers between 1 and 100 per constraints, so numbers >100 or <1 are never in S. So the nearest integer not in S will be at most max( distance to 0, distance to 101 ) whichever is smaller. Since X in [1,100], min distance to 0 is X, to 101 is 101-X. The maximum of these minima is when X=50 or 51? For X=50, min(50, 51) = 50. For X=51, min(51, 50)=50. So max min distance = 50. However, if N < 100, there might be gaps within 1..100, so the distance could be less than that. But worst-case d is 50. So we can bound d to 200 if we want.
But our algorithm with while True is safe.
Implementation:
def solve():
import sys
data = sys.stdin.read().split()
if not data:
return
X = int(data[0])
N = int(data[1])
p = []
if N > 0:
p = list(map(int, data[2:2+N]))
S = set(p)
if X not in S:
print(X)
return
d = 1
while True:
# Check lower
low = X - d
high = X + d
cand = []
if low not in S:
cand.append(low)
if high not in S:
cand.append(high)
if cand:
# There may be one or two. Choose smallest integer.
print(min(cand))
return
d += 1
That's it.
But we must ensure we don't run into infinite loop if S contains all integers? But S is finite, so we'll eventually find a number not in S. Since integers outside 1..100 are not in S, we will definitely find within d up to max(X, something). So safe.
Double-check: If N=0, S empty, X not in S so we print X. Good.
Edge Cases: X might be negative? No, constraints X 1..100.
So solution done.
But wait: The problem says "among the integers not contained in the sequence p_1,...,p_N (not necessarily positive), find the integer nearest to X". It doesn't restrict that integer must be positive. So our search includes all integers.
Consider possibility: X=1, p = [1,2,...,100]. Then S contains 1..100. Our algorithm: d=1: low=0 not in S, so candidate 0, answer 0. That is correct.
Consider X=1, p = [1] only. Then S={1}. X in S. d=1: low=0 not in S, answer 0. But also 2 not in S, so both 0 and 2 are at distance 1. Which is smallest? 0. So answer 0. Good.
Consider X=100, p = [100] only. d=1: low=99 not in S? 99 is not in S (since only 100). So candidate 99. But also 101 not in S, both at distance 1. Min is 99. So answer 99.
Check: That matches? X=100, nearest integer not in list: 99 and 101 are both distance 1, smallest is 99. Good.
Now check if there is any subtlety: When multiple at same distance, choose smallest integer. That is exactly min(cand) if both sides present.
All good.
Now we need to output answer as integer.
Thus final code in Python.
But I'll add perhaps reading with sys.stdin.readline to be safe.
Alternatively, we can simply compute using a loop over possible candidates sorted by distance and then by value. But the expanding search is efficient.
Let's verify with some additional test:
Input: X=50 N=2 p=50 51? Actually p must be distinct. X=50, p=[50,51]. S={50,51}. X in S. d=1: low=49 not in S, high=52 not in S. Both candidates, min=49. So answer 49. Check distances: 49 distance 1, 52 distance 2? Actually 52 distance 2? Wait |52-50|=2. So 49 distance 1. So 49 is correct. But there is also 51 distance 1? But 51 is in S, so not allowed. So 49 is the nearest. Good.
But we should consider that when X is in S, we might have a tie at distance 1 with both sides not in S. Then we pick the smaller.
Implementation wise, our code will pick min.
Edge: When d=0, we handled X not in S case. So fine.
Time complexity: O(d) where d <= ~100. So trivial.
Now final answer: provide explanation and code.
But since it's a typical AtCoder problem (probably ABC something), we can just output solution.
Now I'll produce final answer. ```python
import sys
def main() -> None:
data = sys.stdin.read().split()
if not data:
return
X = int(data[0])
N = int(data[1])
p = []
if N > 0:
p = list(map(int, data[2:2+N]))
forbidden = set(p)
# If X itself is allowed, it is the answer (distance 0)
if X not in forbidden:
print(X)
return
d = 1
while True:
low = X - d
high = X + d
candidates = []
if low not in forbidden:
candidates.append(low)
if high not in forbidden:
candidates.append(high)
if candidates:
# Among all numbers at the minimal distance, choose the smallest
print(min(candidates))
return
d += 1
if name == "main":
main()