prithivMLmods/Coder-Stat huggingface dataset:
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(void) {
  int X, N;
  cin >> X >> N;
  int check = 0;
  vector<int> list(100);

  rep(i, N) {
    cin >> check;
    list.at(check - 1) = 1;
    check = 0;
  }
  for (int j = 0; j < 500; j++) {
    if (X - j - 1 >= 0) {
      if (list.at(X - j - 1) != 1) {
        cout << X - j;
        break;
      }
    } else {
      cout << X - j;
      break;
    }

    if (X + j - 1 <= 100) {
      if (list.at(X + j - 1) != 1) {
        cout << X + j;
        break;
      }
    } else {
      cout << X + j;
      break;
    }
  }
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(void) {
  int X, N;
  cin >> X >> N;
  int check = 0;
  vector<int> list(100);

  rep(i, N) {
    cin >> check;
    list.at(check - 1) = 1;
    check = 0;
  }
  for (int j = 0; j < 500; j++) {
    if (X - j - 1 >= 0) {
      if (list.at(X - j - 1) != 1) {
        cout << X - j;
        break;
      }
    } else {
      cout << X - j;
      break;
    }

    if (X + j - 1 <= 99) {
      if (list.at(X + j - 1) != 1) {
        cout << X + j;
        break;
      }
    } else {
      cout << X + j;
      break;
    }
  }
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
#define N 100007
#define pb push_back;
vector<ll> v[N];
vector<bool> vs(N);
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

int main() {
  ll x, n, i;
  cin >> x >> n;
  vector<ll> v;
  map<ll, ll> mp;
  for (i = 0; i < n; i++) {
    ll y;
    cin >> y;
    mp[y]++;
  }
  for (i = 1; i <= 100; i++) {
    if (mp[i] == 0) {
      v.push_back(i);
    }
  }
  ll ind = lower_bound(v.begin(), v.end(), x) - v.begin();
  if (ind == 0) {
    cout << v[ind];
  } else {
    if (abs(x - v[ind]) >= abs(x - v[ind - 1])) {
      cout << v[ind - 1];
    } else {
      cout << v[ind];
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
#define N 100007
#define pb push_back;
vector<ll> v[N];
vector<bool> vs(N);
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

int main() {
  ll x, n, i;
  cin >> x >> n;
  vector<ll> v;
  map<ll, ll> mp;
  for (i = 0; i < n; i++) {
    ll y;
    cin >> y;
    mp[y]++;
  }
  for (i = 0; i <= 107; i++) {
    if (mp[i] == 0) {
      v.push_back(i);
    }
  }
  ll ind = lower_bound(v.begin(), v.end(), x) - v.begin();
  if (ind == 0) {
    cout << v[ind];
  } else {
    if (abs(x - v[ind]) >= abs(x - v[ind - 1])) {
      cout << v[ind - 1];
    } else {
      cout << v[ind];
    }
  }
}
Problem p02641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int x, n, i, j;
  cin >> x;
  cin >> n;
  int p[n];

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

  i = 0;
  while (1) {
    for (j = 0; j < n; j++) {
      if (p[j] == x + i) {
        j = 0;
        break;
      }
    }
    if (j != 0) {
      cout << x + i << endl;
      return 0;
    }
    if (i >= 0) {
      i = i * (-1) - 1;
    } else {
      i = i * (-1);
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int x, n, i, j;
  cin >> x;
  cin >> n;
  int p[n];

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

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

  i = 0;
  while (1) {
    for (j = 0; j < n; j++) {
      if (p[j] == x + i) {
        j = 0;
        break;
      }
    }
    if (j != 0) {
      cout << x + i << endl;
      return 0;
    }
    if (i >= 0) {
      i = i * (-1) - 1;
    } else {
      i = i * (-1);
    }
  }

  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll MOD = 1000000007;

int main(void) {
  int x, n;
  cin >> x >> n;
  int p[n];
  int dif = 0;
  rep(i, n) cin >> p[i];
  while (1) {
    rep(i, n) {
      if (x - dif == p[i])
        break;
      else if (i == n - 1) {
        cout << x - dif << endl;
        return 0;
      }
    }
    rep(i, n) {
      if (x + dif == p[i])
        break;
      else if (i == n - 1) {
        cout << x + dif << endl;
        return 0;
      }
    }
    dif++;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll MOD = 1000000007;

int main(void) {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  int p[n];
  int dif = 0;
  rep(i, n) cin >> p[i];
  while (1) {
    rep(i, n) {
      if (x - dif == p[i])
        break;
      else if (i == n - 1) {
        cout << x - dif << endl;
        return 0;
      }
    }
    rep(i, n) {
      if (x + dif == p[i])
        break;
      else if (i == n - 1) {
        cout << x + dif << endl;
        return 0;
      }
    }
    dif++;
  }
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int x, n, ans = 100, tmp = 100;
  cin >> x >> n;
  vector<int> p(n), test(100);
  for (int i = 0; i <= 100; i++)
    test.at(i) = i;
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
    test.erase(remove(test.begin(), test.end(), p.at(i)), test.end());
  }
  for (int i : test) {
    if (abs(x - i) < tmp) {
      ans = i;
      tmp = abs(x - i);
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int x, n, ans = 100, tmp = 100;
  cin >> x >> n;
  vector<int> p(n), test(102);
  for (int i = 0; i <= 101; i++)
    test.at(i) = i;
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
    test.erase(remove(test.begin(), test.end(), p.at(i)), test.end());
  }
  for (int i : test) {
    if (abs(x - i) < tmp) {
      ans = i;
      tmp = abs(x - i);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<int> p(100);
  for (int i = 0; i < n; i++) {
    int t = 0;
    cin >> t;
    p.at(t) = 1;
  }

  for (int i = 0; i < 100; i++) {
    if (p[x - i] != 1) {
      cout << x - i << endl;
      return 0;
    }
    if (p[x + i] != 1) {
      cout << x + i << endl;
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<int> p(100);
  for (int i = 0; i < n; i++) {
    int t = 0;
    cin >> t;
    p[t]++;
  }

  for (int i = 0; i < 100; i++) {
    if (p[x - i] != 1) {
      cout << x - i << endl;
      return 0;
    }
    if (p[x + i] != 1) {
      cout << x + i << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int x, n;
  std::cin >> x >> n;

  std::vector<int> p(n);
  for (int i = 0; i < n; ++i)
    std::cin >> p[i];

  int minD = 1000;
  int result = 0;
  for (int j = 200; j > -100; ++j) {
    if (std::any_of(p.begin(), p.end(), [j](int v) { return j == v; }))
      continue;

    if (minD >= std::abs(j - x)) {
      minD = std::abs(j - x);
      result = j;
    }
  }

  std::cout << result << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int x, n;
  std::cin >> x >> n;

  std::vector<int> p(n);
  for (int i = 0; i < n; ++i)
    std::cin >> p[i];

  int minD = 1000;
  int result = 0;
  for (int j = 200; j > -100; --j) {
    if (std::any_of(p.begin(), p.end(), [j](int v) { return j == v; }))
      continue;

    if (minD >= std::abs(j - x)) {
      minD = std::abs(j - x);
      result = j;
    }
  }

  std::cout << result << std::endl;

  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
/* start */

int was[110];

signed main() {
  IOS;
  int x, n;
  cin >> x >> n;
  for (int i = 0; i < n; i++) {
    int it;
    cin >> it;
    was[it] = 1;
  }
  vector<pair<int, int>> ans;
  for (int i = 1; i <= 100; i++) {
    if (!was[i])
      ans.push_back({abs(i - x), i});
  }
  sort(ans.begin(), ans.end());
  cout << ans[0].second << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
/* start */

int was[110];

signed main() {
  IOS;
  int x, n;
  cin >> x >> n;
  for (int i = 0; i < n; i++) {
    int it;
    cin >> it;
    was[it] = 1;
  }
  vector<pair<int, int>> ans;
  for (int i = 0; i < 10000; i++) {
    if (i < 110) {
      if (!was[i])
        ans.push_back({abs(i - x), i});
    } else
      ans.push_back({abs(i - x), i});
  }
  sort(ans.begin(), ans.end());
  cout << ans[0].second << "\n";
}
Problem p02641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int X, N;
  std::cin >> X >> N;
  std::vector<int> P(N);
  int Distance = 0;
  int ans;
  for (auto &p : P) {
    std::cin >> p;
  }
  while (1) {
    auto itr1 = std::find(P.begin(), P.end(), X - Distance);
    auto itr2 = std::find(P.begin(), P.end(), X + Distance);
    if (itr1 == P.end()) {
      ans = X - Distance;
      break;
    } else if (itr2 == P.end()) {
      ans = X + Distance;
      break;
    }
    Distance;
  }
  std::cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int X, N;
  std::cin >> X >> N;
  std::vector<int> P(N);
  int Distance = 0;
  int ans;
  for (auto &p : P) {
    std::cin >> p;
  }
  while (1) {
    auto itr1 = std::find(P.begin(), P.end(), X - Distance);
    auto itr2 = std::find(P.begin(), P.end(), X + Distance);
    if (itr1 == P.end()) {
      ans = X - Distance;
      break;
    } else if (itr2 == P.end()) {
      ans = X + Distance;
      break;
    }
    Distance++;
  }
  std::cout << ans;
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
using ll = long long;

int main() {
  int i, x, n, ans = 0, dist = 999, j = 0;
  cin >> x >> n;
  vector<int> p(n);
  for (i = 0; i < n; i++)
    cin >> p[i];
  sort(p.begin(), p.end());
  for (i = 1; i < x; i++) {
    if (i != p[j]) {
      ans = i;
    } else {
      j++;
    }
  }
  for (i = x; i < x + (x - ans); i++) {
    if (i != p[j]) {
      ans = i;
      break;
    } else {
      j++;
    }
  }

  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
using ll = long long;

int main() {
  int i, x, n, ans = 0, dist = 999, j = 0;
  cin >> x >> n;
  if (n == 0) {
    cout << x;
    return 0;
  }
  vector<int> p(n);
  for (i = 0; i < n; i++)
    cin >> p[i];
  sort(p.begin(), p.end());
  for (i = 1; i < x; i++) {
    if (i != p[j]) {
      ans = i;
    } else {
      j++;
    }
  }
  for (i = x; i < x + (x - ans); i++) {
    if (i != p[j]) {
      ans = i;
      break;
    } else {
      j++;
    }
  }

  cout << ans;
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007
#define MAX 1000000

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> in(101, 0);
  int p;
  for (int i = 0; i < n; i++) {
    cin >> p;
    in.at(p)++;
  }

  int dis = 99999;
  int ans;
  for (int i = 0; i <= 101; i++) {
    if (in.at(i) == 1)
      continue;
    if (dis > abs(x - i)) {
      dis = abs(x - i);
      ans = i;
    }
  }

  cout << ans << endl;
}


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

#define ll long long
#define MOD 1000000007
#define MAX 1000000

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> in(102, 0);
  int p;
  for (int i = 0; i < n; i++) {
    cin >> p;
    in.at(p)++;
  }

  int dis = 99999;
  int ans;
  for (int i = 0; i <= 101; i++) {
    if (in.at(i) == 1)
      continue;
    if (dis > abs(x - i)) {
      dis = abs(x - i);
      ans = i;
    }
  }

  cout << ans << endl;
}
Problem p02641 (C++)

Buggy submission (status: 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;
  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;
}

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;
  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;
}
Problem p02641 (C++)

Buggy submission (status: 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(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;
}


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(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;
}
Problem p02641 (C++)

Buggy submission (status: 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(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;
}


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(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;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int X, N;
  vector<int> p;
  vector<pair<int, int>> q;
  cin >> X >> N;
  int y;
  rep(i, N) {
    cin >> y;
    p.push_back(y);
  }
  for (int i = 1; i <= 100; i++) {
    auto result = find(p.begin(), p.end(), i);
    if (result == p.end()) {
      q.push_back(make_pair((X - i) * (X - i), i));
    }
  }
  sort(q.begin(), q.end());
  cout << q[0].second << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int X, N;
  vector<int> p;
  vector<pair<int, int>> q;
  cin >> X >> N;
  int y;
  rep(i, N) {
    cin >> y;
    p.push_back(y);
  }
  for (int i = 0; i <= 101; i++) {
    auto result = find(p.begin(), p.end(), i);
    if (result == p.end()) {
      q.push_back(make_pair((X - i) * (X - i), i));
    }
  }
  sort(q.begin(), q.end());
  cout << q[0].second << endl;
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repc(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//------------------------------------------------------//

int main() {
  int x, n;
  cin >> x >> n;
  vec<bool> p(101, false);
  rep(i, n) {
    int y;
    cin >> y;
    p[y] = true;
  }
  int l = x, r = x;
  int ans = 0;
  while (ans == 0) {
    if (p[l] == false && p[r] == false)
      ans = l;
    else if (p[l] == false && p[r] == true)
      ans = l;
    else if (p[l] == true && p[r] == false)
      ans = r;
    l--;
    r++;
  }
  cout << ans << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repc(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//------------------------------------------------------//

int main() {
  int x, n;
  cin >> x >> n;
  vec<bool> p(101, false);
  rep(i, n) {
    int y;
    cin >> y;
    p[y] = true;
  }
  int l = x, r = x;
  int ans = 0;
  while (ans == 0) {
    if (l == 0) {
      ans = 0;
      break;
    }
    if (p[l] == false && p[r] == false)
      ans = l;
    else if (p[l] == false && p[r] == true)
      ans = l;
    else if (p[l] == true && p[r] == false)
      ans = r;
    l--;
    r++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>

using namespace std;

int main() {
  int X, N;

  cin >> X >> N;

  int *p;
  p = new int[N];

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

  for (int i = 0; i < N; i++) {
    if (p[i] == X) {
      found = true;
      i = N;
    } else {
      found = false;
      output = X;
    }
  }
  int j = 1;
  while (found) {
    for (int i = 0; i < N; i++) {
      if (p[i] == X - j) {
        found = true;
        i = N;
      } else {
        found = false;
        output = X - j;
      }
    }

    if (found) {
      for (int i = 0; i < N; i++) {
        if (p[i] == X + j) {
          found = true;
          i = N;
        } else {
          found = false;
          output = X + j;
        }
      }
    }
    j++;
  }
  cout << output;

  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>

using namespace std;

int main() {
  int X, N;

  cin >> X >> N;

  int *p;
  p = new int[N];

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

  for (int i = 0; i < N; i++) {
    if (p[i] == X) {
      found = true;
      i = N;
    } else {
      found = false;
      output = X;
    }
  }

  if (N == 0) {
    found = false;
    output = X;
  }

  int j = 1;
  while (found) {
    for (int i = 0; i < N; i++) {
      if (p[i] == X - j) {
        found = true;
        i = N;
      } else {
        found = false;
        output = X - j;
      }
    }

    if (found) {
      for (int i = 0; i < N; i++) {
        if (p[i] == X + j) {
          found = true;
          i = N;
        } else {
          found = false;
          output = X + j;
        }
      }
    }
    j++;
  }
  cout << output;

  return 0;
}
Problem p02641 (Python)

Buggy submission (status: Runtime Error):
X, N = map(int, input().split())
p = list(map(int, input().split()))

if p:
    x = [i for i in range(1, max(p)) if i not in p]

    c = x.pop()
    a = abs(c - X)
    for i in x:
        if abs(i - X) < a:
            c = i
    print(c)
else:
    print(X)


Fixed submission:
X, N = map(int, input().split())
p = list(map(int, input().split()))

i = 0
while True:
    if X - i not in p:
        print(X - i)
        break
    elif X + i not in p:
        print(X + i)
        break
    i += 1
Problem p02641 (Python)

Buggy submission (status: Runtime Error):
def main():
    x, n = map(int, input().split())
    p = list(map(int, input().split()))

    min = 1000

    for i in range(1, 101):
        if i in p:
            continue

        elif min > abs(x - i):
            result = i
            min = abs(x - i)

    print(result)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    x, n = map(int, input().split())
    p = list(map(int, input().split()))

    min = 1000

    for i in range(-50, 150):
        if i in p:
            continue

        elif min > abs(x - i):
            result = i
            min = abs(x - i)

    print(result)


if __name__ == "__main__":
    main()
Problem p02641 (Python)

Buggy submission (status: Runtime Error):
x, n = map(int, input().split())
if n != 0:
    p = list(map(int, input().split()))
    if x in p:
        for i in range(-1000, 1001):
            print(i)
            if i == -100:
                mini = i
            if i in p:
                pass
            else:
                if abs(x - mini) > abs(x - i):
                    mini = i
                elif abs(x - mini) == abs(x - i):
                    mini = min(mini, i)
        else:
            print(mini)
    else:
        print(x)
else:
    print(x)


Fixed submission:
x, n = map(int, input().split())
if n != 0:
    p = list(map(int, input().split()))
    if x in p:
        for i in range(-1000, 1001):
            if i == -1000:
                mini = i
            if i in p:
                pass
            else:
                if abs(x - mini) > abs(x - i):
                    mini = i
                elif abs(x - mini) == abs(x - i):
                    mini = min(mini, i)
        else:
            print(mini)
    else:
        print(x)
else:
    print(x)
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <set>
#include <utility>
#include <vector>
using namespace std;

void forbidden_list(vector<int> v, int x);
int main() {
  int x, n;
  vector<int> v;
  cin >> x >> n;
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    v.push_back(l);
  }
  forbidden_list(v, x);

  return 0;
}

void forbidden_list(vector<int> v, int x) {
  auto it = find(v.begin(), v.end(), x);
  if (it == v.end())
    cout << x << endl;

  else {
    int min = 100;
    vector<pair<int, int>> p;
    for (int i = 1; i < 100; i++) {

      auto it = find(v.begin(), v.end(), i);
      if (it == v.end()) {
        int ab = abs(x - i);

        if (ab < min) {
          min = ab;
          p.push_back(make_pair(min, i));
        }
      } else
        v.erase(it);
    }

    sort(p.begin(), p.end());
    auto it = p.begin();
    cout << it->second << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <set>
#include <utility>
#include <vector>
using namespace std;

void forbidden_list(vector<int> v, int x);
int main() {
  int x, n;
  vector<int> v;
  cin >> x >> n;
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    v.push_back(l);
  }
  forbidden_list(v, x);

  return 0;
}

void forbidden_list(vector<int> v, int x) {
  auto it = find(v.begin(), v.end(), x);
  if (it == v.end())
    cout << x << endl;

  else {
    int min = 100;
    vector<pair<int, int>> p;
    for (int i = 0; i <= 101; i++) {

      auto it = find(v.begin(), v.end(), i);
      if (it == v.end()) {
        int ab = abs(x - i);

        if (ab < min) {
          min = ab;
          p.push_back(make_pair(min, i));
        }
      } else
        v.erase(it);
    }

    sort(p.begin(), p.end());
    auto it = p.begin();
    cout << it->second << endl;
  }
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int X, N;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  int tmp = 0;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> tmp;
    P.at(i) = tmp;
  }
  sort(P.begin(), P.end());

  vector<int> P_non;
  bool flag = true;
  for (int i = 1; i <= 100; i++) {
    for (int j = 0; j < N; j++) {
      if (i == P.at(j))
        flag = false;
    }
    if (flag)
      P_non.push_back(i);
    flag = true;
  }

  int diff = 1000;
  int num = 1000;
  for (unsigned int i = 0; i < P_non.size(); i++) {
    if (abs(P_non.at(i) - X) < diff) {
      diff = abs(P_non.at(i) - X);
      num = i;
    }
  }
  cout << P_non.at(num) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int X, N;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  int tmp = 0;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> tmp;
    P.at(i) = tmp;
  }
  sort(P.begin(), P.end());

  vector<int> P_non;
  bool flag = true;
  for (int i = 1; i <= 100; i++) {
    for (int j = 0; j < N; j++) {
      if (i == P.at(j))
        flag = false;
    }
    if (flag)
      P_non.push_back(i);
    flag = true;
  }
  P_non.push_back(0);
  P_non.push_back(101);
  sort(P_non.begin(), P_non.end());

  int diff = 1000;
  int num = 1000;
  for (unsigned int i = 0; i < P_non.size(); i++) {
    if (abs(P_non.at(i) - X) < diff) {
      diff = abs(P_non.at(i) - X);
      num = i;
    }
  }
  cout << P_non.at(num) << endl;
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int main() {
  int x, n;
  int p;
  bool q[101];
  int i;

  cin >> x >> n;
  if (n == 0) {
    printf("%d\n", x);
    return 0;
  }
  for (i = 1; i <= 100; i++)
    q[i] = true;
  for (i = 0; i < n; i++) {
    cin >> p;
    q[p] = false;
  }
  // for (i=1; i<=100; i++) printf("%d ", q[i]);
  // printf("\n");
  for (i = x; i >= 1; i--) {
    if (q[i]) {
      break;
    }
    // printf("x*2-i = %d q[]=%d\n", x*2-i, q[x*2-i]);
    int i2 = x * 2 - i;
    if (i2 <= 100) {
      if (q[i2]) {
        i = i2;
        break;
      }
    } else {
      i = i2;
    }
  }

  printf("%d\n", i);
  return 0;
}


Fixed submission:
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int main() {
  int x, n;
  int p;
  bool q[101];
  int i;

  cin >> x >> n;
  if (n == 0) {
    printf("%d\n", x);
    return 0;
  }
  for (i = 1; i <= 100; i++)
    q[i] = true;
  for (i = 0; i < n; i++) {
    cin >> p;
    q[p] = false;
  }
  // for (i=1; i<=100; i++) printf("%d ", q[i]);
  // printf("\n");
  for (i = x; i >= 1; i--) {
    if (q[i]) {
      break;
    }
    // printf("x*2-i = %d q[]=%d\n", x*2-i, q[x*2-i]);
    int i2 = x * 2 - i;
    if (i2 <= 100) {
      if (q[i2]) {
        i = i2;
        break;
      }
    } else {
      i = i2;
      break;
    }
  }

  printf("%d\n", i);
  return 0;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
  int x, n;
  cin >> x >> n;
  vector<bool> h(102);
  rep(i, n) {
    int p;
    cin >> p;
    h[p] = true;
  }
  pair<int, int> ans(100000, -1);
  for (int i = 0; i <= 101; i++) {
    if (h[i])
      continue;
    int dif = abs(x - i);
    ans = min(ans, pair<int, int>(dif, i));
  }
  cout << ans.second << endl;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <stack>
#include <string>

#include <algorithm>
#include <fstream>
#include <vector>

#define ll long long int

using namespace std;

int main(void) {

  int x, n, tmp;
  int res, res_u;
  vector<int> p;

  cin >> x >> n;
  res = x;
  res_u = x;

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    p.push_back(tmp);
  }

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

  for (int j = 0; j < n; j++) {
    for (int i = 0; i < n; i++) {
      if (p[i] == res) {
        res--;
      }
      if (p[n - i] == res_u) {
        res_u++;
      }
    }
  }

  if (p[0] == res_u) {
    res_u++;
  }

  if (abs(res - x) <= abs(res_u - x)) {
    cout << res;
  } else {
    cout << res_u;
  }
}

Fixed submission:
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <stack>
#include <string>

#include <algorithm>
#include <fstream>
#include <vector>

#define ll long long int

using namespace std;

int main(void) {

  int x, n, tmp;
  int res, res_u;
  vector<int> p;

  cin >> x >> n;
  res = x;
  res_u = x;

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    p.push_back(tmp);
  }

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

  for (int j = 0; j < n; j++) {
    for (int i = 0; i < n; i++) {
      if (p[i] == res) {
        res--;
      }
      if (p[n - i] == res_u) {
        res_u++;
      }
    }
  }

  if (n) {
    if (p[0] == res_u) {
      res_u++;
    }
  }

  if (abs(res - x) <= abs(res_u - x)) {
    cout << res;
  } else {
    cout << res_u;
  }
}
Problem p02641 (Python)

Buggy submission (status: Runtime Error):
X, N = map(int, input().split())
P = set(list(map(int, input().split())))

targets = set(range(1, 101)) - P
print(min([(target, abs(X - target)) for target in targets], key=lambda x: x[1])[0])


Fixed submission:
X, N = map(int, input().split())
P = set(list(map(int, input().split())))

targets = set(range(-101, 102)) - P
if targets:
    print(min([(target, abs(X - target)) for target in targets], key=lambda x: x[1])[0])
else:
    print(X)
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int main() {
  int X, Y, min, ans, j;
  cin >> X >> Y;
  vector<int> box(100, 10000);
  rep(i, Y) { cin >> box.at(i); }
  sort(box.begin(), box.begin() + Y);
  min = 10000;
  j = 0;
  for (int i = -1 * X; i <= 2 * X; i++) {
    if (box.at(j) == i) {
      j++;
      continue;
    }

    if (min > abs(X - i)) {
      min = abs(X - i);
      ans = i;
    }
  }

  cout << ans << endl;
}

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

int main() {
  int X, Y, min, ans, j;
  cin >> X >> Y;
  vector<int> box(1000, 10000);
  rep(i, Y) { cin >> box.at(i); }
  sort(box.begin(), box.begin() + Y);
  min = 10000;
  j = 0;
  for (int i = -1 * X; i <= 2 * X; i++) {
    if (box.at(j) == i) {
      j++;
      continue;
    }

    if (min > abs(X - i)) {
      min = abs(X - i);
      ans = i;
    }
  }

  cout << ans << endl;
}
Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
// Loop
#define REP(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define RREP(i, a, b) for (ll(i) = (a); (i) >= (b); (i)--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)

#define ALL(v) v.begin(), v.end()

const int INF = 10000;

int main(void) {
  int x, n;
  int diff = INF;
  int ans = 100;
  cin >> x >> n;

  int itemp;
  bool q[101];
  rep(i, 101) { q[i] = false; }
  rep(i, n) {
    cin >> itemp;
    q[itemp] = true;
  }
  REP(i, 0, 110) {
    if (q[i]) {
      continue;
    }
    int dtemp = abs(x - i);
    if (diff == dtemp) {
      cout << ans << endl;
      return 0;
    }
    if (diff > dtemp) {
      diff = dtemp;
      ans = i;
    }
  }
  cout << ans << endl;
}

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

using ll = long long;
using vi = vector<int>;
// Loop
#define REP(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define RREP(i, a, b) for (ll(i) = (a); (i) >= (b); (i)--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)

#define ALL(v) v.begin(), v.end()

const int INF = 10000;

int main(void) {
  int x, n;
  int diff = INF;
  int ans = 100;
  cin >> x >> n;

  int itemp;
  bool q[110];
  rep(i, 110) { q[i] = false; }
  rep(i, n) {
    cin >> itemp;
    q[itemp] = true;
  }
  REP(i, 0, 110) {
    if (q[i]) {
      continue;
    }
    int dtemp = abs(x - i);
    if (diff == dtemp) {
      cout << ans << endl;
      return 0;
    }
    if (diff > dtemp) {
      diff = dtemp;
      ans = i;
    }
  }
  cout << ans << endl;
}
Problem p02641 (Python)

Buggy submission (status: Runtime Error):
X, N = map(int, input().split())
A = list(map(int, input().split()))

tmp = 1000
result = []
result2 = []
for i in range(1, 101):
    if i in A:
        continue

    hoge = abs(X - i)
    if hoge <= tmp:
        tmp = hoge
        result2.append(tmp)
        result.append(i)

result3 = []
for i, v in enumerate(result2):
    if v == min(result2):
        result3.append(result[i])

print(min(result3))


Fixed submission:
X, N = map(int, input().split())
A = list(map(int, input().split()))

tmp = 1000
result = []
result2 = []
for i in range(0, 102):
    if i in A:
        continue

    hoge = abs(X - i)
    if hoge <= tmp:
        tmp = hoge
        result2.append(tmp)
        result.append(i)

result3 = []
for i, v in enumerate(result2):
    if v == min(result2):
        result3.append(result[i])

print(min(result3))
Problem p02641 (Python)

Buggy submission (status: Runtime Error):
X, N = map(int, input().split())

if N == 0:
    print(X)

else:
    p = list(map(int, input().split()))
    q = [i for i in range(-1, 102)]

    for r in p:
        idx = q.index(r)
        del q[idx]

    if X in q:
        print(X)

    else:
        q.sort()
        mindif = 100

        for num in q:
            dif = abs(X - num)
            if mindif <= dif:
                break
            else:
                mindif = dif
                ans = num
        print(ans)


Fixed submission:
X, N = map(int, input().split())

if N == 0:
    print(X)

else:
    p = list(map(int, input().split()))
    q = [i for i in range(-1, 102)]

    for r in p:
        idx = q.index(r)
        del q[idx]

    if X in q:
        print(X)

    else:
        q.sort()
        mindif = 1000

        for num in q:
            dif = abs(X - num)
            if mindif <= dif:
                break
            else:
                mindif = dif
                ans = num
        print(ans)
Problem p02641 (Python)

Buggy submission (status: Runtime Error):
x, n = map(int, input().split())
p = [int(_) for _ in input().split()]

not_in_p = [i for i in range(0, 102) if i not in p]
nip_temp = list(map(lambda s: abs(x - s), not_in_p))

address = nip_temp.index(min(nip_temp))
print(p[address])


Fixed submission:
x, n = map(int, input().split())
p = [int(_) for _ in input().split()]

not_in_p = [i for i in range(0, 102) if i not in p]
nip_temp = list(map(lambda s: abs(x - s), not_in_p))

address = nip_temp.index(min(nip_temp))
print(not_in_p[address])
Problem p02641 (Python)

Buggy submission (status: Runtime Error):
import numpy as np

X, N = map(int, input().split())
P = np.array(list(map(int, input().split())))

P = P - X

for i in range(0, X):
    if i not in P or -i not in P:
        if -i not in P:
            ans = X - i
        else:
            ans = X + i
        break

print(ans)


Fixed submission:
import numpy as np

X, N = map(int, input().split())
P = np.array(list(map(int, input().split())))

P = P - X

for i in range(0, X + 1):
    if i not in P or -i not in P:
        if -i not in P:
            ans = X - i
        else:
            ans = X + i
        break

print(ans)
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <iostream>
using namespace std;
#define ll long long
const ll N = 2e5 + 507, N1 = 1e6 + 107;
;
ll n, i, maxx, nr, j, a[N], f[N];
bitset<N1> g;
int main() {
  cin >> n;
  for (i = 1; i <= n; ++i)
    cin >> a[i], f[a[i]]++;
  sort(a + 1, a + n + 1);
  maxx = a[n];
  for (i = 1; i <= n; ++i) {
    for (j = 2; j <= maxx / a[i]; ++j)
      g[a[i] * j] = 1;
  }
  for (i = 1; i <= n; ++i)
    if (f[a[i]] == 1 && !g[a[i]])
      nr++;
  cout << nr << "\n";
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <iostream>
using namespace std;
#define ll long long
const ll N = 2e5 + 507, N1 = 1e6 + 107;
ll n, i, maxx, nr, j, a[N], f[N1];
bitset<N1> g;
int main() {
  cin >> n;
  for (i = 1; i <= n; ++i)
    cin >> a[i], f[a[i]]++;
  sort(a + 1, a + n + 1);
  maxx = a[n];
  for (i = 1; i <= n; ++i) {
    for (j = 2; j <= maxx / a[i]; ++j)
      g[a[i] * j] = 1;
  }
  for (i = 1; i <= n; ++i)
    if (f[a[i]] == 1 && !g[a[i]])
      nr++;
  cout << nr << "\n";
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

#include <iomanip>
#include <math.h>
#include <queue>
#include <vector>
using Graph = vector<vector<int>>;
const double PI = 3.14159265358979323846;

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// 約数をベクトル形式で返す
vector<int> vect_yakusuu(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return ret;
}

int main() {

  int N;
  int ans = 0;

  int A[100010];
  int num[100010] = {};

  vector<int> yakusuu;

  cin >> N;

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

  for (int i = 0; i < N; i++) {
    yakusuu = vect_yakusuu(A[i]);
    int is_undiv = 1;
    for (int j = 0; j < yakusuu.size() - 1; j++) {
      if (num[yakusuu[j]] != 0) {
        is_undiv = 0;
      }
    }
    int j = yakusuu.size() - 1;
    if (num[yakusuu[j]] > 1) {
      is_undiv = 0;
    }
    if (is_undiv == 1) {
      ans++;
    }
  }

  cout << ans << endl;

  cin >> ans;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

#include <iomanip>
#include <math.h>
#include <queue>
#include <vector>
using Graph = vector<vector<int>>;
const double PI = 3.14159265358979323846;

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// 約数をベクトル形式で返す
vector<int> vect_yakusuu(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return ret;
}

int main() {

  int N;
  int ans = 0;

  int A[1000010];
  int num[1000010] = {};

  vector<int> yakusuu;

  cin >> N;

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

  for (int i = 0; i < N; i++) {
    yakusuu = vect_yakusuu(A[i]);
    int is_undiv = 1;
    for (int j = 0; j < yakusuu.size() - 1; j++) {
      if (num[yakusuu[j]] != 0) {
        is_undiv = 0;
      }
    }
    int j = yakusuu.size() - 1;
    if (num[yakusuu[j]] > 1) {
      is_undiv = 0;
    }
    if (is_undiv == 1) {
      ans++;
    }
  }

  cout << ans << endl;

  cin >> ans;

  return 0;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef int ll;

vector<ll> input;
vector<ll> divi[1000005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll i, j, k, l, m, n, t;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> k;
    input.push_back(k);
  }

  for (i = 1; i <= 1000005; i++) {
    for (j = i; j <= 1000005; j += i) {
      divi[j].push_back(i);
    }
  }

  // for(i=1;i<=10;i++)
  // 	for(j=0;j<divi[i].size();j++)
  // 		cout << divi[i][j] << " ";
  // 	cout << '\n';

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

  ll flag = 0;
  ll ans = 0;

  for (i = 0; i < n; i++) {
    flag = 0;
    // cout << input[i] << "\n";
    for (j = 0; j < divi[input[i]].size(); j++) {

      m = lower_bound(input.begin(), input.end(), divi[input[i]][j]) -
          input.begin();
      l = upper_bound(input.begin(), input.end(), divi[input[i]][j]) -
          input.begin();

      // cout << l << " " << m << '\n';

      if (input[i] != divi[input[i]][j]) {
        if (l != m) {
          flag = 1;
          break;
        }
      }

      if (input[i] == divi[input[i]][j]) {
        if ((l - m) > 1) {
          flag = 1;
          break;
        }
      }

      // if(binary_search(input.begin(),input.end(),divi[input[i]][j]) == true)
      // {
      // 	flag = 1
      // }
    }

    // cout << "flag: " << flag << '\n';

    if (flag == 0)
      ans++;
  }

  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef int ll;

vector<ll> input;
vector<ll> divi[1000005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll i, j, k, l, m, n, t;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> k;
    input.push_back(k);
  }

  for (i = 1; i < 1000001; i++) {
    for (j = i; j < 1000001; j += i) {
      divi[j].push_back(i);
    }
  }

  // for(i=1;i<=10;i++)
  // 	for(j=0;j<divi[i].size();j++)
  // 		cout << divi[i][j] << " ";
  // 	cout << '\n';

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

  ll flag = 0;
  ll ans = 0;

  for (i = 0; i < n; i++) {
    flag = 0;
    // cout << input[i] << "\n";
    for (j = 0; j < divi[input[i]].size(); j++) {

      m = lower_bound(input.begin(), input.end(), divi[input[i]][j]) -
          input.begin();
      l = upper_bound(input.begin(), input.end(), divi[input[i]][j]) -
          input.begin();

      // cout << l << " " << m << '\n';

      if (input[i] != divi[input[i]][j]) {
        if (l != m) {
          flag = 1;
          break;
        }
      }

      if (input[i] == divi[input[i]][j]) {
        if ((l - m) > 1) {
          flag = 1;
          break;
        }
      }

      // if(binary_search(input.begin(),input.end(),divi[input[i]][j]) == true)
      // {
      // 	flag = 1
      // }
    }

    // cout << "flag: " << flag << '\n';

    if (flag == 0)
      ans++;
  }

  cout << ans << '\n';
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
bool compare(int a, int b) { return a > b; }
int main() {
  int x, n, i, ans, j, temp, maxi, k;
  int flag[200100] = {0};
  int mark[100100] = {0};
  int flag2[100100] = {0};

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> flag[i];
    flag2[flag[i]]++;
  }
  sort(flag, flag + n, compare);
  maxi = flag[0];
  ans = 0;
  for (i = 0; i < n; i++) {
    k = 2;
    while (flag[i] * k <= maxi) {
      mark[flag[i] * k] = 1;
      k++;
    }
  }

  for (i = 0; i < n; i++) {
    if (flag2[flag[i]] == 1) {
      if (mark[flag[i]] != 1)
        ans++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
bool compare(int a, int b) { return a > b; }
int main() {
  int x, n, i, ans, j, temp, maxi, k;
  int flag[200100] = {0};
  int mark[1001000] = {0};
  int flag2[1001000] = {0};

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> flag[i];
    flag2[flag[i]]++;
  }
  sort(flag, flag + n, compare);
  maxi = flag[0];
  ans = 0;
  for (i = 0; i < n; i++) {
    k = 2;
    while (flag[i] * k <= maxi) {
      mark[flag[i] * k] = 1;
      k++;
    }
  }

  for (i = 0; i < n; i++) {
    if (flag2[flag[i]] == 1) {
      if (mark[flag[i]] != 1)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02642 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const int M = 1000000;

int main() {
  int n;
  cin >> n;

  vector<int> A(n);
  rep(i, n) cin >> A[i];

  vector<int> C(M);

  for (auto t : A) {
    if (C[t] != 0) {
      C[t] = 2;
      continue;
    }
    for (int j = t; j < M; j += t) {
      C[j]++;
    }
  }

  int cnt = 0;
  for (auto t : A) {
    if (C[t] == 1)
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const int M = 1000010;

int main() {
  int n;
  cin >> n;

  vector<int> A(n);
  rep(i, n) cin >> A[i];

  vector<int> C(M);

  for (auto t : A) {
    if (C[t] != 0) {
      C[t] = 2;
      continue;
    }
    for (int j = t; j < M; j += t) {
      C[j]++;
    }
  }

  int cnt = 0;
  for (auto t : A) {
    if (C[t] == 1)
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}