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.

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

namespace Test
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection.PortableExecutable;
    using System.Text;
    using System.Xml.Serialization;

    public class Test
    {
        public static void Main(string[] args)
        {
            var t = new Test();
            var xn = t.ParseIntList(t.ReadInput());
            var dic = new Dictionary<int, int>();
            var l = int.MaxValue;
            var temp = int.MaxValue;
            var temp2 = int.MaxValue;
            var r = int.MaxValue;
            var ans = int.MaxValue;

            if (xn[1] == 0)
            {
                ans = xn[0];
            }
            else
            {
                var p = t.ParseIntList(t.ReadInput());
                p.ForEach(v => dic.Add(v, v));
                p.Sort();
                //左から
                for (var i = 0; i < p.Count; i++)
                {
                    temp = xn[0] - p[i];
                    if (temp <= 0)
                    {
                        l = p[i];
                        break;
                    }
                }

                //候補が2つの場合右からも
                if (temp < 0)
                {
                    for (var i = p.Count - 1; i > 0; i--)
                    {
                        temp2 = xn[0] - p[i];
                        if (temp2 >= 0)
                        {
                            r = p[i];
                            break;
                        }
                    }
                }
                else
                {
                    r = l;
                }

                //候補が1つの場合
                if (l == r)
                {
                    var loopCount = 1;
                    while (true)
                    {
                        var lm = l - loopCount;
                        var lp = l + loopCount;
                        if (!t.DicSearch(lm, dic))
                        {
                            ans = lm;
                            break;
                        }
                        if (!t.DicSearch(lp, dic))
                        {
                            ans = lp;
                            break;
                        }
                        loopCount++;
                    }
                }
                //2つの場合
                else
                {
                    var loopCount = 1;
                    while (true)
                    {
                        var lm = l + loopCount;
                        var lp = r + loopCount;
                        if (!t.DicSearch(lm, dic))
                        {
                            ans = lm;
                            break;
                        }
                        if (!t.DicSearch(lp, dic))
                        {
                            ans = lp;
                            break;
                        }
                        loopCount++;
                    }
                }
            }
            Console.WriteLine(ans); 
        }

        public bool DicSearch(int x,Dictionary<int,int> dic)
        {
            return dic.ContainsKey(x);
        }


        public string[] ReadInput()
        {
            return Console.ReadLine().Split(' ');
        }

        public List<int> ParseIntList(string[] x)
        {
            var convertedList = new List<int>(x.Length);
            for (var i = 0; i < x.Length; i++)
            {
                convertedList.Add(int.Parse(x[i]));
            }

            return convertedList;
        }


        public List<double> ParseDoubleList(string[] x)
        {
            var convertedList = new List<double>(x.Length);
            for (var i = 0; i < x.Length; i++)
            {
                convertedList.Add(double.Parse(x[i]));
            }

            return convertedList;
        }

        public List<long> ParseLongList(string[] x)
        {
            var convertedList = new List<long>(x.Length);
            for (var i = 0; i < x.Length; i++)
            {
                convertedList.Add(long.Parse(x[i]));
            }

            return convertedList;
        }

        public int CountContainWords(string source, string word)
        {
            var count = 0;
            var loopCount = source.Length - (word.Length - 1);
            for (var i = 0; i < loopCount; i++)
            {
                if (source.Substring(i, word.Length) == word)
                {
                    count++;
                }
            }
            return count;
        }
    }
}