Problem p02819 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define vmax 10000
constexpr ll inf = 100000000000000;
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

// 最小公倍数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

// 素因数分解
map<int, int> prime_factor(int n, map<int, int> &res) {
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1) {
    if (res[n] == 0) {
      res[n] = 1;
    } else {
      res[n]++;
    }
  }
  return res;
}

// 約数列挙
vector<ll> divisor(ll n) {
  vector<ll> a;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      a.push_back(i);
      if (i * i != n) {
        a.push_back(n / i);
      }
    }
  }
  SORT(a);
  return a;
}

// 拡張ユークリッドの互除法
int extgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int d = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
  }
}

// 素数判定
void sieve(int n, vector<bool> &is_prime) {
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return;
}

// 繰り返し2乗法
ll mod_pow(ll x, ll n, ll mod) {
  x %= mod;
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

// mod　m での a の逆元
int mod_inverse(int a, int m) {
  int x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

// nCkの前計算
void init(ll n, ll m, vector<ll> &factrial, vector<ll> &inverse) {
  factrial.resize(n + 1);
  inverse.resize(n + 1);
  factrial[0] = 1;
  inverse[0] = 1;
  for (ll i = 1; i <= n; i++) {
    factrial[i] = factrial[i - 1] * i % m;
    inverse[i] = mod_inverse(factrial[i], m);
  }
}

// nCk
ll nCk(ll n, ll k, ll m, vector<ll> &factrial, vector<ll> &inverse) {
  if (n < 0 || k < 0 || n < k) {
    return 0;
  }
  return factrial[n] * inverse[k] % m * inverse[n - k] % m;
}

int main() {
  int n;
  cin >> n;
  vector<bool> a(1e6, true);
  sieve(1e6 + 3, a);
  for (int i = n; i < 1e6 + 3; i++) {
    if (a[i]) {
      cout << i << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define vmax 10000
constexpr ll inf = 100000000000000;
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

// 最小公倍数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

// 素因数分解
map<int, int> prime_factor(int n, map<int, int> &res) {
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1) {
    if (res[n] == 0) {
      res[n] = 1;
    } else {
      res[n]++;
    }
  }
  return res;
}

// 約数列挙
vector<ll> divisor(ll n) {
  vector<ll> a;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      a.push_back(i);
      if (i * i != n) {
        a.push_back(n / i);
      }
    }
  }
  SORT(a);
  return a;
}

// 拡張ユークリッドの互除法
int extgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int d = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
  }
}

// 素数判定
void sieve(int n, vector<bool> &is_prime) {
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return;
}

// 繰り返し2乗法
ll mod_pow(ll x, ll n, ll mod) {
  x %= mod;
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

// mod　m での a の逆元
int mod_inverse(int a, int m) {
  int x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

// nCkの前計算
void init(ll n, ll m, vector<ll> &factrial, vector<ll> &inverse) {
  factrial.resize(n + 1);
  inverse.resize(n + 1);
  factrial[0] = 1;
  inverse[0] = 1;
  for (ll i = 1; i <= n; i++) {
    factrial[i] = factrial[i - 1] * i % m;
    inverse[i] = mod_inverse(factrial[i], m);
  }
}

// nCk
ll nCk(ll n, ll k, ll m, vector<ll> &factrial, vector<ll> &inverse) {
  if (n < 0 || k < 0 || n < k) {
    return 0;
  }
  return factrial[n] * inverse[k] % m * inverse[n - k] % m;
}

int main() {
  int n;
  cin >> n;
  vector<bool> a(1e6 + 10, true);
  sieve(1e6 + 3, a);
  for (int i = n; i < 1e6 + 3; i++) {
    if (a[i]) {
      cout << i << endl;
      return 0;
    }
  }
}