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

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x;
    return 0;
  }
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  int ans = INT_MAX;
  int i = x - 1, j = x + 1;
  if (find(a.begin(), a.end(), x) == a.end()) {
    ans = x;
    cout << ans;
    return 0;
  }
  while (1) {
    if (abs(x - i) < abs(x - ans)) {
      if (find(a.begin(), a.end(), i) == a.end()) {
        ans = i;
        break;
      }
    }
    if (abs(x - j) < abs(x - ans)) {
      if (find(a.begin(), a.end(), j) == a.end()) {
        ans = j;
        break;
      }
    }
    i--;
    j++;
  }
  cout << ans;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int x, n;
  cin >> x >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  int ans = abs(a[0] - x);
  for (int i = a[0] - (a[n - 1] - a[0]) - 1; i <= 2 * a[n - 1]; i++) {
    if (abs(x - i) && abs(x - i) < abs(ans - x)) {
      if (find(a.begin(), a.end(), i) == a.end()) {
        ans = i;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

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

  int a, n;
  cin >> a >> n;

  vector<int> v(101);
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    v[t] = 1;
  }

  int l = a;
  int r = a;
  while (true) {
    if (!v[l]) {
      cout << l;
      return 0;
    }
    if (!v[r]) {
      cout << r;
      return 0;
    }
    l--;
    r++;
  }

  return 0;
}


Error: -11

Fixed submission:

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

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

  int a, n;
  cin >> a >> n;

  vector<int> v(n + 1);

  int ans = 0;
  int m = INT_MIN;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    v[t] = 1;
    m = max(m, t);
  }

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

  for (int i = a; i <= m; i++) {
    if (v[i] == 0) {
      ans = i;
      break;
    }
  }

  for (int i = a - 1; i >= INT_MIN + 100; i--) {
    if (v[i] == 0) {
      if (abs(ans - a) > abs(a - i)) {
        ans = i;
        break;
      }
    }
  }

  cout << ans;

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

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.second < b.second);
}
int main() {

  ll t, i, j, k, n, l, x, y;
  cin >> x >> n;
  map<ll, ll> m;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<pair<ll, ll>> v;
  for (i = 0; i < n; i++) {
    ll p;
    cin >> p;
    m[p]++;
  }
  for (i = -1000; i <= 1000; i++) {
    if (m[i] == 0) {
      v.push_back({i, abs(i - x)});
    }
  }
  sort(v.begin(), v.end(), sortbysec);
  cout << v[0].first << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.second < b.second);
}
int main() {

  ll t, i, j, k, n, l, x, y;
  cin >> x >> n;
  map<ll, ll> m;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<pair<ll, ll>> v;
  for (i = 0; i < n; i++) {
    ll p;
    cin >> p;
    m[p]++;
  }
  for (i = 1; i <= 100; i++) {
    if (m[i] == 0) {
      v.push_back({i, abs(i - x)});
    }
  }
  sort(v.begin(), v.end(), sortbysec);
  cout << v[0].first << endl;

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

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

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> vec(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  vec.at(N) = 130;
  for (int u = 0; u < N; u++) {
    for (int v = u + 1; v < N; v++) {
      if (vec.at(u) == vec.at(v)) {
        vec.at(v) = 120;
      }
    }
  }
  sort(vec.begin(), vec.end());
  int p, q = 0;
  for (int j = 0; j < N; j++) {
    if (X != vec.at(j)) {
      p++;
    } else {
      q = j;
      break;
    }
  }
  if (p == N) {
    cout << X << endl;
  } else {
    for (int k = 0; k < N; k++) {
      if (q - k - 1 >= 0 && q + k + 1 < N + 1) {
        if (vec.at(q - k - 1) != X - k - 1) {
          cout << X - k - 1 << endl;
          break;
        } else if (vec.at(q + k + 1) != X + k + 1) {
          cout << X + k + 1 << endl;
          break;
        } else if (q - k - 1 == 0 && vec.at(q + k + 1) != 120) {
          cout << X - k - 2 << endl;
          break;
        } else if (vec.at(q + k + 1) == 120) {
          cout << X + k + 2 << endl;
          break;
        }
      } else if (q == 0) {
        cout << vec.at(0) - 1 << endl;
        break;
      }
    }
  }
}


Error: 0

Fixed submission:

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

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  sort(vec.begin(), vec.end());
  int p, q;
  for (int j = 0; j < N; j++) {
    if (X != vec.at(j)) {
      p++;
    } else {
      q = j;
    }
  }
  if (p == N) {
    cout << X << endl;
  } else {
    for (int k = 0; k < N; k++) {
      if (vec.at(q - k - 1) != X - k - 1) {
        cout << X - k - 1 << endl;
        break;
      } else if (vec.at(q + k + 1) != X + k + 1) {
        cout << X + k + 1 << endl;
        break;
      }
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: 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[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;
}

Error: 0

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[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;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }

int main() {
  ll x, n;
  cin >> x >> n;
  if (!n) {
    cout << x << endl;
    return 0;
  }
  bitset<102> bs;
  rep(i, n) {
    int a;
    cin >> a;
    bs.set(a, true);
  }
  for (int i = 0;; i++) {
    if (!bs.test(x - i)) {
      cout << x - i << endl;
      break;
    }
    if (!bs.test(x + i)) {
      cout << x + i << endl;
      break;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }

int main() {
  ll x, n;
  cin >> x >> n;
  if (!n) {
    cout << x << endl;
    return 0;
  }
  bitset<101> bs;
  rep(i, n) {
    int a;
    cin >> a;
    bs.set(a, true);
  }
  for (int i = 0;; i++) {
    if (!bs.test(x - i)) {
      cout << x - i << endl;
      break;
    }
    if (!bs.test(x + i)) {
      cout << x + i << endl;
      break;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  for (int i = 0; i < x + 1; i++) {
    if (find(arr, arr + n, x + (i * -1)) == arr + n) {
      cout << x + (i * -1);
      break;
    } else if (find(arr, arr + n, x + i) == arr + n) {
      cout << x + i;
      break;
    }
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, const char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int x, n;
  cin >> x >> n;
  unordered_set<int> s;
  vector<int> v;

  if (n == 0 or n == 1) {
    cout << x;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    s.insert(x);
  }

  auto pair = minmax_element(s.begin(), s.end());
  for (int i = *pair.first; i < *pair.second; i++) {
    if (s.find(i) == s.end())
      v.push_back(i);
  }

  int minimum = v[0];
  for (int e : v) {
    if (abs(x - e) < abs(x - minimum))
      minimum = e;
  }
  cout << minimum;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); ++i)
#define pb push_back
#define all(v) v.begin(), v.end()
#define sort_1(v) sort(v.begin(), v.end())
#define sort_2(v) sort(v.begin(), v.end(), greater<ll>())
#define reverse(v) reverse(v.begin(), v.end())
typedef long long ll;
typedef long double la;
using namespace std;

int main() {
  int x, n;
  int mn = 1000000;
  cin >> x >> n;
  vector<int> p(n);

  vector<int> ans;

  map<int, int> m;
  map<int, int> a;
  rep(i, n) {
    cin >> p[i];
    m[p[i]] = 1;
  }

  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  // sort_1(p);
  rep(i, 200) {
    if (m[i] == 0) {
      a[i] = abs(x - i);
    }
  }
  for (auto x1 : a) {
    if (mn > x1.second && x1.second != 0) {
      mn = x1.second;
      continue;
    }
    if (x1.second == 0 && x == x1.first) {
      bool flag = true;
      for (int i = 0; i < n; i++) {
        if (x1.first == p[i]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        mn = x1.second;
      }
    }
  }
  for (auto x1 : a) {
    if (mn == x1.second) {
      ans.pb(x1.first);
    }
  }
  sort_1(ans);
  cout << ans[0] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); ++i)
#define pb push_back
#define all(v) v.begin(), v.end()
#define sort_1(v) sort(v.begin(), v.end())
#define sort_2(v) sort(v.begin(), v.end(), greater<ll>())
#define reverse(v) reverse(v.begin(), v.end())
typedef long long ll;
typedef long double la;
using namespace std;

int main() {
  int x, n;
  int mn = 1000000;
  cin >> x >> n;
  vector<int> p(n);

  vector<int> ans;

  map<int, int> m;
  map<int, int> a;
  rep(i, n) {
    cin >> p[i];
    m[p[i]] = 1;
  }

  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  // sort_1(p);
  for (int i = 1; i <= 100; i++) {
    if (m[i] == 0) {
      a[i] = abs(x - i);
    }
  }
  for (auto x1 : a) {
    if (mn > x1.second && x1.second != 0) {
      mn = x1.second;
      continue;
    }
    if (x1.second == 0 && x == x1.first) {
      bool flag = true;
      rep(i, n) {
        if (x1.first == p[i]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        mn = x1.second;
      }
    }
  }
  for (auto x1 : a) {
    if (mn == x1.second) {
      ans.pb(x1.first);
    }
  }
  sort_1(ans);
  cout << ans[0] << endl;
}
Problem p02641 (C++) — buggy submission, verdict: 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[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;
}


Error: 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.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;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

int main() {
  int p, X, N;
  cin >> X >> N;

  vector<int> arr;

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

  int ans;
  int res = 103;

  for (int i = 0; i <= 101; i++) {
    if (find(begin(arr), end(arr), i) == end(arr)) {
      if (res > abs(i - X)) {
        res = abs(i - X);
        ans = i;
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <stdio.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

int main() {
  int p, X, N;
  cin >> X >> N;
  // vectorのほうがいいかも
  vector<int> arr;

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

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

  int ans;
  int key;
  int res;
  // 2文探索？
  if (arr[0] >= X) {
    ans = X - 1;
  } else if (arr[N - 1] + 1 < N) {
    ans = X - 1;
  } else if (arr[N - 1] = N) {
    ans = X + 1;
  } else if (arr[0] < N && arr[N - 1] > N) {
    int ans = abs(arr[0] - X);
    for (int i = 0; i < N; i++) {
      if (ans > abs(arr[i] - X)) {
        res = arr[i];
        key = i;
      }
    }

    int ans1;
    for (int i = key; i > 0; i--) {
      if (find(arr.begin(), arr.end(), i) == arr.end()) {
        ans1 = i;
        break;
      }
    }

    int ans2;
    for (int i = key; i < N; i++) {
      if (find(arr.begin(), arr.end(), i) == arr.end()) {
        ans2 = i;
        break;
      }
    }

    if (abs(ans1 - X) >= abs(ans2 - X)) {
      ans = ans1;
    } else {
      ans = ans2;
    }
  }

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

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

int main() {

  int X, N, tmp, j;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }

  vector<int> p(N);
  rep(i, N) {
    cin >> tmp;
    p.at(i) = tmp;
  }
  bool torf = false, torf2 = false;

  // vector <bool> torf (N);

  // cout << X-100 << endl;
  rep(i, 100) {

    for (j = 0; j < N; j++) {
      if (X - i == p.at(j)) {
        torf = true;
      }
      if (X + i == p.at(j)) {

        torf2 = true;
      }
    }
    if (torf == false) {
      cout << X - i << endl;
      return 0;

    } else if (torf2 == false) {
      cout << X + i << endl;
      return 0;
    }
    torf = false;
    torf2 = false;
    // cout << torf << endl;
    /* if(torf==true){
       if(X-i!=p.at(j))
       cout << X-i << endl;
       else
         cout << X+i << endl;
       return 0;
     }


       torf=false;*/
  }
}

Error: 0

Fixed submission:

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

int main() {

  int X, N, tmp;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }

  vector<bool> p(100, false);
  rep(i, N) {
    cin >> tmp;
    p.at(tmp) = true;
  }

  rep(i, 100) {

    if (p.at(X - i) == false) {
      cout << X - i << endl;
      return 0;
    }
    if (p.at(X + i) == false) {
      cout << X + i << endl;
      return 0;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int X;
  int N;
  int Y[110];
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> Y[i];
  }
  bool flag = false;
  // 逐次検索
  for (int i = 0; i < N; i++) {
    if (X == Y[i]) {
      flag = false;
      break;
    }
    flag = true;
  }
  if (flag) {
    cout << X << endl;
    return 0;
  }
  for (int delta = 1; delta <= 50; delta++) {
    for (int i = 0; i < N; i++) {
      if (X - delta == Y[i]) {
        flag = false;
        break;
      }
      flag = true;
    }
    if (flag) {
      cout << X - delta << endl;
      return 0;
    }
    for (int i = 0; i < N; i++) {
      if (X + delta == Y[i]) {
        flag = false;
        break;
      }
      flag = true;
    }
    if (flag) {
      cout << X + delta << endl;
      return 0;
    }
  }
  return 1;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int X;
  int N;
  int Y[110];
  cin >> X >> N;
  for (int i = 0; i < N; i++) {
    cin >> Y[i];
  }
  bool flag = false;
  // 逐次検索
  for (int i = 0; i < N; i++) {
    if (X == Y[i]) {
      flag = false;
      break;
    }
    flag = true;
  }
  if (flag) {
    cout << X << endl;
    return 0;
  }
  for (int delta = 1; delta <= 50; delta++) {
    for (int i = 0; i < N; i++) {
      if (X + delta == Y[i]) {
        flag = false;
        break;
      }
      flag = true;
    }
    if (flag) {
      cout << X + delta << endl;
      return 0;
    }
    for (int i = 0; i < N; i++) {
      if (X - delta == Y[i]) {
        flag = false;
        break;
      }
      flag = true;
    }
    if (flag) {
      cout << X - delta << endl;
      return 0;
    }
  }
  return 1;
}
Problem p02641 (C++) — buggy submission, verdict: 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;
  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;
}


Error: TLE

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;
  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;
}