alexjercan/bugnet huggingface dataset:
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int INF = 2e9;

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  sort(p.begin(), p.end());
  queue<int> que;
  int j = 0;
  rep(i, 102) {
    if (i == p[j])
      j++;
    else
      que.push(i);
  }
  int s = que.front();
  que.pop();
  int ans = 2e9;
  int res = 2e9;
  while (!que.empty()) {
    int t = que.front();
    que.pop();
    int a = abs(s - x);
    int b = abs(t - x);
    if (a <= b) {
      if (res > a) {
        res = a;
        ans = s;
      }
    } else {
      if (res > b) {
        res = a;
        ans = t;
      }
    }
    s = t;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int INF = 2e9;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  sort(p.begin(), p.end());
  queue<int> que;
  int j = 0;
  rep(i, 102) {
    if (i == p[j])
      j++;
    else
      que.push(i);
  }
  int s = que.front();
  que.pop();
  int ans = 2e9;
  int res = 2e9;
  while (!que.empty()) {
    int t = que.front();
    que.pop();
    int a = abs(s - x);
    int b = abs(t - x);
    if (a <= b) {
      if (res > a) {
        res = a;
        ans = s;
      }
    } else {
      if (res > b) {
        res = a;
        ans = t;
      }
    }
    s = t;
  }
  cout << ans << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> p(N);
  rep(i, N) cin >> p[i];

  vector<bool> isValid(102, true);
  rep(i, N) { isValid[p[i]] = false; }

  int ans;
  int tmp = 1000;
  rep(i, 102) {
    if (isValid[i] && abs(X - i) < tmp) {
      ans = i;
      tmp = abs(X - i);
    }
  }

  cout << ans << endl;

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  int X, N;
  vector<int> p(N);
  cin >> X >> N;
  rep(i, N) cin >> p[i];

  vector<bool> isValid(102, true);
  rep(i, N) { isValid[p[i]] = false; }

  int ans;
  int tmp = 1000;
  rep(i, 102) {
    if (isValid[i] && abs(X - i) < tmp) {
      ans = i;
      tmp = abs(X - i);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> v(102);
  rep(i, n) {
    int p;
    cin >> p;
    v[p] = 1;
  }
  P ans(99999, -1);
  for (int i = 0; i <= 101; ++i) {
    if (v[i] == 1)
      continue;
    int dif = abs(x - i);
    ans = min(ans, P(dif, i));
  }
  cout << ans.second << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int x, n;
  int a = 0, b = 0, p, m;
  int s = 1;
  cin >> x >> n;
  p = x;
  m = x;
  std::vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v.at(i);
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    if (x == v.at(i))
      a = i + 1, b = i - 1, p++, m--;
    else
      std::cout << x << '\\n';
    return 0;
  }
  while (s == 1) {
    if (v.at(a) != p)
      s = 0;
    else if (v.at(b) != m)
      s = 0;
    else
      p++, m--;
    a++, b--;
    if (a == n || b == -1)
      s = 0;
  }
  if (p - x <= x - m)
    cout << p << endl;
  else
    cout << m << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
int x, n;
int num[105];

int find(int j) {
  int low = 0, high = n - 1, mid;

  while (low <= high) {
    mid = (low + high) / 2;
    if (num[mid] == j) {
      return 1;
    } else if (num[mid] < j) {
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return 0;
}
int main() {

  int ans1, ans2, j = 0;

  cin >> x >> n;

  if (n == 0) {
    cout << x << endl;
    return 0;
  }

  for (int a = 0; a < n; a++) {
    cin >> num[a];
  }

  sort(num, num + n);

  while (1) {
    ans1 = find(x + j);
    ans2 = find(x - j);

    if (ans1 == 0 && ans2 == 0) {
      cout << min(x + j, x - j);
      return 0;
    } else if (ans1 == 0) {
      cout << (x + j) << endl;
      return 0;
    } else if (ans2 == 0) {
      cout << (x - j) << endl;
      return 0;
    } else {
      j++;
    }
  }
}

Error: 1

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int x, n;
int num[100];

int find(int j) {
  int low = 0, high = n - 1, mid;

  while (low <= high) {
    mid = (low + high) / 2;
    if (num[mid] == j) {
      return 1;
    } else if (num[mid] < j) {
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return 0;
}
int main() {

  int ans1, ans2, j = 0;

  cin >> x >> n;

  if (n == 0) {
    cout << x << endl;
    return 1;
  }

  for (int a = 0; a < n; a++) {
    cin >> num[a];
  }

  sort(num, num + n);

  while (1) {
    ans1 = find(x + j);
    ans2 = find(x - j);

    if (ans1 == 0 && ans2 == 0) {
      cout << min(x + j, x - j);
      return 1;
    } else if (ans1 == 0) {
      cout << (x + j) << endl;
      return 1;
    } else if (ans2 == 0) {
      cout << (x - j) << endl;
      return 1;
    } else {
      j++;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int solve(long long X, long long N, std::vector<long long> p) {
  long long index = 0;
  long long q_index = 0;
  long long absx;
  long long mina = 9999999999;
  long long ans;
  vector<long long> q;
  sort(p.begin(), p.end());

  if (N == 0) {
    cout << X << endl;
    return 0;
  }

  for (int i = -100; i <= 200; i++) {
    if (p[index] != i) {

      q.push_back(i);
      q_index++;
    } else {
      index++;
    }
  }

  for (int i = 0; i < q_index; i++) {
    absx = abs(X - q[i]);
    if (mina > absx) {
      mina = min(mina, absx);
      ans = q[i];
    }
  }
  cout << ans << endl;
  return 0;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long X;
  scanf("%lld", &X);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> p(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &p[i]);
  }
  solve(X, N, std::move(p));
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int solve(long long X, long long N, std::vector<long long> p) {
  long long index = 0;
  long long q_index = 0;
  long long absx;
  long long mina = 9999999999;
  long long ans;
  vector<long long> q;
  sort(p.begin(), p.end());

  if (N == 0) {
    cout << X << endl;
    return 0;
  }

  for (int i = 1; i <= 100; i++) {
    if (p[index] != i) {

      q.push_back(i);
      q_index++;
    } else {
      index++;
    }
  }

  for (int i = 0; i <= q_index; i++) {
    absx = abs(X - q[i]);
    if (mina > absx) {
      mina = min(mina, absx);
      ans = q[i];
    }
  }
  cout << ans << endl;
  return 0;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long X;
  scanf("%lld", &X);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> p(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &p[i]);
  }
  solve(X, N, std::move(p));
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
using ll = long long;
template <typename T> T sq(T x) { return x * x; }

signed main() {
  int X, N;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  vector<int> p(200);
  rep(i, N) {
    int a;
    cin >> a;
    p.at(a)++;
  }
  rep(i, 200) {
    int Xp = X + i;
    int Xm = X - i;
    if (p.at(Xm) == 0) {
      cout << Xm << endl;
      return 0;
    }
    if (p.at(Xp) == 0) {
      cout << Xp << endl;
      return 0;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
using ll = long long;
template <typename T> T sq(T x) { return x * x; }

signed main() {
  int X, N;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  vector<int> p(100);
  rep(i, N) {
    int a;
    cin >> a;
    p.at(a)++;
  }
  rep(i, N / 2 + 2) {
    int Xp = X + i;
    int Xm = X - i;
    if (p.at(Xm) == 0) {
      cout << Xm << endl;
      return 0;
    }
    if (p.at(Xp) == 0) {
      cout << Xp << endl;
      return 0;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> d(102);
  rep(i, n) {
    int p;
    cin >> p;
    d[p] = 1;
  }
  P ans(99999, -1);
  rep(i, 102) {
    if (d[i] == 1)
      continue;
    int dif = abs(x - i);
    ans = min(ans, P(dif, i));
  }
  cout << ans.second << endl;
  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, cc, N) for (int i = cc; i <= N; ++i)
#define all(x) (x).begin(), (x).end()
int INF = 101;

int main() {
  int x, n;
  cin >> x >> n;
  int min = INF, num, def;
  vector<int> p(102);
  rep(i, 0, 102) { p[i] = 0; }
  int a;
  rep(i, 0, n) {
    cin >> a;
    p[a] = 1;
  }
  rep(i, 0, 102) {
    if (p[i] == 1)
      continue;
    def = abs(i - x);
    if (min > def) {
      min = def;
      num = i;
    }
  }
  cout << num << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() { /* ここに書く */

  int X, N, q, P, M, ans, j;
  j = 0;
  ans = 0;

  cin >> X >> N;
  vector<bool> vec(101);

  for (int i = 0; i < N; i++) {

    cin >> q;
    vec.at(q) = true;
  }

  bool p, m;

  while (ans == 0) {

    P = X + j;
    M = X - j;

    if (P < 101) {

      p = vec.at(P);

      if (p == false) {

        ans = P;
      }
    }

    else {
      ans = 101;
    }

    // cout << p << ' ' << P << ' ' << ans <<endl;

    if (M > 0) {

      m = vec.at(M);

      if (m == false) {

        ans = M;
      }
    }

    else {
      ans = 0;
      break;
    }

    // cout << m << ' ' << M << ' ' << ans << endl;

    j++;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() { /* ここに書く */

  int X, N, q, P, M, ans, j;
  j = 0;
  ans = 0;

  cin >> X >> N;
  vector<bool> vec(101);

  for (int i = 0; i < N; i++) {

    cin >> q;
    vec.at(q) = true;
  }

  bool p, m;

  while (ans == 0) {

    P = X + j;
    M = X - j;

    if (P < 101) {

      p = vec.at(P);

      if (p == false) {

        ans = P;
      }
    }

    // cout << p << ' ' << P << ' ' << ans <<endl;

    if (M > 0) {

      m = vec.at(M);

      if (m == false) {

        ans = M;
      }
    }

    // cout << m << ' ' << M << ' ' << ans << endl;

    j++;
  }

  cout << ans << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
using P = pair<int, int>;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> d(102);
  for (int i = 0; i < n; ++i) {
    int p;
    cin >> p;
    d[p] = 1;
  }

  P ans(99999, -1);
  for (int i = 0; i <= 101; ++i) {
    if (d[i] == 1)
      continue;
    int dif = abs(x - i);
    ans = min(ans, P(dif, i));
  }
  cout << ans.second << endl;
  return 0;
}


Error: -6
malloc(): corrupted top size


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using P = pair<int, int>;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> d(n);
  for (int i = 0; i < n; ++i) {
    int p;
    cin >> p;
    d[p] = 1;
  }

  P ans(99999, -1);
  for (int i = 0; i <= 101; ++i) {
    if (d[i] == 1)
      continue;
    int dif = abs(x - i);
    ans = min(ans, P(dif, i));
  }
  cout << ans.second << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

void printVector(vector<int> v) {
  for (auto x : v) {
    cout << x << ' ';
  }
  cout << '\\n';
}

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> v1(101);
  vector<int> v2;
  for (int i = 0; i <= n - 1; i++) {
    int a;
    cin >> a;
    v1[a] = 1;
  }
  // printVector(v1);
  v2.push_back(0);
  for (int i = 1; i <= 100; i++) {
    if (!v1[i])
      v2.push_back(i);
  }
  v2.push_back(101);
  // printVector(v2);
  auto lb = lower_bound(v2.begin(), v2.end(), x) - v2.begin();
  auto gb = upper_bound(v2.begin(), v2.end(), x) - v2.begin();
  // x fora de v2
  if (lb == gb) {
    if (v2[lb] - x < x - v2[lb - 1])
      cout << v2[lb] << '\\n';
    else
      cout << v2[lb - 1] << '\\n';
  }
  // x em v2
  else {
    cout << x << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

void printVector(vector<int> v) {
  for (auto x : v) {
    cout << x << ' ';
  }
  cout << '\\n';
}

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> v1(100);
  vector<int> v2;
  for (int i = 0; i <= n - 1; i++) {
    int a;
    cin >> a;
    v1[a] = 1;
  }
  for (int i = 1; i <= 100; i++) {
    if (!v1[i]) {
      v2.push_back(i);
    }
  }
  // printVector(v2);
  int m = v2.size();
  auto lb = lower_bound(v2.begin(), v2.end(), x) - v2.begin();
  auto gb = upper_bound(v2.begin(), v2.end(), x) - v2.begin();
  // Elemento não está em v2
  if (lb == gb) {

    if (lb == m) {
      cout << v2[lb - 1] << '\\n';
    } else if (v2[lb] - x < x - v2[lb - 1]) {
      cout << v2[lb] << '\\n';
    } else {
      cout << v2[lb - 1] << '\\n';
    }
  }
  // Elemento está em v2, então é a resposta!
  else {
    cout << x << '\\n';
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>; // グラフ型
typedef long long ll;
#define _GLIBCXX_DEBUG

int main() {
  int X, N, ans, buf, buf1;
  cin >> X >> N;
  vector<int> p(N);
  vector<int> q;

  if (N == 0) {
    cout << X << endl;
    return 0;
  }

  if (N == 100) {
    if (X <= 50) {
      cout << 0 << endl;
      return 0;
    } else {
      cout << 101 << endl;
      return 0;
    }
  }

  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }

  sort(p.begin(), p.end());

  int tmp = 0;
  for (int i = 0; i <= 101; i++) {
    if (i == p[tmp])
      tmp++;
    // else if (i == X) continue;
    else {
      q.push_back(i);
    }
  }

  sort(q.begin(), q.end());

  buf = 200;
  /*
  for (ll i=0; i<q.size(); i++){
    cout << q[i] << endl;
  }
  */

  ans = 0;
  for (int i = 0; i < q.size(); i++) {
    buf1 = abs(q[i] - X);
    if (buf > buf1) {
      buf = buf1;
      ans = i;
    } else {
      break;
    }
  }
  cout << q[ans] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>; // グラフ型
typedef long long ll;
#define _GLIBCXX_DEBUG

int main() {
  ll X, N, ans, buf, buf1;
  cin >> X >> N;
  vector<ll> p(N);
  vector<ll> q;

  if (N == 0) {
    cout << X << endl;
    return 0;
  }

  for (ll i = 0; i < N; i++) {
    cin >> p[i];
  }

  sort(p.begin(), p.end());

  ll tmp = 0;
  for (ll i = 1; i <= 100; i++) {
    if (i == p[tmp])
      tmp++;
    // else if (i == X) continue;
    else {
      q.push_back(i);
    }
  }

  sort(q.begin(), q.end());

  buf = 110;
  /*
  for (ll i=0; i<q.size(); i++){
    cout << q[i] << endl;
  }
  */

  for (ll i = 0; i < q.size(); i++) {
    buf1 = abs(q[i] - X);
    if (buf > buf1) {
      buf = buf1;
      ans = i;
    } else {
      break;
    }
  }
  cout << q[ans] << endl;

  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int target, n;
  cin >> target >> n;

  if (n == 0) {
    cout << target << endl;
    return 0;
  }

  vector<int> ex(n);

  for (int i = 0; i < n; i++) {
    cin >> ex.at(i);
  }

  vector<int> dx(202);

  for (int i = 0; i < n; i++) {
    dx.at(ex.at(i)) = 1;
  }

  for (int i = 0; i < 101; i++) {
    if (dx.at(target - i) == 0) {
      cout << target - i << endl;
      break;
    }
    if (dx.at(target + i) == 0) {
      cout << target + i << endl;
      break;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int target, n;
  cin >> target >> n;

  if (n == 0) {
    cout << target << endl;
    return 0;
  }

  vector<int> ex(n);

  for (int i = 0; i < n; i++) {
    cin >> ex.at(i);
  }

  vector<int> dx(101);

  for (int i = 0; i < n; i++) {
    dx.at(ex.at(i)) = 1;
  }

  for (int i = 0; i < 101; i++) {
    if (dx.at(target - i) == 0) {
      cout << target - i << endl;
      break;
    }
    if (dx.at(target + i) == 0) {
      cout << target + i << endl;
      break;
    }
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
typedef long long ll;
namespace mp = boost::multiprecision;
using Mint = mp::cpp_int;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define sort_g(v) sort(all(v), greater<int>())
#define max_v(v) *max_element(all(v));
#define min_v(v) *min_element(all(v));
#define dump(x) cerr << #x << ' ' << '=' << ' ' << x << endl
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec.at(i) << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

int main() {
  int X, N, A;
  cin >> X >> N;
  vector<int> P(1001);
  rep(i, N) {
    cin >> A;
    P.at(A)++;
  }

  int ans = 0;
  int min_D = 10000;
  rep(i, 1000) {
    if (P.at(i) == 1)
      continue;

    int D = abs(i - X);
    if (D < min_D) {
      min_D = D;
      ans = i;
    }
  }

  cout << ans << endl;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 101) >= this->size() (which is 101)


Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
typedef long long ll;
namespace mp = boost::multiprecision;
using Mint = mp::cpp_int;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define sort_g(v) sort(all(v), greater<int>())
#define max_v(v) *max_element(all(v));
#define min_v(v) *min_element(all(v));
#define dump(x) cerr << #x << ' ' << '=' << ' ' << x << endl
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec.at(i) << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

int main() {
  int X, N, A;
  cin >> X >> N;
  vector<int> P(101);
  rep(i, N) {
    cin >> A;
    P.at(A)++;
  }

  int ans = 0;
  int min_D = 10000;
  rep(i, 1000) {
    if (P.at(i) == 1)
      continue;

    int D = abs(i - X);
    if (D < min_D) {
      min_D = D;
      ans = i;
    }
  }

  cout << ans << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
  } else {

    vector<long long> a(n), aa;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    // sort(a.begin(),a.end());
    for (int i = 1; i < 101; i++) {
      long long r = 1;
      for (int j = 0; j < n; j++) {
        if (a[j] == i)
          r = 0;
      }
      if (r)
        aa.push_back(i);
    }
    aa.push_back(0);
    aa.push_back(101);
    sort(aa.begin(), aa.end());
    long long ans = 0;
    long long aasize = aa.size() - 1;
    for (long long i = 0; i < aasize; i++) {
      if (aa[i] <= x && aa[i + 1] >= x) {
        if (x - aa[i] <= aa[i + 1] - x)
          ans = aa[i];
        else
          ans = aa[i + 1];
      }
    }

    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
  } else {

    vector<long long> a(n), aa;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    // sort(a.begin(),a.end());
    for (int i = 1; i < 101; i++) {
      long long r = 1;
      for (int j = 0; j < n; j++) {
        if (a[j] == i)
          r = 0;
      }
      if (r)
        aa.push_back(i);
    }
    sort(aa.begin(), aa.end());
    long long ans = 0;
    for (int i = 0; i < aa.size() - 1; i++) {
      if (aa[i] <= x && aa[i + 1] >= x) {
        if (x - aa[i] <= aa[i + 1] - x)
          ans = aa[i];
        else
          ans = aa[i + 1];
      }
    }
    if (ans > 0)
      cout << ans << endl;
    else if (x < 101 - x)
      cout << 0 << endl;
    else
      cout << 101 << endl;
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fr first
#define se second
#define ll long long
#define pb push_back
using namespace std;

const ll INF = 1e16 + 1;
const ll N = 2e3 + 5;
const ll mod = 1e9 + 7;
const long double eps = 1E-7;

int res;
int u[1001];
int main() {
  int x, n, k;
  cin >> x >> n;
  map<int, int> mp;
  for (int i = 1; i <= n; ++i) {
    cin >> k;
    mp[k] = 1;
  }
  int mn = 1e9, idx;
  for (int i = 0; i <= 1000; ++i) {
    if (!mp[i]) {
      if (mn > abs(x - i)) {
        idx = i;
        mn = abs(x - i);
      }
    }
  }
  cout << idx;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define fr first
#define se second
#define ll long long
#define pb push_back
using namespace std;

const ll INF = 1e16 + 1;
const ll N = 2e3 + 5;
const ll mod = 1e9 + 7;
const long double eps = 1E-7;

int res;
int u[101];
int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  int x = n - 1, y = n + 1;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    u[a[i]] = true;
    if (a[i] == x) {
      while (x--) {
        if (u[x] != true) {
          break;
        }
      }
    }
    if (a[i] == y) {
      while (y++) {
        if (u[y] != true) {
          break;
        }
      }
    }
  }
  if (u[n] != true) {
    cout << n;
  } else {
    if (abs(x - n) <= abs(y - n)) {
      cout << x;
    } else {
      cout << y;
    }
  }
}