alexjercan/bugnet huggingface dataset:
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool sosuu[INF];
int main() {
  memset(sosuu, true, sizeof(sosuu));
  sosuu[0] = sosuu[1] = false;
  for (int a = 2; a < INF; a++) {
    if (sosuu[a]) {
      for (int b = a * 2; b < INF; b += a)
        sosuu[b] = false;
    }
  }
  int x, y;
  while (cin >> x >> y, (x + 1) | (y + 1)) {
    int s[20000];
    fill(s, s + (y + 1) * (y + 1), 1 << 29);
    int sum = 0, l = 0;
    for (int z = x + 1; l <= y; z++) {
      if (sosuu[z]) {
        l++;
        int r = l;
        for (int i = z; r <= y; i++) {
          if (sosuu[i]) {
            r++;
            s[sum] = z + i;
            sum++;
          }
        }
      }
    }
    sort(s, s + (y + 1) * (y + 1));
    cout << s[y - 1] << endl;
  }
}

Error: TLE

Fixed submission:

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

bool sosuu[INF];
int main() {
  memset(sosuu, true, sizeof(sosuu));
  sosuu[0] = sosuu[1] = false;
  for (int a = 2; a < INF; a++) {
    if (sosuu[a]) {
      for (int b = a * 2; b < INF; b += a)
        sosuu[b] = false;
    }
  }
  int x, y;
  while (cin >> x >> y, x | y) {
    int s[20000];
    fill(s, s + (y + 1) * (y + 1), 1 << 29);
    int sum = 0, l = 0;
    for (int z = x + 1; l <= y; z++) {
      if (sosuu[z]) {
        l++;
        int r = l;
        for (int i = z; r <= y; i++) {
          if (sosuu[i]) {
            r++;
            s[sum] = z + i;
            sum++;
          }
        }
      }
    }
    sort(s, s + (y + 1) * (y + 1));
    cout << s[y] << endl;
  }
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
const int MAXN = 2 * 1e5;
vector<int> pri(2 * (int)1e5, true);

void prim() {
  pri[1] = false;
  for (int i = 2; i * i < MAXN; i++) {
    if (pri[i])
      for (int j = 2 * i; j < MAXN; j += i)
        if (pri[j])
          pri[j] = false;
  }
}
int main() {
  int n, p;
  prim();
  while (cin >> n >> p, n != -1) {
    vector<int> twop;
    int limitt = n + n / 100 + p * 10;
    for (int i = n + 1; i < limitt; i++) {
      if (pri[i])
        for (int j = i; j < limitt; j++) {
          if (pri[j])
            twop.push_back(i + j);
        }
    }
    sort(twop.begin(), twop.end());
    cout << twop[p - 1] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
const int MAXN = 2 * 1e5;
vector<int> pri(2 * (int)1e5, true);

void prim() {
  pri[1] = false;
  for (int i = 2; i * i < MAXN; i++) {
    if (pri[i])
      for (int j = 2 * i; j < MAXN; j += i)
        if (pri[j])
          pri[j] = false;
  }
}
int main() {
  int n, p;
  prim();
  while (cin >> n >> p, n != -1) {
    vector<int> twop;
    int limitt = n + p * 10;
    for (int i = n + 1; i < limitt; i++) {
      if (pri[i])
        for (int j = i; j < limitt; j++) {
          if (pri[j])
            twop.push_back(i + j);
        }
    }
    sort(twop.begin(), twop.end());
    cout << twop[p - 1] << endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF 1010001000
#define EPS 1e-10
#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef pair<int, int> i_i;
typedef long long int llong;
typedef pair<llong, llong> ll_ll;
typedef struct edge {
  int f, t;
} Edge;

vector<bool> prime(111000, true);

void sieve();

int main() {
  int n, m;
  sieve();
  while (cin >> n >> m, n != -1) {
    int cnt = 0;
    vector<int> ans;
    for (int i = n + 1; cnt < 100; i++) {
      if (prime[i]) {
        int ct = 0;
        for (int j = i; ct < 100; j++) {
          if (prime[j]) {
            ans.push_back(i + j);
            ct++;
          }
        }
        cnt++;
      }
    }
    sort(ans.begin(), ans.end());

    cout << ans[m - 1] << endl;
  }

  return 0;
}

void sieve() {
  prime[0] = prime[1] = false;
  for (int i = 2; i * i < 111000; i++) {
    if (prime[i]) {
      for (int j = 2; i * j < 111000; j++) {
        prime[i * j] = false;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF 1010001000
#define EPS 1e-10
#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef pair<int, int> i_i;
typedef long long int llong;
typedef pair<llong, llong> ll_ll;
typedef struct edge {
  int f, t;
} Edge;

vector<bool> prime(111000, true);

void sieve();
bool make(int num, int n);

int main() {
  int n, m;
  sieve();
  while (cin >> n >> m, n != -1) {
    int cnt = 0, ret = n + 1;
    while (cnt < m) {
      cout << ret << " ";
      ret++;
      if (make(ret, n)) {
        cnt++;
      }
    }
    cout << endl;
    cout << ret << endl;
  }
  return 0;
}

bool make(int num, int n) {
  for (int i = n; i * n < num; i++) {
    if (prime[i] && prime[num - i]) {
      return true;
    }
  }
  return false;
}

void sieve() {
  prime[0] = prime[1] = false;
  for (int i = 2; i * i < 111000; i++) {
    if (prime[i]) {
      for (int j = 2; i * j < 111000; j++) {
        prime[i * j] = false;
      }
    }
  }
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  int prime[300010] = {};
  int b = 0;
  for (int i = 2; i <= 200274; i++) {
    if (prime[i] == 0) {
      prime[i] = 1;
      b++;
      for (int j = 2 * i; j <= 200274; j += i) {
        prime[j] = -1;
      }
    }
  }
  vector<int> p(b);
  int a = 0;
  for (int i = 0; i < 200274; i++) {
    if (prime[i] == 1) {
      p[a] = i;
      a++;
    }
  }
  int s[200275];
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == -1 && m == -1)
      break;
    for (int i = 0; i < 200275; i++)
      s[i] = 0;
    int c;
    for (int i = 0; i < 200275; i++) {
      if (p[i] > n) {
        c = i;
        break;
      }
    }
    for (int i = c; i <= c + m; i++) {
      for (int j = i; j <= c + m; j++) {
        int sum = p[i] + p[j];
        if (sum <= 200274)
          s[sum]++;
      }
    }
    int t = 0;
    for (int i = 4; i < 200275; i++) {
      t += s[i];
      if (t >= m) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  int prime[300010] = {};
  int b = 0;
  for (int i = 2; i <= 200274; i++) {
    if (prime[i] == 0) {
      prime[i] = 1;
      b++;
      for (int j = 2 * i; j <= 200274; j += i) {
        prime[j] = -1;
      }
    }
  }
  vector<int> p(b);
  int a = 0;
  for (int i = 0; i < 200274; i++) {
    if (prime[i] == 1) {
      p[a] = i;
      a++;
    }
  }
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == -1 && m == -1)
      break;
    long long s[200275] = {};
    for (int i = 0; i < b; i++) {
      for (int j = i; j < b; j++) {
        int sum = p[i] + p[j];
        if (sum <= 200274 && p[i] > n && p[j] > n)
          s[sum]++;
      }
    }
    long long t = 0;
    for (int i = 4; i < 200275; i++) {
      t += s[i];
      if (t >= m) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
bool u[200000];
int main() {
  fill(u, u + 200000, true);
  u[0] = false;
  u[1] = false;
  for (int i = 2; i < 200000; i++) {
    if (u[i]) {
      int j = 2;
      while (i * j < 200000) {
        u[i * j] = false;
        j++;
      }
    }
  }
  int n, p;
  while (cin >> n >> p && n != -1 || p != -1) {
    int a[100], f = 0;
    for (int i = n + 1; i < 200000 && f < p; i++) {
      if (u[i]) {
        a[f] = i;
        f++;
      }
    }
    int b[10000];
    f = 0;
    for (int i = 0; i < p; i++) {
      for (int j = i; j < p; j++) {
        b[f] = a[i] + a[j];
        f++;
      }
    }
    sort(b, b + f);
    cout << b[p - 1] << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
vector<int> a;
bool b[1000000];
int main() {
  fill(b, b + 1000000, true);
  b[0] = false;
  b[1] = false;
  for (int i = 2; i < 1000000; i++) {
    if (b[i]) {
      a.push_back(i);
      int j = 2;
      while (i * j < 1000000) {
        b[i * j] = false;
        j++;
      }
    }
  }
  int n = 0, p = 55;
  while (cin >> n >> p && n != -1) {
    int c = sqrt(p) + 2;
    int i = 0;
    while (a[i] < n)
      i++;
    multiset<long long> d;
    for (int j = i; j <= i + c; j++) {
      for (int z = i; z <= i + c; j++) {
        d.insert(i + j);
      }
    }
    multiset<long long>::iterator it = d.begin();
    for (int i = 0; i < p; i++) {
      it++;
    }
    cout << (*it) << endl;
  }
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

bool check(int x) {
  if (x == 2)
    return true;
  if (x == 1 || x % 2 == 0)
    return false;
  for (int i = 3; i <= sqrt(x); i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, p;
  vector<int> v, res;
  while (1) {
    cin >> n >> p;
    if (n < 0)
      break;
    v.clear();
    res.clear();
    for (int i = n + 1;; i++) {
      if (check(i))
        v.push_back(i);
      if (v.size() > 100)
        break;
    }
    // for(int i = 0;i < 100;i++){
    //	cout << v[i] << endl;
    // }
    for (int i = 0; i < 100; i++) {
      for (int j = i; j < 100; j++) {
        res.push_back(v[i] + v[j]);
      }
    }
    sort(res.begin(), res.end());
    cout << res[p - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

bool check(int x) {
  if (x == 2)
    return true;
  if (x == 1 || x % 2 == 0)
    return false;
  for (int i = 3; i <= sqrt(x); i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, p;
  vector<int> v, res;
  while (1) {
    cin >> n >> p;
    if (n < 0)
      break;
    v.clear();
    res.clear();
    for (int i = n + 1;; i++) {
      if (check(i))
        v.push_back(i);
      if (v.size() > 1000)
        break;
    }
    // for(int i = 0;i < 100;i++){
    //	cout << v[i] << endl;
    // }
    for (int i = 0; i < 1000; i++) {
      for (int j = i; j < 1000; j++) {
        res.push_back(v[i] + v[j]);
      }
    }
    sort(res.begin(), res.end());
    cout << res[p - 1] << endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

#define MAX 1000001

vector<int> vprimes;
bool *isPrime = new bool[MAX];

/* ツ素ツ青板青カツ青ャ O(n^2) */
void MakePrime() {
  isPrime[0] = false;
  isPrime[1] = false;
  isPrime[2] = true;
  for (int i = 2; i < MAX; ++i) {
    isPrime[i] = true;
  }
  for (long long int i = 2; i < MAX; ++i) {
    if (isPrime[i]) {
      for (long long int j = 2; i * j < MAX; ++j) {
        isPrime[i * j] = false;
      }
    }
  }
  for (int i = 0; i < MAX; ++i) {
    if (isPrime[i]) {
      vprimes.push_back(i);
    }
  }
}

int main() {
  MakePrime();
  int N, P;
  while (cin >> N >> P && (N >= 0) && (P >= 0)) {
    vector<int> v;
    vector<int> primes;
    for (int i = 0; i < vprimes.size(); ++i) {
      if (vprimes[i] > N) {
        primes.push_back(vprimes[i]);
      }
    }
    for (int i = 0; i < primes.size(); ++i) {
      for (int j = i; j < min(2 * P, (int)primes.size()); ++j) {
        v.push_back(primes[i] + primes[j]);
      }
    }
    sort(v.begin(), v.end());
    cout << v[P - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

#define MAX 1000001

vector<int> vprimes;
bool *isPrime = new bool[MAX];

/* ツ素ツ青板青カツ青ャ O(n^2) */
void MakePrime() {
  isPrime[0] = false;
  isPrime[1] = false;
  isPrime[2] = true;
  for (int i = 2; i < MAX; ++i) {
    isPrime[i] = true;
  }
  for (long long int i = 2; i < MAX; ++i) {
    if (isPrime[i]) {
      for (long long int j = 2; i * j < MAX; ++j) {
        isPrime[i * j] = false;
      }
    }
  }
  for (int i = 0; i < MAX; ++i) {
    if (isPrime[i]) {
      vprimes.push_back(i);
    }
  }
}

int main() {
  MakePrime();
  int N, P;
  while (cin >> N >> P && (N >= 0) && (P >= 0)) {
    vector<int> v;
    vector<int> primes;
    for (int i = 0; i < vprimes.size(); ++i) {
      if (vprimes[i] > N)
        primes.push_back(vprimes[i]);
    }
    for (int i = 0; i < primes.size(); ++i) {
      for (int j = i; j < primes.size(); ++j) {
        v.push_back(primes[i] + primes[j]);
        if (v.size() > MAX)
          break;
      }
    }
    nth_element(v.begin(), v.begin() + P, v.end());
    cout << v[P - 1] << endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair<int, int> iP;
typedef pair<iP, int> iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

#define MAX 1000000

vector<int> p;
vector<bool> isP(MAX, true);

void makePrime() {
  isP[0] = isP[1] = false;
  for (int i = 2; i * i < MAX; i++)
    for (int j = i + i; j < MAX; j += i)
      isP[j] = false;

  for (int i = 2; i < MAX; i++) {
    if (isP[i])
      p.push_back(i);
  }
}

int main() {

  makePrime();

  int N, P;
  while (cin >> N >> P, ~N) {
    int off = upper_bound(p.begin(), p.end(), N) - p.begin();

    vector<int> M;
    for (int i = 0; i < P; i++) {
      for (int j = i; j < P; j++) {
        M.push_back(p[off + i] + p[off + j]);
      }
    }
    sort(M.begin(), M.end());
    cout << M[P - 1] << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair<int, int> iP;
typedef pair<iP, int> iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

#define MAX 200001

void makePrime(vector<bool> &isP, vector<vector<int>> &setP) {
  isP[0] = isP[1] = false;
  for (int i = 2; i * i < MAX; i++)
    for (int j = i + i; j < MAX; j += i)
      isP[j] = false;

  vector<int> tmp;
  for (int i = 2; i < MAX; i++)
    if (isP[i])
      tmp.push_back(i);

  for (int i = 0; i < tmp.size(); i++) {
    for (int j = i; j < tmp.size(); j++) {
      setP[tmp[i] + tmp[j]].push_back(min(tmp[i], tmp[j]));
    }
  }
}

int main() {

  vector<vector<int>> setP(MAX * 2);
  vector<bool> isP(MAX, true);
  makePrime(isP, setP);

  int N, P;
  while (cin >> N >> P, ~N) {
    int cnt = 0;
    for (int i = 4; i < MAX * 2; i++) {
      for (int j = 0; j < setP[i].size(); j++) {
        if (setP[i][j] > N)
          cnt++;
        if (cnt == P) {
          cout << i << endl;
          break;
        }
      }
      if (cnt == P)
        break;
    }
  }
}
Problem p01268 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int L = 1e7;
const int M = 30;

bool p[L];

void make_p() {
  REP(i, L) p[i] = true;
  p[0] = p[1] = false;
  for (int i = 2; i * i <= L; ++i) {
    if (!p[i])
      continue;
    for (int j = i + i; j < L; j += i)
      p[j] = false;
  }
}

int main() {
  make_p();
  int N, P;
  while (cin >> N >> P && N != -1) {
    vector<int> _p(M), v;
    for (int i = N + 1, cnt = 0; cnt < M; ++i)
      if (p[i])
        _p[cnt++] = i;
    REP(i, M) FOR(j, i, M) v.push_back(_p[i] + _p[j]);
    SORT(v);
    cout << v[P - 1] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int L = 1e8;
const int M = 30;

bool p[L];

void make_p() {
  REP(i, L) p[i] = true;
  p[0] = p[1] = false;
  for (int i = 2; i * i <= L; ++i) {
    if (!p[i])
      continue;
    for (int j = i + i; j < L; j += i)
      p[j] = false;
  }
}

int main() {
  make_p();
  int N, P;
  while (cin >> N >> P && N != -1) {
    // vector<int> _p(M), v;
    // for(int i = N + 1, cnt = 0; cnt < M; ++i) if(p[i]) _p[cnt++] = i;
    // REP(i, M) FOR(j, i, M) v.push_back(_p[i] + _p[j]);
    // SORT(v);
    // cout <<v[P - 1] <<endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MAX_N (123456)
typedef vector<int> VI;
#define pb push_back
#define all(vec) (vec).begin(), (vec).end()

bool prime[MAX_N + 1];

int main() {
  for (int i = 2; i <= MAX_N; i++)
    prime[i] = true;
  for (int i = 2; i * i <= MAX_N; i++)
    for (int j = i * i; j <= MAX_N; j += i)
      prime[j] = false;

  int n, p;
  while (scanf("%d %d", &n, &p), n != -1) {
    VI v;
    for (int i = n + 1, j = 0; j < p; i++) {
      if (prime[i]) {
        v.pb(i);
        j++;
      }
    }
    VI vv;
    for (int i = 0; i < p; i++) {
      for (int j = i; j < p; j++) {
        vv.pb(v[i] + v[j]);
      }
    }
    sort(all(vv));

    for (int i = 0; i < vv.size(); i++) {
      // printf("%d ", vv[i]);
    }

    printf("%d\\n", vv[p - 1]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <string.h>
using namespace std;

#define MAX (100000)

bool prime[MAX + 1];
int prime_sum[MAX * 2 + 1];

int main() {
  prime[0] = prime[1] = false;
  for (int i = 2; i <= MAX; i++) {
    prime[i] = true;
  }

  for (int i = 2; i * i <= MAX; i++) {
    for (int j = i * i; j <= MAX; j += i) {
      prime[j] = false;
    }
  }

  int N, P;

  while (scanf("%d %d", &N, &P), N + P != -2) {
    int cnt = 0;
    int i = N * 2 + 2;

    memset(prime_sum, 0, sizeof(prime_sum));
    for (int k = N + 1; k <= MAX; k++) {
      for (int l = k; l <= MAX; l++) {
        if (prime[k] && prime[l]) {
          prime_sum[k + l]++;
        }
      }
    }

    while (1) {
      for (int j = 0; j < prime_sum[i]; j++) {
        cnt++;
        printf("%d\\n", i);
      }

      if (cnt >= P) {
        break;
      }
      i++;
    }

    printf("%d\\n", i);
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const int N = 150000;

int main() {
  int n, p;
  VI primes, d(N, 1);
  FOR(i, 2, N - 1) {
    if (!d[i])
      continue;
    primes.push_back(i);
    int x = 2 * i;
    while (x < N) {
      d[x] = 0;
      x += i;
    }
  }
  while (cin >> n >> p && p >= 0) {
    VI cand;
    int i = 0;
    while (primes[i] <= n)
      i++;
    FOR(j, i, i + p) FOR(k, i, j) { cand.push_back(primes[j] + primes[k]); }
    sort(cand.begin(), cand.end());
    cout << cand[p - 1] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const int N = 150000;

int main() {
  int n, p;
  VI primes, d(N, 1);
  FOR(i, 2, N - 1) {
    if (!d[i])
      continue;
    primes.push_back(i);
    int x = 2 * i;
    while (x < N) {
      d[x] = 0;
      x += i;
    }
  }
  while (cin >> n >> p && p) {
    VI cand;
    int i = 0;
    while (primes[i] <= n)
      i++;
    FOR(j, i, i + p) FOR(k, i, j) { cand.push_back(primes[j] + primes[k]); }
    sort(cand.begin(), cand.end());
    cout << cand[p - 1] << endl;
  }
  return 0;
}