Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++) // 0から
#define REP(i, n) for (int j = i; j < n; j++) // iから

typedef long long ll;

// 英字ならture、数字ならfalse
bool isalpha(char s) {
  if (('A' <= s && s <= 'Z') || ('a' <= s && s <= 'z')) {
    return 1;
  } else {
    return 0;
  }
}
// 全ての文字が英字ならture、数字ならfalse
bool Isalpha(string s) {
  ll n;
  bool tmp = true;
  rep(i, n) {
    if (isalpha(s[i])) {
      tmp = false;
    }
  }
  return tmp;
}

using Graph = vector<vector<ll>>;

// cout<<fixed<<setprecision(10);

ll binary_search(vector<ll> a, ll n, ll key) {
  ll right = n, left = -1;
  ll md = (right + left) / 2;

  while (right - left > 1) {
    if (a[md] <= key) {
      right = md;
    } else {
      left = md;
    }
    md = (right + left) / 2;
  }
  if (left == -1)
    return -1; // 無い場合
  return right;
}

ll MIN(ll a, ll b) {
  ll tmp;
  if (a < b) {
    tmp = a;
  } else {
    tmp = b;
  }
  return tmp;
}

ll MAX(ll a, ll b) {
  ll tmp;
  if (a < b) {
    tmp = b;
  } else {
    tmp = a;
  }

  return tmp;
}

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<int> p(n);
  vector<bool> b(101, true);
  rep(i, n) {
    cin >> p[i];
    b[p[i]] = false;
  }

  int l = abs(x + 1), ans;
  for (int i = 0; i < 102; i++) {
    if (b[i]) {
      if (abs(x - i) < l) {
        ans = i;
        l = abs(x - i);
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++) // 0から
#define REP(i, n) for (int j = i; j < n; j++) // iから

typedef long long ll;

// 英字ならture、数字ならfalse
bool isalpha(char s) {
  if (('A' <= s && s <= 'Z') || ('a' <= s && s <= 'z')) {
    return 1;
  } else {
    return 0;
  }
}
// 全ての文字が英字ならture、数字ならfalse
bool Isalpha(string s) {
  ll n;
  bool tmp = true;
  rep(i, n) {
    if (isalpha(s[i])) {
      tmp = false;
    }
  }
  return tmp;
}

using Graph = vector<vector<ll>>;

// cout<<fixed<<setprecision(10);

ll binary_search(vector<ll> a, ll n, ll key) {
  ll right = n, left = -1;
  ll md = (right + left) / 2;

  while (right - left > 1) {
    if (a[md] <= key) {
      right = md;
    } else {
      left = md;
    }
    md = (right + left) / 2;
  }
  if (left == -1)
    return -1; // 無い場合
  return right;
}

ll MIN(ll a, ll b) {
  ll tmp;
  if (a < b) {
    tmp = a;
  } else {
    tmp = b;
  }
  return tmp;
}

ll MAX(ll a, ll b) {
  ll tmp;
  if (a < b) {
    tmp = b;
  } else {
    tmp = a;
  }

  return tmp;
}

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  vector<bool> b(n, true);
  rep(i, n) {
    cin >> p[i];
    b[p[i]] = false;
  }

  int l = abs(x + 1), ans;
  for (int i = 0; i < 102; i++) {
    if (b[i]) {
      if (abs(x - i) < l) {
        ans = i;
        l = abs(x - i);
      }
    }
  }

  cout << ans << endl;

  return 0;
}