Problem:


Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.



Sample input:
6 5
4 7 10 6 5


Sample output:
8

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
p = list(map(int, input().split()))

p.sort()

arr = [False] * 102

for i in range(len(p)):
    arr[p[i]] = True

dist = 10*10

ans = -99

search = list(range(-100,102))
search.reverse()

for i in search:
    if arr[i] == False:
        dist = min(abs(x - i), dist)
        if dist == abs(x - i):
            ans = i
print(ans)

Submission status: Accepted

Solution code:
X,N=map(int,input().split())
if N==0:
  print(X)
else:
  p=set(map(int,input().split()))
  for i in range(N+1):
    if X-i not in p:
      print(X-i)
      break
    elif X+i not in p:
      print(X+i)
      break

Submission status: Accepted

Solution code:
import bisect
X, N = map(int, input().split())
P = list(map(int, input().split()))
R = []
for i in range(105):
    R.append(i)
for p in P:
    R.remove(p)
a = bisect.bisect_right(R, X)
if X - R[a - 1] > R[a] - X:
    print(R[a])
else:
    print(R[a-1])

Submission status: Accepted

Solution code:
import sys
X, N = map(int, input().split())
if N==0:
    tmp = input()
    print(X)
    sys.exit(0)

p = list(map(int, input().split()))
tmp = 10**9
ans = 0
for i in range(-1, 102):
    if i not in p:
        if tmp > abs(i-X):
            ans = i
            tmp = abs(i-X)
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

# N = I()
X,N = LI()
_p = LI()
#AB = [LI() for _ in range(N)]
#A,B = zip(*AB)
# p = np.array(_p)
#C = np.zeros(N + 1)

p = set(_p)
for i in range(100):
    if X - i not in p:
        print(X - i)
        exit()
    if X + i not in p:
        print(X + i)
        exit()

# 条件を満たす配列
#q = np.where(p - X == 0)

Submission status: Accepted

Solution code:
x,n = map(int,input().split())
p = list(map(int,input().split()))
ans = 0
for i in range(102):
    if i in p:
        continue
    if abs(i-x) < abs(ans-x):
        ans = i
print(ans)

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
A = list(map(int, input().split()))

diff = 10000
candidate = -100000
for i in range(-101, 102):
    if abs(i - x) < diff and i not in set(A):
        diff = abs(i - x)
        candidate = i
print(candidate)

Submission status: Accepted

Solution code:
X, N = map(int, input().split())
p = list(map(int, input().split()))

ans = X
for i in range(N+1):
    if not X - i in p:
        ans = X - i
        break
    elif not X + i in p:
        ans = X + i
        break

print(ans)

Submission status: Accepted

Solution code:
X,N = map(int,input().split())
p = list(map(int,input().split()))


# p = list(p.sort())
list2 = sorted(p)

for i in range(1000):
    if X-i not in list2:
        print(X-i)
        break
    elif not X + i in list2:
        print(X+i)
        break

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import itertools
import math
import sys

INF = float('inf')


def solve(X: int, N: int, p: "List[int]"):
    return min(set(p) ^ set(range(0, 101 + 1)), key=lambda v: abs(v-X))


def main():

    sys.setrecursionlimit(10 ** 6)

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(solve(X, N, p))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X, N = map(int, input().split())
p = map(int, input().split())

p = set(p)
for i in range(100):
    if X - i not in p:
        print(X - i)
        break
    elif X + i not in p:
        print(X + i)
        break

Submission status: Accepted

Solution code:
X, N = map(int, input().split())
p = list(map(int, input().split()))

ans = 101
i = 101

while i != -1:
    a = 0
    for j in range(0,N):
        if i == p[j]:
            a = 1
    if a == 0 and abs(i-X) <= abs(ans-X):
        ans = i
    i -= 1

print(ans)

Submission status: Accepted

Solution code:
x,n=map(int,input().split())

if n==0:
  print(x)
  
else:
  a=list(map(int,input().split()))

  for i in range(100):
    if x-i not in a:
      ans=x-i
      break
    elif x+i not in a:
      ans=x+i
      break
    
  print(ans)

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
p = list(map(int, input().split()))
ans = 0
xhigh=0
xlow=0
if n==0:
    ans = x
else:
    if x not in p:
        ans=x
    else:
        xhigh=x+1
        xlow=x-1
        while xhigh in p and xlow in p:
            xhigh +=1
            xlow -=1
        else:
            if xlow in p:
                ans =xhigh
            else:
                ans = xlow
print(ans)

Submission status: Accepted

Solution code:
X, N = list(map(int, input().split()))
P = list(map(int, input().split()))

P = set(P)

diff = float('INF')
ans = None
for i in range(0, 102):
    if i in P:
        continue
    d = abs(X - i)
    if d < diff:
        diff = d
        ans = i

print(ans)

Submission status: Accepted

Solution code:
X, N = list(map(int, input().split()))
P = list(map(int, input().split()))

P = set(P)

diff = float('INF')
ans = None
for i in range(-100, 102):
    if i in P:
        continue
    d = abs(X - i)
    if d < diff:
        diff = d
        ans = i

print(ans)

Submission status: Accepted

Solution code:
xn = list(map(int,input().split()))
P = list(map(int,input().split()))

x = xn[0]
n = xn[1]

kMax = int(n/2)
bl = False
for k in range(kMax+1):
     if (x - k) not in P:
         ans = x - k
         bl = True
         break
     elif (x + k) not in P:
         ans = x + k
         bl = True
         break

if not bl:
    ans = x - (kMax + 1)     

print(ans)

Submission status: Accepted

Solution code:
X, N = list(map(int, input().split()))
P = list(map(int, input().split()))

P = set(P)

diff = float('INF')
ans = None
for i in range(-100, 300):
    if i in P:
        continue
    d = abs(X - i)
    if d < diff:
        diff = d
        ans = i

print(ans)

Submission status: Accepted

Solution code:
import sys
X,N=map(int, sys.stdin.readline().split())
P=map(int, sys.stdin.readline().split())
L=[ 0 for _ in range(102) ]
for p in P:
    L[p]=1

d=float("inf")
ans=None
for i,x in enumerate(L):
    if x==0:
        if abs(X-i)<d:
            d=abs(X-i)
            ans=i
print ans

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def solve():
    X, N = map(int, input().split())
    if N == 0:
        P  =[]
    else:
        P = list(map(int, input().split()))

    b = [0] * 1000
    for p in P:
        b[p] = 1

    ans = 0
    for i in range(len(b)):
        if b[i] == 0:
            if abs(X - i) < abs(X - ans):
                ans = i
    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
x, n = map(int, input().split())
p = list(map(int, input().split()))
if not p or x not in p:
  print(x)
  sys.exit()
dp = [i-x for i in p]
dp_dic = {}
for i in range(len(p)):
  if abs(dp[i]) in dp_dic:
    dp_dic[abs(dp[i])].append(p[i])
  else:
    dp_dic[abs(dp[i])] = [p[i]]
i = 1
while True:
  if i in dp_dic and len(dp_dic[i]) == 1:
    if x + i == dp_dic[i][0]:
      print(x - i)
    else:
      print(x + i)
    break
  elif i not in dp_dic:
    print(x - i)
    break
  i += 1

Submission status: Accepted

Solution code:

def solve():
    x,n=map(int,input().split())
    p=set(map(int,input().split()))
    d=100
    for i in range(0,102) :
        if abs(i-x)<d and not (i in p) :
            d=abs(i-x)
            ans=i
    return ans

print(solve())

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
a = set(map(int, input().split()))
print(min([i for i in range(102) if i not in a], key=lambda y:abs(x-y)))

Submission status: Accepted

Solution code:
import sys

X,N = map(int, input().split())
p = list(map(int,input().split()))

if(N == 0):
    print(X)
    sys.exit()
    
min = 0
plu = 0
ans = 200
a = [1] * 102 # 0と101を入れている

for i in range(N):
    a[p[i]] = 0

if(a[X] == 1):
    ans = X
    print(ans)
    sys.exit()


for k in range(100):
    min = X - (k+1)
    plu = X + (k+1)

    if(a[plu] == 1):
        ans = plu
    if(a[min] == 1):
        ans = min
    
    if(ans < 200):
        break

print(ans)

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
p = list(map(int, input().split()))

a = set()
for i in range(-100, 201):
    a.add(i)
for v in p:
    a.remove(v)
a = list(a)
b = list(map(lambda v: abs(v - x), a))
print(a[b.index(min(b))])

Submission status: Accepted

Solution code:
from sys import stdin
import sys

x,n = [int(x) for x in stdin.readline().rstrip().split()]
p = [int(x) for x in stdin.readline().rstrip().split()]

for i in range(10**4):
    x1 = x-i
    x2 = x+i

    if x1 not in p:
        print (x1)
        sys.exit()

    if x2 not in p:
        print (x2)
        sys.exit()

Submission status: Accepted

Solution code:
import math
import sys
readline = sys.stdin.readline


def main():
    x, n = map(int, readline().rstrip().split())
    p = list(map(int, readline().rstrip().split()))
    for k in range(100):
        if p.count(x - k) == 0:
            print(x - k)
            break
        if p.count(x + k) == 0:
            print(x + k)
            break


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys



def check_number(p,numbers):
  for plis in p:
    if plis == numbers:
      return 1
  return 0


X,N = map(int,input().split())
p = list(map(int,input().split()))

if N == 0:
  print( X )
  sys.exit()


after_ans = X
i = 100


for i in range(X,X+100,1):#前半
  if check_number(p,i) == 0:
    after_ans = i
    after_abs = abs(X-i)
    break

for i in range(X,X-100,-1):#後半
  if check_number(p,i) == 0:
    before_ans = i
    before_abs = abs(X-i)
    break

if abs(after_abs) == abs(before_abs):
  answer = before_ans
elif abs(after_abs) > abs(before_abs):
  answer = before_ans
else:
  answer = after_ans

print( answer )

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
if n != 0:
    P = list(map(int, input().split()))
else:
    print(x)
    exit()

d = 0
while True:
    if (x - d) not in P:
        print(x - d)
        exit()
    elif (x + d) not in P:
        print(x + d)
        exit()
    d += 1

Submission status: Accepted

Solution code:
x, n = map(int,input().split())
if n == 0:
    ans = x
else:
    p = list(map(int,input().split()))
    i = 0
    while True:
        if not(x - i in p):
            ans = x - i
            break
        if not(x + i in p):
            ans = x + i
            break
        i += 1
print(ans)

Submission status: Accepted

Solution code:
T = list(map(int, input().split()))
S = list(map(int, input().split()))

X = T[0]
N = T[1]

cntup = X
cntdown = X
if N == 0:
    print(X)
    
else:
    for i in range(1,100):
        if cntup in S and cntdown in S:
            cntup += 1
            cntdown -=1
        elif cntup not in S and cntdown not in S:
            print(min(cntup,cntdown))
            break
        elif cntup in S and cntdown not in S:
            print(cntdown)
            break
        elif cntup not in S and cntdown in S:
            print(cntup)
            break

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n,m=map(int,input().split())
a=list(map(int,input().split()))
if len(a)==0:
  print(n)
else:
  a.sort()
  if n not in a:
    print(n)
  else:
    cnt=0
    for i in range(102):
      if i in a:
        continue
      elif abs(cnt-n)>abs(i-n):
        cnt=i
    print(cnt)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    X,N = list(map(int,input().split()))
    p = list(map(int,input().split()))
    if not X in p: # pが空配列・pにXが含まれない
        print(X)
    else:
        i = 1
        while True:
            if not (X - i) in p:
                print(X-i)
                break
            if not (X + i) in p:
                print(X+i)
                break
            i += 1

Submission status: Accepted

Solution code:
X,N=map(int,input().split())
p=[]
if N != 0:
  p=list(map(int,input().split()))
  p.sort()
i=0
while True:
  if X-i not in p:
    print(X-i)
    break
  elif X+i not in p:
    print(X+i)
    break
  i += 1

Submission status: Accepted

Solution code:
def main():
    x, n = map(int, input().split())
    p = list(map(int, input().split()))

    p.sort()

    if len(p) == 0:
        print(x)
        return

    diff_min = 1000
    min_n = 1000
    for _ in range(0, 102):
        diff = abs(_ - x)
        if diff < diff_min:
            if _ not in p:
                diff_min = diff
                min_n = _

    print(min_n)


main()

Submission status: Accepted

Solution code:
m,n = map(int, input().split())
l=list(map(int, input().split()))
for i in range(101):
    if m-i not in l:
        print(m-i)
        break
    if m+i not in l:
        print(m+i)
        break

Submission status: Accepted

Solution code:
x,n=map(int,input().split())
p=list(map(int,input().split())) 

for i in range(100) :
    if x-i not in p :
        print(x-i)
        exit()
    if x+i not in p :
        print(x+i)
        exit()

Submission status: Accepted

Solution code:
X, N=list(map(int,input().split()))
p=list(map(int,input().split()))
for i in range(N+1):
    if X-i not in p:
        print(X-i)
        break
    if X+i not in p:
        print(X+i)
        break

Submission status: Accepted

Solution code:
X,N=map(int,input().split())
p=list(map(int,input().split()))
 
ans=0
for i in range(100):
  if X-i not in p:
    ans=X-i
    break
  elif X+i not in p:
    ans=X+i
    break
 
print(ans)

Submission status: Accepted

Solution code:
number=list(map(int,input().split()))
x,n=number[0],number[1]
tmp=[]
try:
    tmp=list(map(int,input().split()))
except:
    pass
answer=x
key_m=x
key_p=x
far_p,far_m=0,0
i,j=0,0
far_answer=0
while answer==x:
    key_p=x+i
    while key_p in tmp:
        i+=1
        key_p=x+i
    key_m=x-j
    while key_m in tmp:
        j+=1
        key_m=x-j
    far_m=int(x-key_m)
    far_p=int(key_p-x)
    if far_m==far_p:
        far_answer=far_m
    else:
        far_answer=min(far_p,far_m)

    if far_answer==far_m:
        answer=key_m
        break
    else:
        answer=key_p
print(answer)

Submission status: Accepted

Solution code:
x,n = map(int,input().split())
p = [int(i) for i in input().split()]
x_abs = 100000
ans = []
for i in range(-2,102):
    if i in p:
        continue
    if x_abs > abs(x-i):
        ans = i
        x_abs = abs(x-i)
    elif x_abs == abs(x-1):
        ans = min(ans, i)
print(ans)

Submission status: Accepted

Solution code:
x,n = map(int,input().split())
p = [int(i) for i in input().split()]
x_abs = 100000
ans = []
for i in range(-2,102):
    if i in p:
        continue
    if x_abs > abs(x-i):
        ans = i
        x_abs = abs(x-i)
    elif x_abs == abs(x-1):
        ans = min(ans, i)
print(ans)

Submission status: Accepted

Solution code:
X,N = [int(i) for i in input().split()]

if N == 0 :
  print(X)
  exit()
  
p = [int(j) for j in input().split()]
p.sort()

ans = 0
m = 0
while True :
  if not X + m in p:
    if not X - m in p:
      ans = X-m
      break
    else :
      ans = X+m
      break
  if not X - m in p :
    ans = X-m
    break
  m += 1
  
print(ans)

Submission status: Accepted

Solution code:
x,y = map(int,input().split())
list = list(map(int, input().split()))
check_x = x
while True:
    if check_x not in list:
        break
    check_x -= 1

check_x_high = x
while True:
    check_x_high += 1
    if check_x_high not in list:
        break

if check_x_high-x < x-check_x:
    print(check_x_high)
else:
    print(check_x)

Submission status: Accepted

Solution code:
X, N = map(int, input().split())
p = list(map(int, input().split()))

for d in range(X + 1):
    for s in [-1, +1]:
        a = X + s * d
        if p.count(a) == 0:
            print(a)
            exit(0)

Submission status: Accepted

Solution code:
X, N = map(int,input().split())
p_list = list(map(int,input().split()))

temp = -1
for i in range(102):
  if(temp == -1):
    if(i not in p_list):
      temp = abs(X - i)
      ans = i
  elif(temp > abs(X-i) and i not in p_list):
   temp = abs(X-i)
   ans = i

print(ans)

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
p = list(map(int, input().split()))

if n == 0:
    ans = x
else:
    ans = float('inf')

d = float('inf')

for i in range(n+2):
    tmpx = x + 1 + n//2 - i
    if not tmpx in p:
        tmpd = abs(x -tmpx) 
        if d >= tmpd:
            d = tmpd
            if ans >= tmpx:
                ans = tmpx

print(ans)

Submission status: Accepted

Solution code:
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

X, N = LI()
P = LI()
# u = X
# d = X
# Pに含まれない整数（負も含まれる）で最もXに近くて最小の値を探す

if N == 0:
    print(X)
    sys.exit()

elif (N != 0) and (X not in P):
    print(X)
    sys.exit()

Q = [q for q in range(-1, 102) if q not in P]  # -1以上101以下整数のうちPに存在しないもの
ans = min(Q, key=lambda x: (abs(x - X), x))  # maspyさん解法
# python の min() は、順序比較のためのキーを指定できて、abs(x-X) が最小のもののうち x が最小のものをとるとかはこんな感じで書いたりできます。
print(ans)

Submission status: Accepted

Solution code:
X,N = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]

if N != 0:
    if X in p:
        sorted_p = sorted(p)

        x = sorted_p.index(X)
            
        def checknum(d,loweridx,upperidx):
            upper = sorted_p[upperidx]
            lower = sorted_p[loweridx]
            if X-d == lower:
                if X+d == upper:
                    if loweridx == 0:
                        print(X-d-1)
                    elif upperidx == N-1 and sorted_p[loweridx-1] == X-d-1:
                        print(X+d+1)
                    else:
                        checknum(d+1,loweridx-1,upperidx+1)
                else:
                    print(X+d)        
            else:
                return print(X-d)

        if x == 0:
            print(X-1)
        elif x == N-1:
            if sorted_p[x-1] != X-1:
                print(X-1)
            else:
                print(X+1)
        else:
            checknum(1,x-1,x+1)
    else:
        print(X)
else:
    print(X)

Submission status: Accepted

Solution code:
x,n = map(int,input().split())
l = list(map(int,input().split()))
s = set(l)
o = []
d = {}
for i in range(-1000,1000):
    if i not in s:
        g = abs(i-x)
        o.append(g)
        if g in d:
            h = d[g]
            h.append(i)
            d[g] = h

        else:
            d[g] = [i]

o.sort()
k = o[0]
n = d[k]
n.sort()
print(n[0])

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
if n > 0:
	p = list(map(int, input().split()))
else:
	p = []

distance = 1000
foundminimum = None
for i in range(102):
	if i in p:
		continue
	if distance > abs(x - i):
		distance = abs(x - i)
		foundminimum = i

print(foundminimum)

Submission status: Accepted

Solution code:
import sys
import numpy as np
from heapq import heappush, heappop 
from bisect import bisect_left as bi_l, bisect_right as bi_r
from collections import deque, Counter, defaultdict
from itertools import combinations, product
inf = float('inf')
MOD = 10**9+7
# MOD = 998244353


class ABC001():
  def A():
    h1, h2 = map(int, sys.stdin.read().split())
    print(h1-h2)

  def B(): pass
  def C(): pass 
  def D(): pass 


class ABC002():
  def A():
    x, y = map(int, sys.stdin.readline().split())
    print(max(x, y))

  def B():
    vowels = set('aeiou')
    s = sys.stdin.readline().rstrip() 
    t = ''
    for c in s:
      if c in vowels: continue 
      t += c 
    print(t)

  def C():
    *coords, = map(int, sys.stdin.readline().split())
    def triangle_area(x0, y0, x1, y1, x2, y2):
      x1 -= x0; x2 -= x0; y1 -= y0; y2 -= y0;
      return abs(x1*y2 - x2*y1) / 2
    print(triangle_area(*coords))

  def D():
    n, m = map(int, sys.stdin.readline().split())
    edges = set()
    for _ in range(m):
      x, y = map(int, sys.stdin.readline().split())
      x -= 1; y -= 1
      edges.add((x, y))
    cand = []
    for i in range(1, 1<<n):
      s = [j for j in range(n) if i>>j & 1]
      for x, y in combinations(s, 2):
        if (x, y) not in edges: break
      else:
        cand.append(len(s))
    print(max(cand))


class ABC003():
  def A():
    n = int(sys.stdin.readline().rstrip())
    print((n+1)*5000)

  def B():
    atcoder = set('atcoder')
    s, t = sys.stdin.read().split()
    for i in range(len(s)):
      if s[i] == t[i]: continue
      if s[i] == '@' and t[i] in atcoder: continue 
      if t[i] == '@' and s[i] in atcoder: continue
      print('You will lose')
      return 
    print('You can win')

  def C():
    n, k, *r = map(int, sys.stdin.read().split())
    res = 0
    for x in sorted(r)[-k:]:
      res = (res+x) / 2
    print(res)

  def D(): pass 


class ABC004():
  def A():
    print(int(sys.stdin.readline().rstrip())*2)

  def B():
    c = [sys.stdin.readline().rstrip() for _ in range(4)]
    for l in c[::-1]:
      print(l[::-1])

  def C():
    n = int(sys.stdin.readline().rstrip())
    n %= 30
    res = list(range(1, 7))
    for i in range(n):
      i %= 5
      res[i], res[i+1] = res[i+1], res[i]
    print(''.join(map(str, res)))

  def D(): pass 


class ABC005():
  def A():
    x, y = map(int, sys.stdin.readline().split())
    print(y//x)

  def B():
    n, *t = map(int, sys.stdin.read().split())
    print(min(t))

  def C():
    t = int(sys.stdin.readline().rstrip())
    n = int(sys.stdin.readline().rstrip())
    a = [int(x) for x in sys.stdin.readline().split()]
    m = int(sys.stdin.readline().rstrip())
    b = [int(x) for x in sys.stdin.readline().split()]
    i = 0
    for p in b:
      if i == n: print('no'); return 
      while p-a[i] > t:
        i += 1
        if i == n: print('no'); return 
      if a[i] > p: print('no'); return 
      i += 1
    print('yes')

  def D():
    n = int(sys.stdin.readline().rstrip())
    d = np.array([sys.stdin.readline().split() for _ in range(n)], np.int64)
    s = d.cumsum(axis=0).cumsum(axis=1)
    s = np.pad(s, 1)
    max_del = np.zeros((n+1, n+1), dtype=np.int64)
    for y in range(1, n+1):
      for x in range(1, n+1):
        max_del[y, x] = np.amax(s[y:n+1, x:n+1] - s[0:n-y+1, x:n+1] - s[y:n+1, 0:n-x+1] + s[0:n-y+1, 0:n-x+1])
    res = np.arange(n**2+1)[:, None]
    i = np.arange(1, n+1)
    res = max_del[i, np.minimum(res//i, n)].max(axis=1)
    q = int(sys.stdin.readline().rstrip())
    p = np.array(sys.stdin.read().split(), dtype=np.int64)
    print(*res[p], sep='\n')


class ABC006():
  def A():
    n = sys.stdin.readline().rstrip()
    if '3' in n: print('YES')
    elif int(n)%3 == 0: print('YES')
    else: print('NO')

  def B():
    mod = 10007
    t = [0, 0, 1]
    for _ in range(1001001):
      t.append(t[-1]+t[-2]+t[-3]); t[-1] %= mod
    n = int(sys.stdin.readline().rstrip())
    print(t[n-1])
    
  def C():
    n, m = map(int, sys.stdin.readline().split())
    cnt = [0, 0, 0]
    if m == 1: cnt = [-1, -1, -1]
    else:
      if m & 1: m -= 3; cnt[1] += 1; n -= 1
      cnt[2] = m//2 - n 
      cnt[0] = n - cnt[2]
    if cnt[0]<0 or cnt[1]<0 or cnt[2]<0: print(-1, -1, -1)
    else: print(*cnt, sep=' ')
      
  def D():
    n, *c = map(int, sys.stdin.read().split())
    inf = float('inf')
    lis = [inf]*n 
    for x in c: lis[bi_l(lis, x)] = x 
    print(n - bi_l(lis, inf))


class ABC007():
  def A():
    n = int(sys.stdin.readline().rstrip())
    print(n-1)

  def B():
    s = sys.stdin.readline().rstrip()
    if s == 'a': print(-1)
    else: print('a')

  def C():
    r, c = map(int, sys.stdin.readline().split())
    sy, sx = map(int, sys.stdin.readline().split())
    gy, gx = map(int, sys.stdin.readline().split())
    sy -= 1; sx -=1; gy -= 1; gx -= 1
    maze = [sys.stdin.readline().rstrip() for _ in range(r)]
    queue = deque([(sy, sx)])
    dist = np.full((r, c), np.inf); dist[sy, sx] = 0
    while queue:
      y, x = queue.popleft()
      for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        i += y; j += x
        if maze[i][j] == '#' or dist[i, j] != np.inf: continue 
        dist[i, j] = dist[y, x] + 1
        queue.append((i, j))
    print(int(dist[gy, gx]))
  
  def D(): pass 


class ABC008():
  def A():
    s, t = map(int, sys.stdin.readline().split())
    print(t-s+1)

  def B():
    n, *s = sys.stdin.read().split()
    res = defaultdict(int)
    for name in s: res[name] += 1
    print(sorted(res.items(), key=lambda x: x[1])[-1][0])

  def C():
    n, *a = map(int, sys.stdin.read().split())
    a = np.array(a)
    c = n - np.count_nonzero(a[:, None]%a, axis=1)
    print(np.sum((c+1)//2/c))
  
  def D(): pass

class ABC009():
  def A():
    n = int(sys.stdin.readline().rstrip())
    print((n+1)//2)

  def B():
    n, *a = map(int, sys.stdin.read().split())
    print(sorted(set(a))[-2])
    
  def C():
    n, k = map(int, sys.stdin.readline().split())
    s = list(sys.stdin.readline().rstrip())
    cost = [1]*n
    r = k
    for i in range(n-1):
      q = []
      for j in range(i+1, n):
        if s[j] < s[i] and cost[i]+cost[j] <= r:
          heappush(q, (s[j], cost[i]+cost[j], -j))
      if not q: continue
      _, c, j = heappop(q); j = -j 
      s[i], s[j] = s[j], s[i]
      r -= c 
      cost[i] = cost[j] = 0
    print(''.join(s))
      
  def D(): pass 


class ABC010():
  def A():
    print(sys.stdin.readline().rstrip()+'pp')
  def B():
    n, *a = map(int, sys.stdin.read().split())
    tot = 0
    for x in a:
      c = 0 
      while x%2==0 or x%3==2:
        x -= 1
        c += 1
      tot += c
    print(tot)

  def C():
    sx, sy, gx, gy, t, v, n, *xy = map(int, sys.stdin.read().split())
    x, y = np.array(xy).reshape(-1, 2).T
    def dist(x1, y1, x2, y2):
      return np.sqrt((x2-x1)**2 + (y2-y1)**2)
    ans = 'YES' if (dist(sx, sy, x, y)+dist(x, y, gx, gy) <= v*t).any() else 'NO'
    print(ans)

  def D(): pass 

class ABC011():
  def A():
    n = int(sys.stdin.readline().rstrip())
    print(n%12+1)

  def B():
    s = sys.stdin.readline().rstrip() 
    print(s[0].upper()+s[1:].lower())

  def C():
    n, *ng = map(int, sys.stdin.read().split())
    ng = set(ng)
    if n in ng: print('NO')
    else:
      r = 100
      while n > 0:
        if r == 0: print('NO'); return
        for i in range(3, 0, -1):
          if (n-i) in ng: continue 
          n -= i
          r -= 1
          break 
        else: print('NO'); return 
      print('YES')

  def D(): pass 


class ABC170():
  def A():
    x = [int(x) for x in sys.stdin.readline().split()]
    for i in range(5):
      if x[i] != i+1:
        print(i+1)
        break

  def B():
    x, y = map(int, sys.stdin.readline().split())
    print('Yes' if 2*x <= y <= 4*x and y%2 == 0 else 'No')
      
  def C():
    x, n, *p = map(int, sys.stdin.read().split())
    a = list(set(range(102)) - set(p))
    a = [(abs(y-x), y) for y in a]
    print(sorted(a)[0][1])
    
  def D(): pass 
  def E(): pass 
  def F(): pass 

if __name__ == '__main__':
  ABC170.C()

Submission status: Accepted

Solution code:
import collections
x, n= map(int, input().split())
P = list(map(int, input().split()))
 
PX = list(map(lambda p: p-x,P))
CPX = collections.Counter(PX)
ans = 0

for i in range(100):
  if CPX[-i] == 0:
    ans = x -i
    break
  elif CPX[i] == 0:
    ans = x +i
    break

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
x, n = map(int, input().split())
if n != 0:
    P = list(map(int, input().split()))
    P = sorted(P)
else:
    P = []

flag = False
ans = []
if x not in P or n == 0:
    ans.append(x)
else:
    idx = bisect_left(P, x)
    for i in range(1, 110):
        for_num = P[idx] + i
        bac_num = P[idx] - i
        if for_num not in P:
            ans.append(for_num)
            flag = True
        if bac_num not in P:
            ans.append(bac_num)
            flag = True
        if flag:
            break

print(min(ans))

Submission status: Accepted

Solution code:
x, n = map(int, input().split())
if (n == 0):
    print(x)
    exit()
p = list(map(int, input().split()))
pp = list(set(sorted([i - x for i in p if i-x > 0])))
pm = list(set(sorted([abs(i - x) for i in p if i - x < 0])))
pp.append(9999)
pm.append(9999)
if ([1 for i in p if i == x]):
    pass
else:
    print(x)
    exit()
a = 1
i = 0
while 1:
    if (pm[i] != a):
        print(x - a)
        exit()
    if(pp[i] != a):
        print(x + a)
        exit()
    a += 1
    i += 1

Submission status: Accepted

Solution code:
x,n = map(int,input().split())
if n==0:
  print(x)
  exit(0)
  
lis = list(map(int,input().split()))
i=0

def judge(X):
  if (X not in lis) and (X >= 0):
    print(X)
    exit(0)
    
for j in range(100):
  judge(x-i)
  judge(x+i)
  i+=1