Problem p02819 (C++)

Buggy submission (status: Runtime Error):

// libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// namespace
using namespace std;
using namespace __gnu_pbds;

// typedef
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, greater<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set1;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long, long> pll;

// define
#define inp(X) cin >> X;
#define inp_long(X)                                                            \
  long long X;                                                                 \
  cin >> X;
#define inp_arr(A, N)                                                          \
  long long A[N];                                                              \
  for (long long int i = 0; i < N; i++) {                                      \
    cin >> A[i];                                                               \
  }
#define sz(A) A.size()
#define pb push_back
#define pf push_front
#define fi first
#define se second
// #define endl '\n'
#define vect(A) vector<long long> A;
#define inp_vect(A, N)                                                         \
  ;                                                                            \
  vect(A);                                                                     \
  for (long long i = 0; i < N; i++) {                                          \
    long long q;                                                               \
    cin >> q;                                                                  \
    A.push_back(q);                                                            \
  }
#define forin(i, a, b) for (long long i = a; i <= b; i++)
#define forde(i, a, b) for (long long i = a; i >= b; i--)
#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

const long long inf = (long long)1e18;
const long long MOD = (long long)(1e9 + 7);

ll z[100004] = {0};

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % MOD;
  p = (p * p) % MOD;

  return (y % 2 == 0) ? p : (x * p) % MOD;
}
ll modInverse(ll a) { return power(a, MOD - 2); }
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen ("OUTPUT.txt" , "w" , stdout);
#endif
  fio;

  forin(i, 2, 1e5) {
    ll d = 0;
    forin(j, 2, sqrt(i)) {
      if (i % j == 0) {
        d++;
        break;
      }
    }
    if (d) {
      z[i] = 0;
    } else {
      z[i] = 1;
    }
  }
  z[100003] = 1;
  ll n;
  cin >> n;
  forin(i, n, 100003) {
    if (z[i]) {
      cout << i << endl;
      break;
    }
  }
}

Fixed submission:

// libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// namespace
using namespace std;
using namespace __gnu_pbds;

// typedef
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, greater<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set1;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long, long> pll;

// define
#define inp(X) cin >> X;
#define inp_long(X)                                                            \
  long long X;                                                                 \
  cin >> X;
#define inp_arr(A, N)                                                          \
  long long A[N];                                                              \
  for (long long int i = 0; i < N; i++) {                                      \
    cin >> A[i];                                                               \
  }
#define sz(A) A.size()
#define pb push_back
#define pf push_front
#define fi first
#define se second
// #define endl '\n'
#define vect(A) vector<long long> A;
#define inp_vect(A, N)                                                         \
  ;                                                                            \
  vect(A);                                                                     \
  for (long long i = 0; i < N; i++) {                                          \
    long long q;                                                               \
    cin >> q;                                                                  \
    A.push_back(q);                                                            \
  }
#define forin(i, a, b) for (long long i = a; i <= b; i++)
#define forde(i, a, b) for (long long i = a; i >= b; i--)
#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

const long long inf = (long long)1e18;
const long long MOD = (long long)(1e9 + 7);

ll z[100004] = {0};

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % MOD;
  p = (p * p) % MOD;

  return (y % 2 == 0) ? p : (x * p) % MOD;
}
ll modInverse(ll a) { return power(a, MOD - 2); }
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int main() {
  /*
 #ifndef ONLINE_JUDGE
     freopen ("input.txt" , "r" , stdin);
              // freopen ("OUTPUT.txt" , "w" , stdout);
      #endif */
  fio;

  forin(i, 2, 1e5) {
    ll d = 0;
    forin(j, 2, sqrt(i)) {
      if (i % j == 0) {
        d++;
        break;
      }
    }
    if (d) {
      z[i] = 0;
    } else {
      z[i] = 1;
    }
  }
  z[100003] = 1;
  ll n;
  cin >> n;
  forin(i, n, 100003) {
    if (z[i]) {
      cout << i << endl;
      break;
    }
  }
}