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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, N) for (int i = 0; i < (N); i++)
#define erep(i, N) for (int i = N - 1; i >= 0; i--)
const ll INF = 1000000000;
// const ll MOD = 1000000007;
const ld PI = (acos(-1));
using Graph = vector<vector<int>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef pair<int, int> P;
typedef pair<ll, ll> PLL;
double rad(double a) { return a * 180 / PI; }

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

// dpTable
// int dp[100050];

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> p(N);
  rep(i, N) cin >> p.at(i);
  vector<bool> list(105, true);
  rep(i, N) list.at(p.at(i)) = false;
  int pmax, pmin;
  for (int i = X; i < 103; i++) {
    if (list.at(i)) {
      pmax = i;
      break;
    }
  }
  for (int i = X; i >= 0; i--) {
    if (list.at(i)) {
      pmin = i;
      break;
    }
  }
  if (X - pmin > pmax - X)
    cout << pmax << endl;
  else
    cout << pmin << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, N) for (int i = 0; i < (N); i++)
#define erep(i, N) for (int i = N; i >= 0; i--)
#define PI 3.1415926535

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

char upper(char c) {
  if ('a' <= c && c <= 'z') {
    c = c - ('a' - 'A');
  }
  return c;
}

char lower(char c) {
  if ('A' <= c && c <= 'Z') {
    c = c + ('a' - 'A');
  }
  return c;
}

const ll INF = 1000000000000000000;

// input
int X, N;

// processing
vector<int> Table(101, true);

// dpTable
// int dp[100050];

int main() {
  cin >> X >> N;
  vector<int> p(N);
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  bool A = false;
  rep(i, N) {
    cin >> p.at(i);
    Table.at(p.at(i)) = false;
    if (p.at(i) == X) {
      A = true;
    }
    if (!(A) && i == N - 1) {
      cout << X << endl;
      return 0;
    }
  }
  int AA = -1000000, BB = 1000000;
  for (int i = X; i > 0; i--) {
    if (Table.at(i)) {
      AA = i;
      break;
    }
  }
  for (int i = X; i <= 101; i++) {
    if (Table.at(i) == true) {
      BB = i;
      break;
    }
  }
  if (abs(X - AA) <= abs(X - BB)) {
    cout << AA << endl;
  } else {
    cout << BB << endl;
  }
  return 0;
}