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

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
#define fil(x) memset(x, 0, sizeof(x))
#define fillb(x) memset(x, false, sizeof(x))
#define fillm(x) memset(x, -1, sizeof(x))
#define mp make_pair
#define pll pair<int, int>
#define pf printf
#define sf scanf
#define all(x) x.begin(), x.end()
#define pb push_back
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define np std::string::npos
#define fo(i, n) for (int i = 0; i < n; i++)
#define fo1(i, n) for (int i = 1; i < n; i++)
#define fof(i, a, b) for (int i = a; i <= b; i++)
#define off(i, a, b) for (int i = a; i >= b; i--)
#define of(i, n) for (int i = n - 1; i >= 0; i--)
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
#define Inf INFINITY
#define minv(v) *min_element(v.begin(), v.end())
#define maxv(v) *max_element(v.begin(), v.end())
#define cases(cs, t) for (int cs = 1; cs <= t; cs++)
#define PI acos(-1)
#define nl endl

typedef unsigned long long lll;
typedef long long ll;
int knigtx8[] = {2, 2, -2, -2, 1, 1, -1, -1}; /// knight move in chess
int knigty8[] = {1, -1, 1, -1, 2, -2, 2, -2}; /// knight move in chess
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};      /// for 8 directions
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};      /// for 8 directions
int dx4[] = {0, 0, 1, -1};                    /// for 4 directions
int dy4[] = {1, -1, 0, 0};                    /// for 4 directions
const int maxx = 100005;

// this fuction sorts vector pair according to first element in descending
// order.
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first > b.first;
}
ll pw(ll a, ll b) {
  ll ans = 1;
  for (ll i = 1; i <= b; ++i)
    ans = (ans * a);
  return ans;
}
template <typename T> inline T Bigmod(T base, T power, T MOD) {
  T ret = 1;
  while (power) {
    if (power & 1)
      ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}

int main() {
  IOS;
  int n, x;
  cin >> x >> n;
  int a[n], ache[115];
  fil(ache);
  // int mn = 1000;
  vector<pair<int, int>> v;
  fo(i, n) {
    cin >> a[i];
    ache[a[i]] = 1;
    // v.pb({abs(a[i]-x), a[i]});
  }
  fo(i, 110) {
    if (!ache[i]) {
      v.pb({abs(i - x), i});
    }
  }
  sort(all(v));
  cout << v[0].second << endl;
}

/// Alhamdulillah


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
#define fil(x) memset(x, 0, sizeof(x))
#define fillb(x) memset(x, false, sizeof(x))
#define fillm(x) memset(x, -1, sizeof(x))
#define mp make_pair
#define pll pair<int, int>
#define pf printf
#define sf scanf
#define all(x) x.begin(), x.end()
#define pb push_back
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define np std::string::npos
#define fo(i, n) for (int i = 0; i < n; i++)
#define fo1(i, n) for (int i = 1; i < n; i++)
#define fof(i, a, b) for (int i = a; i <= b; i++)
#define off(i, a, b) for (int i = a; i >= b; i--)
#define of(i, n) for (int i = n - 1; i >= 0; i--)
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
#define Inf INFINITY
#define minv(v) *min_element(v.begin(), v.end())
#define maxv(v) *max_element(v.begin(), v.end())
#define cases(cs, t) for (int cs = 1; cs <= t; cs++)
#define PI acos(-1)
#define nl endl

typedef unsigned long long lll;
typedef long long ll;
int knigtx8[] = {2, 2, -2, -2, 1, 1, -1, -1}; /// knight move in chess
int knigty8[] = {1, -1, 1, -1, 2, -2, 2, -2}; /// knight move in chess
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};      /// for 8 directions
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};      /// for 8 directions
int dx4[] = {0, 0, 1, -1};                    /// for 4 directions
int dy4[] = {1, -1, 0, 0};                    /// for 4 directions
const int maxx = 100005;

// this fuction sorts vector pair according to first element in descending
// order.
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first > b.first;
}
ll pw(ll a, ll b) {
  ll ans = 1;
  for (ll i = 1; i <= b; ++i)
    ans = (ans * a);
  return ans;
}
template <typename T> inline T Bigmod(T base, T power, T MOD) {
  T ret = 1;
  while (power) {
    if (power & 1)
      ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}

int main() {
  IOS;
  int n, x;
  cin >> x >> n;
  int a[n], ache[101];
  fil(ache);
  // int mn = 1000;
  vector<pair<int, int>> v;
  fo(i, n) {
    cin >> a[i];
    ache[a[i]] = 1;
    // v.pb({abs(a[i]-x), a[i]});
  }
  fo(i, 110) {
    if (!ache[i]) {
      v.pb({abs(i - x), i});
    }
  }
  sort(all(v));
  cout << v[0].second << endl;
}

/// Alhamdulillah