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

#include <bits/stdc++.h>
#define ll long long int
#define ss second
#define ff first
#define Mem(x, val) memset(x, val, sizeof x)
#define pb push_back
#define all(v) v.begin(), v.end()
#define unik(v) v.erase(unique(v.begin(), v.end()), v.end())
#define cerr(x) cerr << __LINE__ << " th Line : " << #x << " = " << x << "\\n"
#define debug(a, b)                                                            \\
  cerr << #a << " = " << a << "       |      " << #b << " = " << b << '\\n'
#define endl '\\n'
#define SZ(a) ((int)a.size())
using namespace std;
typedef pair<int, int> pi32;
typedef pair<ll, ll> pi64;
typedef vector<int> vi32;
typedef vector<ll> vi64;
typedef vector<pi32> vp32;
typedef vector<pi64> vp64;
const ll mod = 1e9 + 7;
const int MX = 1e5 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vi32 a(n);
  for (int &X : a)
    cin >> X;
  sort(all(a));
  if (x == a[0]) {
    cout << a[0] - 1 << endl;
    return 0;
  }
  if (x < a[0] || x > a[n - 1]) {
    cout << x << endl;
    return 0;
  }
  vi32 b;
  for (int it : a)
    b.pb(it);
  b.pb(-100);
  b.pb(105);
  sort(all(b));
  int lb = lower_bound(b.begin(), b.end(), x) - b.begin();
  int mn1 = 1e9;
  int n1 = -1;
  // cerr(lb);
  for (int i = lb; i > 0; i--) {
    if (b[i] - b[i - 1] > 1) {
      mn1 = abs(x - (b[i] - 1));
      n1 = b[i] - 1;
      break;
    }
  }
  int mn2 = 1e9;
  int n2 = -1;
  for (int i = lb; i + 1 < SZ(b); i++) {
    if (b[i + 1] - b[i] > 1) {
      mn2 = abs(x - (b[i] + 1));
      n2 = b[i] + 1;
      break;
    }
  }
  // debug(mn1,mn2);
  // debug(n1,n2)<<endl;
  if (mn1 <= mn2)
    cout << n1 << endl;
  else
    cout << n2 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ss second
#define ff first
#define Mem(x, val) memset(x, val, sizeof x)
#define pb push_back
#define all(v) v.begin(), v.end()
#define unik(v) v.erase(unique(v.begin(), v.end()), v.end())
#define cerr(x) cerr << __LINE__ << " th Line : " << #x << " = " << x << "\\n"
#define debug(a, b)                                                            \\
  cerr << #a << " = " << a << "       |      " << #b << " = " << b << '\\n'
#define endl '\\n'
#define SZ(a) ((int)a.size())
using namespace std;
typedef pair<int, int> pi32;
typedef pair<ll, ll> pi64;
typedef vector<int> vi32;
typedef vector<ll> vi64;
typedef vector<pi32> vp32;
typedef vector<pi64> vp64;
const ll mod = 1e9 + 7;
const int MX = 1e5 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int x, n;
  cin >> x >> n;
  vi32 a(n);
  for (int &X : a)
    cin >> X;
  sort(all(a));

  if (x == a[0]) {
    cout << a[0] - 1 << endl;
    return 0;
  }
  if (x < a[0] or n == 0 or x > a[n - 1]) {
    cout << x << endl;
    return 0;
  }
  vi32 b = a;
  b.pb(0);
  b.pb(105);
  sort(all(b));
  int lb = lower_bound(b.begin(), b.end(), x) - b.begin();
  int mn1 = -1;
  int n1 = -1;
  // cerr(lb);
  for (int i = lb; i > 0; i--) {
    if (b[i] - b[i - 1] > 1) {
      mn1 = abs(x - (b[i] - 1));
      n1 = b[i] - 1;
      break;
    }
  }
  int mn2 = -1;
  int n2 = -1;
  for (int i = lb; i + 1 < SZ(b); i++) {
    if (b[i + 1] - b[i] > 1) {
      mn2 = abs(x - (b[i] + 1));
      n2 = b[i] + 1;
      break;
    }
  }
  // debug(mn1,mn2);
  // debug(n1,n2)<<endl;
  if (mn1 <= mn2)
    cout << n1 << endl;
  else
    cout << n2 << endl;
  return 0;
}