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

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;
#define MOD 1000000007
// #define int                          int64_t
#define baba_shiv                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define mpii map<int, int>
#define pb push_back
#define mkp make_pair
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define ub(v, x) upper_bound(all(v), x)
#define lb(v, x) lower_bound(all(v), x)
#define bs(v, x) binary_search(all(v), x)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (((a * b)) / __gcd(a, b))
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define vs vector<string>
#define ff first
#define ss second
#define foru(v, s, e) for (int v = s; v < e; v++)
#define ford(v, s, e) for (int v = s; v > e; v--)
#define pi 3.14159265358979323846

/***********************************************************************************************************************/
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}

#define deb(x) cerr << #x << " = " << x << endl;

/*struct CustomCompare
{
    bool operator()(pii &a, pii &b)
    {
        if (a.ss == b.ss)
            return a.ff < b.ff;
        return a.ss < b.ss;
    }

};
*/

/***********************************************************************************************************************/

bool compare(pii &a, pii &b) {
  if (a.ff == b.ff) {
    return a.ss < b.ss;
  }
  return a.ff < b.ff;
}

signed main() {
  baba_shiv;
  int t = 1;
  while (t--) {
    int x, n;
    cin >> x >> n;
    if (n == 0) {
      cout << x;
      return 0;
    }
    set<int> st;
    foru(i, 0, n) {
      int x;
      cin >> x;
      st.insert(x);
    }
    vpii vec;
    for (int i = -1000; i <= 1000; i++) {
      if (!st.count(i)) {
        vec.pb({abs(x - i), i});
      }
    }
    sort(all(vec), compare);
    cout << vec.front().ss;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;
#define MOD 1000000007
// #define int                          int64_t
#define baba_shiv                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define mpii map<int, int>
#define pb push_back
#define mkp make_pair
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define ub(v, x) upper_bound(all(v), x)
#define lb(v, x) lower_bound(all(v), x)
#define bs(v, x) binary_search(all(v), x)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (((a * b)) / __gcd(a, b))
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define vs vector<string>
#define ff first
#define ss second
#define foru(v, s, e) for (int v = s; v < e; v++)
#define ford(v, s, e) for (int v = s; v > e; v--)
#define pi 3.14159265358979323846

/***********************************************************************************************************************/
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}

#define deb(x) cerr << #x << " = " << x << endl;

/*struct CustomCompare
{
    bool operator()(pii &a, pii &b)
    {
        if (a.ss == b.ss)
            return a.ff < b.ff;
        return a.ss < b.ss;
    }

};
*/

/***********************************************************************************************************************/

signed main() {
  baba_shiv;
  int t = 1;
  while (t--) {
    int x, n;
    cin >> x >> n;
    set<int> st;
    foru(i, 0, n) {
      int x;
      cin >> x;
      st.insert(x);
    }
    vpii vec;
    int left = 0;
    int t = x;

    while (t >= 0) {
      if (!st.count(t)) {
        vec.pb({abs(x - t), t});
        break;
      }
      t--;
    }
    t = x;
    int right;
    while (t <= *st.rbegin() + 1) {
      if (!st.count(t)) {
        vec.pb({abs(x - t), t});
        break;
      }
      t++;
    }
    sort(all(vec));
    cout << vec.front().ss << endl;
  }
}