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

// ######      ###      #######    #######    ##     #     #####        ###
// ##### // #     #    #   #          #        #       # #    #    #     # #   #
// ###  // #     #   #     #        #         #       #  #   #   #       #    #
// #    ###  //
// ######   #########      #          #       #   #  #   #           #########
// #   // #     #  #       #     #           #       #    # #   #    ####   # #
// #   // #     #  #       #    #            #       #     ##   #    #  #   # #
// //
// ######   #       #   #######    #######    #      #    #####  #   #       #
// #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define ain(a, n)                                                              \\
  for (ll i = 0; i < (n); ++i)                                                 \\
    cin >> (a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i, n) for (ll i = 0; i < (n); ++i)
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (ll i = (a); i >= (b); --i)
#define cases                                                                  \\
  ll T = 0;                                                                    \\
  cin >> T;                                                                    \\
  while (T--)
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define END "\\n"
#define pb push_back
#define go(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)
#define back(c, itr) for (auto itr = (c).rbegin(); itr != (c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MAXN 2005
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 1e9;
ll binexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
void task(bool flag) {
  if (flag)
    cout << "YES\\n";
  else
    cout << "NO\\n";
}
ll lcm(ll a, ll b) { return ((1LL * a * b) / (__gcd(a, b))); }

signed main() {

  fast
  // cases
  {
    ll x, n;
    cin >> x >> n;
    vl a(n);
    unordered_map<ll, ll> pres;
    loop(i, n) cin >> a[i], ++pres[a[i]];
    ll ans = 1, diff = inf;
    if (!a.empty()) {
      FORD(i, x, 0) {
        if (pres[i] == 0) {
          if (abs(i - x) < diff) {
            ans = i;
            diff = abs(i - x);
          }
        }
      }
      ll i = x, ans2 = inf, diff2 = inf;
      while (1) {
        if (pres[i] == 0) {
          ans2 = i;
          diff2 = abs(i - x);
          break;
        }
        ++i;
      }
      // cout << diff2 << " " << diff << END;
      if (diff2 < diff)
        ans = ans2;
    } else
      ans = x;
    cout << ans << END;
  }

  return 0;
}

Error: 0

Fixed submission:

// ######      ###      #######    #######    ##     #     #####        ###
// ##### // #     #    #   #          #        #       # #    #    #     # #   #
// ###  // #     #   #     #        #         #       #  #   #   #       #    #
// #    ###  //
// ######   #########      #          #       #   #  #   #           #########
// #   // #     #  #       #     #           #       #    # #   #    ####   # #
// #   // #     #  #       #    #            #       #     ##   #    #  #   # #
// //
// ######   #       #   #######    #######    #      #    #####  #   #       #
// #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define ain(a, n)                                                              \\
  for (ll i = 0; i < (n); ++i)                                                 \\
    cin >> (a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i, n) for (ll i = 0; i < (n); ++i)
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (ll i = (a); i >= (b); --i)
#define cases                                                                  \\
  ll T = 0;                                                                    \\
  cin >> T;                                                                    \\
  while (T--)
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define END "\\n"
#define pb push_back
#define go(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)
#define back(c, itr) for (auto itr = (c).rbegin(); itr != (c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MAXN 2005
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 1e9;
ll binexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
void task(bool flag) {
  if (flag)
    cout << "YES\\n";
  else
    cout << "NO\\n";
}
ll lcm(ll a, ll b) { return ((1LL * a * b) / (__gcd(a, b))); }

signed main() {

  fast
  // cases
  {
    ll x, n;
    cin >> x >> n;
    vl a(n);
    unordered_map<ll, ll> pres;
    loop(i, n) cin >> a[i], ++pres[a[i]];
    ll ans = 1, diff = inf;
    FOR(i, 1, *max_element(all(a)) + 1) {
      if (pres[i] == 0) {
        if (abs(i - x) < diff) {
          ans = i;
          diff = abs(i - x);
        }
      }
    }
    cout << ans << END;
  }

  return 0;
}