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)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

public class ABC170_C
{
    public static void Main()
    {
        Inline(out int x, out int n);
        if (n == 0)
        {
            Out(x);
            return;
        }
        Inline(out int[] ints);
        int res = -1;
        List<int> min = new List<int>(), max = new List<int>();
        bool same = false;
        foreach (var num in ints)
        {
            if (num < x) min.Add(Math.Abs(x - num));
            else if (num == x) same = true;
            else max.Add(Math.Abs(x - num));
        }

        for(int i = 1;i < 100; i++)
        {
            if (i == 1 && !same) res = x;
            else if (!min.Contains(i)) res = x - i;
            else if (!max.Contains(i)) res = x + i;
            else continue;
            break;
        }

        Out(res);
    }

    /* Template */
    //入力:1入力
    public static void Inline<A>(out A var)
    {
        Inline(out var, out object _, out object _, out object _, out object _);
    }
    //入力:2入力
    public static void Inline<A, B>(out A var1, out B var2)
    {
        Inline(out var1, out var2, out object _, out object _, out object _);
    }
    //入力:3入力
    public static void Inline<A, B, C>(out A var1, out B var2, out C var3)
    {
        Inline(out var1, out var2, out var3, out object _, out object _);
    }
    //入力:4入力
    public static void Inline<A, B, C, D>(out A var1, out B var2, out C var3, out D var4)
    {
        Inline(out var1, out var2, out var3, out var4, out object _);
    }
    //入力:5入力
    public static void Inline<A, B, C, D, E>(out A var1, out B var2, out C var3, out D var4, out E var5)
    {
        var o = new object[5];
        var t = new Type[] { typeof(A), typeof(B), typeof(C), typeof(D), typeof(E) };
        string[] s = Console.ReadLine().Split(' ');
        for (int i = 0; i < s.Length; i++)
        {
            if (typeof(int) == t[i]) o[i] = int.Parse(s[i]);
            else if (typeof(long) == t[i]) o[i] = long.Parse(s[i]);
            else if (typeof(double) == t[i]) o[i] = double.Parse(s[i]);
            else if (typeof(decimal) == t[i]) o[i] = decimal.Parse(s[i]);
            else o[i] = s[i];
        }
        var1 = (A)o[0];
        var2 = (B)o[1];
        var3 = (C)o[2];
        var4 = (D)o[3];
        var5 = (E)o[4];
    }

    //入力:1行配列
    public static void Inline<A>(out A[] var)
    {
        InlineArg(out var);
    }
    //入力:1行配列
    public static void InlineArg<A>(out A[] arg)
    {
        object obj = null;
        Type t = typeof(A);
        string[] line = Console.ReadLine().Split(' ');
        if (typeof(int) == t) obj = line.Select(int.Parse).ToArray();
        else if (typeof(long) == t) obj = line.Select(long.Parse).ToArray();
        else if (typeof(double) == t) obj = line.Select(double.Parse).ToArray();
        else if (typeof(decimal) == t) obj = line.Select(decimal.Parse).ToArray();
        else obj = line;
        arg = (A[])obj;
    }

    //文字列結合
    public static string JoinString(object obj, string sep)
    {
        return obj.GetType().IsArray ?
            string.Join(sep, ((IEnumerable<object>)obj).Select(i => i.ToString()).ToArray()) : obj.ToString();
    }

    //出力
    public static void Out(object o)
    {
        Console.WriteLine(o);
    }
    //出力:Yes
    public static void OutYes()
    {
        Console.WriteLine("Yes");
    }
    //出力:No
    public static void OutNo()
    {
        Console.WriteLine("No");
    }
    //出力:YES
    public static void OutYES()
    {
        Console.WriteLine("YES");
    }
    //出力:NO
    public static void OutNO()
    {
        Console.WriteLine("NO");
    }

    //最大
    public static int[] Max(int[] array, int count)
    {
        int[] result = new int[count];
        for (int c = 0; c < count; c++)
        {
            int max = -1;
            int plus = Vector<int>.Count;
            var vmax = new Vector<int>(max);
            for (int i = 0; i < array.Length - plus; i += plus)
            {
                vmax = Vector.Max(vmax, new Vector<int>(array, i));
            }
            for (int i = 0; i < plus; i++)
            {
                if (max < vmax[i]) max = vmax[i];
            }
            for (int i = array.Length - plus; i < array.Length; i++)
            {
                if (i < 0) i = 0;
                if (max < array[i]) max = array[i];
            }

            result[c] = max;
            if (c != count - 1)
            {
                var list = new List<int>(array);
                list.Remove(max);
                array = list.ToArray();
            }
        }
        return result;
    }

    //最小
    public static int[] Min(int[] array, int count)
    {
        int[] result = new int[count];
        for (int c = 0; c < count; c++)
        {
            int min = int.MaxValue;
            int plus = Vector<int>.Count;
            var vmax = new Vector<int>(min);
            for (int i = 0; i < array.Length - plus; i += plus)
            {
                vmax = Vector.Min(vmax, new Vector<int>(array, i));
            }
            for (int i = 0; i < plus; i++)
            {
                if (min > vmax[i]) min = vmax[i];
            }
            for (int i = array.Length - plus; i < array.Length; i++)
            {
                if (i < 0) i = 0;
                if (min > array[i]) min = array[i];
            }

            result[c] = min;
            if (c != count - 1)
            {
                var list = new List<int>(array);
                list.Remove(min);
                array = list.ToArray();
            }
        }
        return result;
    }

    //公約数
    public static int[] CommonDivisor(int num)
    {
        var list = new List<int>();
        int count = 0;

        for (int i = 0; i * i <= num; i++)
        {
            if (num % i == 0)
            {
                var n = num / i;
                list.Insert(count, i);
                if (i != n) list.Insert(count + 1, n);
                else break;
                count++;
            }
        }
        return list.ToArray();
    }
    public static decimal[] CommonDivisor(decimal num)
    {
        var list = new List<decimal>();
        int count = 0;

        for (decimal d = 0; d * d <= num; d++)
        {
            if (num % d == 0)
            {
                var n = num / d;
                list.Insert(count, d);
                if (d != n) list.Insert(count + 1, n);
                else break;
                count++;
            }
        }
        return list.ToArray();
    }

    //素因数分解
    public static int[] PrimeFactorization(int num)
    {
        int l = 2, n = num;
        var result = new int[0];
        while (l * l <= num)
        {
            if (n % l == 0)
            {
                n /= l;
                Array.Resize(ref result, result.Length + 1);
                result[^1] = l;
            }
            else l++;
        }
        if (n != 1)
        {
            Array.Resize(ref result, result.Length + 1);
            result[^1] = n;
        }
        return result;
    }
    public static decimal[] PrimeFactorization(decimal num)
    {
        decimal l = 2, n = num;
        var result = new decimal[0];
        while (l * l <= num)
        {
            if (n % l == 0)
            {
                n /= l;
                Array.Resize(ref result, result.Length + 1);
                result[^1] = l;
            }
            else l++;
        }
        if (n != 1)
        {
            Array.Resize(ref result, result.Length + 1);
            result[^1] = n;
        }
        return result;
    }

    //大小比較
    public static bool Compare(bool lequal, decimal a, decimal x, decimal b, bool requal)
    {
        if (a <= x && x <= b)
        {
            if ((!lequal && x == a) || (!requal && x == b)) return false;
            else return true;
        }
        return false;
    }

    //互いに素
    public static bool IsRelativelyPrime(decimal a, decimal b)
    {
        return a > 1 && b > 1 ? CommonDivisor(a).Intersect(CommonDivisor(b)).ToArray().Length == 0 : false;
    }

    //素数判定
    public static bool IsPrimeNumber(decimal num)
    {
        if (num < 2) return false;
        else if (num == 2) return true;
        else if (num % 2 == 0) return false;

        for (decimal i = 3; i * i <= num; i += 2)
        {
            if (num % i == 0) return false;
        }
        return true;
    }

    //最大公約数
    public static int GCD(int a, int b)
    {
        return (int)GCD((decimal)a, b);
    }
    public static decimal GCD(decimal a, decimal b)
    {
        decimal[] acd = CommonDivisor(a).ToArray(), bcd = CommonDivisor(b).ToArray();
        Dictionary<decimal, int> dic1 = new Dictionary<decimal, int>(), dic2 = new Dictionary<decimal, int>();
        int count = 0;
        decimal n = 0, result = 1;
        for (int i = 0; i < acd.Length; i++)
        {
            if (n == acd[i]) count++;
            else
            {
                if (n != 0) dic1.Add(n, count);
                n = acd[i];
                count = 1;
            }
        }
        dic1.Add(n, count);
        n = 0;
        count = 0;
        for (int i = 0; i < bcd.Length; i++)
        {
            if (n == bcd[i]) count++;
            else
            {
                if (dic1.ContainsKey(n)) dic2[n] = Math.Max(count, dic1[n]);
                n = bcd[i];
                count = 1;
            }
        }
        if (dic1.ContainsKey(n)) dic2[n] = Math.Max(count, dic1[n]);
        foreach (decimal d in dic2.Keys)
        {
            result *= (decimal)Math.Pow((double)d, dic2[d]);
        }
        return result;
    }

    //最小公倍数
    public static decimal LCM(decimal a, decimal b)
    {
        decimal[] acd = CommonDivisor(a).ToArray(), bcd = CommonDivisor(b).ToArray();
        var dic = new Dictionary<decimal, int>();
        int count = 0;
        decimal n = 0, result = 1;
        for (int i = 0; i < acd.Length; i++)
        {
            if (n == acd[i]) count++;
            else
            {
                if (n != 0) dic.Add(n, count);
                n = acd[i];
                count = 1;
            }
        }
        dic.Add(n, count);
        n = 0;
        count = 0;
        for (int i = 0; i < bcd.Length; i++)
        {
            if (n == bcd[i]) count++;
            else
            {
                if (dic.ContainsKey(n)) dic[n] = Math.Max(count, dic[n]);
                else if (n != 0) dic.Add(n, count);
                n = bcd[i];
                count = 1;
            }
        }
        if (dic.ContainsKey(n)) dic[n] = Math.Max(count, dic[n]);
        else dic.Add(n, count);
        foreach (decimal l in dic.Keys)
            result *= (decimal)Math.Pow((double)l, dic[l]);
        return result;
    }

    //組合せ数(nCr)
    public static decimal Combination(int n, int r)
    {
        if (n / 2 < r) r = n - r;
        return TotalPower(n, r + 1) / TotalPower(n - r);
    }

    //組合せ数総和(Σ(k=1,n,nCk))
    public static double CombinationSum(int n)
    {
        return Math.Pow(2, n);
    }

    //重複組合せ数(nHr)
    public static decimal RepetitionCombination(int n, int r)
    {
        return Combination(n + r - 1, r);
    }

    //重複組合せ数総和(Σ(k=1,n,nHk))
    public static decimal RepetitionCombinationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += RepetitionCombination(n, i);
        }
        return result;
    }

    //順列数(nPr)
    public static decimal Permutation(int n, int r)
    {
        return TotalPower(n, r + 1);
    }

    //順列数総和(Σ(k=1,n,nPk))
    public static decimal PermutationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += Permutation(n, i);
        }
        return result;
    }

    //円順列数
    public static decimal CircularPermutation(int n)
    {
        return TotalPower(n - 1);
    }

    //円順列数総和
    public static decimal CircularPermutationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += CircularPermutation(i);
        }
        return result;
    }

    //数珠順列数
    public static decimal NecklacePermutation(int n)
    {
        return CircularPermutation(n - 1) / 2;
    }

    //数珠順列数総和
    public static decimal NecklacePermutationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += NecklacePermutation(i);
        }
        return result;
    }

    //重複順列数(nΠr)
    public static decimal RepetitionPermutation(int n, int r)
    {
        return (decimal)Math.Pow(n, r);
    }

    //重複順列数総和(Σ(k=1,n,nΠk))
    public static decimal RepetitionPermutationSum(int n)
    {
        decimal result = 0;
        for (int i = 1; i <= n; i++)
        {
            result += RepetitionPermutation(n, i);
        }
        return result;
    }

    //総乗
    public static decimal TotalPower(int max)
    {
        return TotalPower(max, 1);
    }
    public static decimal TotalPower(int max, int min)
    {
        decimal result = 1;
        for (int i = min; i <= max; i++) result *= i;
        return result;
    }
    public static decimal TotalPower(int[] ints)
    {
        decimal result = 1;
        foreach (int i in ints) result *= i;
        return result;
    }

    //総和
    public static decimal TotalSum(int max)
    {
        return TotalSum(max, 1);
    }
    public static decimal TotalSum(int max, int min)
    {
        decimal result = 1;
        for (int i = min; i <= max; i++) result += i;
        return result;
    }
    public static decimal TotalSum(int[] ints)
    {
        decimal result = 1;
        foreach (int i in ints) result += i;
        return result;
    }
}