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

// AtCoder
// ABC C - Forbidden List

#include <algorithm>
#include <chrono>
#include <cmath>
#include <functional>
#include <iostream>
#include <random>
#include <string>
#include <vector>

using namespace std;
// using namespace std::chrono;

int main() {
  // steady_clock::time_point start = steady_clock::now();
  int X, N;

  cin >> X >> N;

  if (N > 0) {
    int p[N];
    for (int i = 0; i < N; i++) {
      cin >> p[i];
    }
    // 計算
    int ans = 500;
    int ans_id = -1;
    int dist;
    int cnt = 0;
    int res[200];
    int id = 0;
    int flag = 1;
    for (int i = 0; i <= 101; i++) {
      for (int j = 0; j < N; j++) {
        if (i == p[j])
          flag = 0;
      }
      if (flag == 1) {
        if (X - i >= 0) {
          dist = X - i;
        } else {
          dist = -(X - i);
        }
        if (ans >= dist) {
          ans = dist;
          ans_id = i;
          cnt += 1;
          res[id] = i;
          id = id + 1;
        }
      }
      flag = 1;
    }
    if (cnt == 1) {
      cout << res[0] << endl;
    } else {
      int ans_min = 500;
      int candidate;
      int c_id = 0;
      for (int i = 0; i < id; i++) {
        if (X - res[i] >= 0) {
          dist = X - res[i];
        } else {
          dist = -(X - res[i]);
        }
        if (ans_min > dist) {
          ans_min = dist;
          ans_id = res[i];
        } else if (ans_min == dist) {
          ans_min = dist;
          candidate = res[i];
          c_id++;
        }
      }
      if (c_id == 0) {
        cout << ans_id << endl;
      } else {
        cout << min(ans_id, candidate) << endl;
      }
    }
  } else {
    cout << X << endl;
  }

  // 計算時間集計
  /*
  steady_clock::time_point end = steady_clock::now();
steady_clock::duration d = end - start;
  cout << "計算時間は" << duration_cast<milliseconds>(d).count() <<
"ミリ秒です。\\n";
  */
}


Error: 0

Fixed submission:

// AtCoder
// ABC C - Forbidden List

#include <algorithm>
#include <chrono>
#include <cmath>
#include <functional>
#include <iostream>
#include <random>
#include <string>
#include <vector>

using namespace std;
// using namespace std::chrono;

int main() {
  // steady_clock::time_point start = steady_clock::now();
  int X, N;

  cin >> X >> N;

  if (N > 0) {
    int p[N];
    for (int i = 0; i < N; i++) {
      cin >> p[i];
    }
    // 計算
    int ans = 500;
    int ans_id = -1;
    int dist;
    int cnt = 0;
    int res[N];
    int id = 0;
    int flag = 1;
    for (int i = 0; i <= 101; i++) {
      for (int j = 0; j < N; j++) {
        if (i == p[j])
          flag = 0;
      }
      if (flag == 1) {
        if (X - i >= 0) {
          dist = X - i;
        } else {
          dist = -(X - i);
        }
        if (ans >= dist) {
          ans = dist;
          ans_id = i;
          cnt += 1;
          res[id] = i;
          id++;
        }
      }
      flag = 1;
    }
    if (cnt == 1) {
      cout << res[0] << endl;
    } else {
      int ans_min = 500;
      int candidate;
      int c_id = 0;
      for (int i = 0; i < id; i++) {
        if (X - res[i] >= 0) {
          dist = X - res[i];
        } else {
          dist = -(X - res[i]);
        }
        if (ans_min > dist) {
          ans_min = dist;
          ans_id = res[i];
        } else if (ans_min == dist) {
          ans_min = dist;
          candidate = res[i];
          c_id++;
        }
      }
      if (c_id == 0) {
        cout << ans_id << endl;
      } else {
        cout << min(ans_id, candidate) << endl;
      }
    }
  } else {
    cout << X << endl;
  }

  // 計算時間集計
  /*
  steady_clock::time_point end = steady_clock::now();
steady_clock::duration d = end - start;
  cout << "計算時間は" << duration_cast<milliseconds>(d).count() <<
"ミリ秒です。\\n";
  */
}