alexjercan/bugnet huggingface dataset:
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUM 300000

int main(void) {
  int n, p, i, j, count1, count2, flag[MAXNUM + 1], tmp[MAXNUM + 1];
  for (i = 0; i <= MAXNUM; i++)
    flag[i] = 1;
  flag[0] = flag[1] = 0;
  for (i = 2; i <= sqrt((double)MAXNUM); i++) {
    for (j = 2 * i; j <= MAXNUM; j += i)
      flag[j] = 0;
  }
  while (1) {
    scanf("%d%d", &n, &p);
    if (n == -1 && p == -1)
      break;
    count1 = 0;
    memset(tmp, 0, sizeof(tmp));
    for (i = n + 1; count1 < p; i++) {
      if (flag[i]) {
        count1++;
        count2 = 0;
        for (j = i; count2 < p; j++) {
          if (flag[j]) {
            count2++;
            tmp[i + j]++;
          }
        }
      }
    }
    count1 = 0;
    for (i = n + 1; count1 < p; i++) {
      if (tmp[i])
        count1 += tmp[i];
    }
    printf("%d\\n", i - 1);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUM 200274

int main(void) {
  int n, p, i, j, count1, count2, flag[MAXNUM + 1], tmp[MAXNUM + 1];
  for (i = 0; i <= MAXNUM; i++)
    flag[i] = 1;
  flag[0] = flag[1] = 0;
  for (i = 2; i <= sqrt((double)MAXNUM); i++) {
    for (j = 2 * i; j <= MAXNUM; j += i)
      flag[j] = 0;
  }
  while (1) {
    scanf("%d%d", &n, &p);
    if (n == -1 && p == -1)
      break;
    count1 = 0;
    memset(tmp, 0, sizeof(tmp));
    for (i = n + 1; count1 < p; i++) {
      if (flag[i]) {
        count1++;
        count2 = 0;
        for (j = i; count2 < p; j++) {
          if (flag[j]) {
            count2++;
            tmp[i + j]++;
          }
        }
      }
    }
    count1 = 0;
    for (i = n + 1; count1 < p; i++) {
      if (tmp[i])
        count1 += tmp[i];
    }
    printf("%d\\n", i - 1);
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> Prime(1000000), Matsuzaki(100000);

int judge(int n) {
  if (n < 2)
    return 0;
  if (n == 2)
    return 1;
  if (n % 2 == 0)
    return 0;
  for (int i = 3; i <= n / i; i += 2) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  int a = 0, b = 0, N, P, ii;
  for (int i = 2; i < 1000000; i++) {
    if (judge(i))
      Prime[b++] = i;
  }
  while (1) {
    cin >> N >> P;
    if (N == -1 && P == -1)
      break;
    a = 0;
    for (int i = 0; i < b; i++) {
      if (Prime[i] > N) {
        ii = i;
        break;
      }
    }
    for (int i = ii; i < ii + 100; i++) {
      for (int j = i; j < ii + 100; j++) {
        Matsuzaki[a] = Prime[i] + Prime[j];
        a++;
      }
    }
    sort(Matsuzaki.begin(), Matsuzaki.begin() + a);
    cout << Matsuzaki[P - 1] << endl;
  }
  return 0;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> Prime(100000), Matsuzaki(1000);

int judge(int n) {
  if (n < 2)
    return 0;
  if (n == 2)
    return 1;
  if (n % 2 == 0)
    return 0;
  for (int i = 3; i <= n / i; i += 2) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  int a = 0, b = 0, N, P, ii;
  for (int i = 2; i < 1000000; i++) {
    if (judge(i))
      Prime[b++] = i;
  }
  while (1) {
    cin >> N >> P;
    if (N == -1 && P == -1)
      break;
    a = 0;
    for (int i = 0; i < b; i++) {
      if (Prime[i] > N) {
        ii = i;
        break;
      }
    }
    for (int i = ii; i < ii + 100; i++) {
      for (int j = i; j < ii + 100; j++) {
        Matsuzaki[a] = Prime[i] + Prime[j];
        a++;
      }
    }
    sort(Matsuzaki.begin(), Matsuzaki.begin() + a);
    cout << Matsuzaki[P - 1] << endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>

#define MEMZERO(x) memset(x, 0, sizeof(x))
#define MEMSET(x, y) memset(x, y, sizeof(x))

using namespace std;

const int SIZE = 101200;
bool isPrime[SIZE];
int prime[SIZE / 2], pSize;
int table[5100];

int memo[10000][100];

void setPrime(void) {
  pSize = 0;
  for (int i = 2; i < SIZE; ++i)
    isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < SIZE; ++i) {
    if (!isPrime[i])
      continue;
    prime[pSize++] = i;
    for (int j = i + i; j < SIZE; j += i) {
      isPrime[j] = false;
    }
  }
}

int search_index(int num) {
  int l = 0, h = pSize;
  while (l < h) {
    int m = (l + h) / 2;
    if (prime[m] < num)
      l = m + 1;
    else if (prime[m] > num)
      h = m;
    else
      return (m + 1);
  }
  return l;
}

int main(void) {
  setPrime();
  for (int i = 0; i < 10000; ++i)
    for (int j = 0; j < 100; ++j)
      memo[i][j] = -1;
  int m, p;
  while (cin >> m >> p, m != -1 || p != -1) {
    MEMZERO(table);
    int ii = search_index(m);
    if (memo[ii][p - 1] != -1) {
      cout << memo[ii][p - 1] << endl;
      continue;
    }
    int count = 0;
    for (int i = 0; i < 100; ++i)
      for (int j = i; j < 100; ++j) {
        table[count++] = prime[i + ii] + prime[j + ii];
      }
    sort(table, table + count);
    cout << table[p - 1] << endl;
    for (int i = 0; i < 100; ++i)
      memo[ii][i] = table[i];
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

#define MEMZERO(x) memset(x, 0, sizeof(x))
#define MEMSET(x, y) memset(x, y, sizeof(x))

using namespace std;

const int SIZE = 101200;
bool isPrime[SIZE];
int prime[SIZE / 2], pSize;
int table[5100];

int memo[100001][100];

void setPrime(void) {
  pSize = 0;
  for (int i = 2; i < SIZE; ++i)
    isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < SIZE; ++i) {
    if (!isPrime[i])
      continue;
    prime[pSize++] = i;
    for (int j = i + i; j < SIZE; j += i) {
      isPrime[j] = false;
    }
  }
}

int search_index(int num) {
  int l = 0, h = pSize;
  while (l < h) {
    int m = (l + h) / 2;
    if (prime[m] < num)
      l = m + 1;
    else if (prime[m] > num)
      h = m;
    else
      return (m + 1);
  }
  return l;
}

int main(void) {
  setPrime();
  for (int i = 0; i <= 100001; ++i)
    for (int j = 0; j < 100; ++j)
      memo[i][j] = -1;
  int m, p;
  while (cin >> m >> p, m != -1 || p != -1) {
    if (memo[m][p - 1] != -1) {
      cout << memo[m][p - 1] << endl;
      continue;
    }
    MEMZERO(table);
    int ii = search_index(m);
    int count = 0;
    for (int i = 0; i < 100; ++i)
      for (int j = i; j < 100; ++j) {
        table[count++] = prime[i + ii] + prime[j + ii];
      }
    sort(table, table + count);
    cout << table[p - 1] << endl;
    for (int i = 0; i < 100; ++i)
      memo[m][i] = table[i];
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {

  int hurui[200000] = {0};
  int sosuu[100000];
  int cnt = 0;
  vector<int> retu;
  int N, P;

  for (int i = 2; i <= 200000; i++) {
    if (hurui[i] == 0) {
      sosuu[cnt] = i;
      cnt++;
      for (int j = i; j <= 200000; j += i) {
        hurui[j] = 1;
      }
    }
  }

  while (1) {
    cin >> N >> P;
    if (N == -1 && P == -1)
      break;
    for (int i = 0;; i++) {
      if (sosuu[i] > N) {
        for (int j = 0; j < P; j++) {
          for (int k = j; k < P + 1; k++) {
            retu.push_back(sosuu[i + j] + sosuu[i + k]);
          }
        }
        break;
      }
    }
    sort(retu.begin(), retu.end());
    cout << retu[P - 1] << endl;
    retu.clear();
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {

  int hurui[200000] = {0};
  int sosuu[100000];
  int cnt = 0;
  vector<int> retu;
  int N, P;

  for (int i = 2; i <= 200000; i++) {
    if (hurui[i] == 0) {
      sosuu[cnt] = i;
      cnt++;
      for (int j = i; j <= 200000; j += i) {
        hurui[j] = 1;
      }
    }
  }

  while (1) {
    cin >> N >> P;
    if (N == -1 && P == 1)
      break;
    for (int i = 0;; i++) {
      if (sosuu[i] > N) {
        for (int j = 0; j < P; j++) {
          for (int k = j; k < P + 1; k++) {
            retu.push_back(sosuu[i + j] + sosuu[i + k]);
          }
        }
        break;
      }
    }
    sort(retu.begin(), retu.end());
    cout << retu[P - 1] << endl;
    retu.clear();
  }
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

// 2150.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 110000;
const int MAX_P = 100;

bool prime[MAX_N + 1];
vector<int> t;
void sieve() {
  fill(prime, prime + MAX_N + 1, 1);
  prime[0] = prime[1] = 0;
  for (int i = 2; i <= MAX_N; ++i) {
    if (prime[i]) {
      for (int j = i * 2; j <= MAX_N; j += i) {
        prime[j] = 0;
      }
      t.push_back(i);
    }
  }
}

int M(int n, int p) {
  auto itr = upper_bound(t.begin(), t.end(), n);

  vector<int> sum;
  for (int i = 0; i < p; ++i)
    for (int j = i; j < p; ++j)
      sum.push_back(*(itr + i) + *(itr + j));
  sort(sum.begin(), sum.end());
  return sum[p - 1];
}
int main() {
  sieve();

  int n, p;
  while (cin >> n >> p) {
    if (n == -1 && p == -1)
      break;
    cout << M(n, p) << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

// 2150.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
const int MAX_P = 100;
bool prime[MAX_N + 1];
void sieve() {
  fill(prime, prime + MAX_N + 1, 1);

  prime[0] = prime[1] = 0;
  for (int i = 2; i < MAX_N; ++i)
    if (prime[i])
      for (int j = i * 2; j < MAX_N; j += i)
        prime[j] = 0;
}
int M(int n, int p) {
  vector<int> v;
  int cnt = 0;
  for (int i = n + 1; cnt < 100; ++i) {
    if (prime[i]) {
      v.push_back(i);
      cnt++;
    }
  }

  vector<int> w;
  for (int i = 0; i < MAX_P; ++i)
    for (int j = i; j < MAX_P; ++j)
      w.push_back(v[i] + v[j]);

  sort(w.begin(), w.end());
  return w[p - 1];
}
int main() {
  sieve();

  int n, p;
  while (cin >> n >> p) {
    if (n == -1 && p == -1)
      break;
    cout << M(n, p) << endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e15;
template <typename T> void cmax(T &a, T b) { a = max(a, b); }
template <typename T> void cmin(T &a, T b) { a = min(a, b); }

const int MAX_N = 200005;
bool isPrime[MAX_N];
void eratos() {
  for (int i = 0; i < MAX_N; i++)
    isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for (int i = 0; i < MAX_N; i++) {
    if (isPrime[i]) {
      for (int j = i + i; j < MAX_N; j += i) {
        isPrime[j] = false;
      }
    }
  }
}

signed main() {
  eratos();
  int n, p;
  while (cin >> n >> p) {
    if (n == -1 && p == -1)
      break;
    vector<int> ans;
    int tmpp = 101;
    int s = n + 1;
    while (tmpp) {
      if (isPrime[s]) {
        int tmpq = 101;
        int t = s;
        while (tmpq) {
          if (isPrime[t]) {
            ans.push_back(s + t);
            tmpq--;
          }
          t++;
        }
        tmpp--;
      }
      s++;
    }
    sort(all(ans));

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

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, p;
  while (cin >> n >> p) {
    if (n == -1)
      break;
    int m, sosu[200] = {}, count = 0, judge = 1;
    for (m = n + 1; count <= 200; m++) {
      for (int i = 2; i < m; i++) {
        if (m % i == 0) {
          judge = 0;
        }
      }
      if (judge == 1 && m != 1) {
        sosu[count] = m;
        count++;
      } else
        judge = 1;
    }
    vector<int> sum(200);
    int COUNT = 0;
    for (int i = 0; i < 10; i++) {
      for (int j = i; j < 25; j++) {
        sum[COUNT] = sosu[i] + sosu[j];
        COUNT++;
        if (COUNT == 200)
          break;
      }
      if (COUNT == 200)
        break;
    }
    sort(sum.begin(), sum.end());
    cout << sum[p - 1] << endl;
  }
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MAX 222222
using namespace std;
bool is_prim[MAX];

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

void init(void) {
  for (int i = 0; i < MAX; i++) {
    is_prim[i] = true;
  }
  is_prim[0] = false;
  is_prim[1] = false;
  for (int i = 2; i < MAX; i++) {
    for (int j = i + i; j < MAX; j += i) {
      is_prim[j] = false;
    }
  }
}

int main(void) {
  int N, P;
  init();
  while (cin >> N >> P, !(N == -1 && P == -1)) {
    list<int> seq;
    // int i = 1;
    // while(P > 0){
    //   if(is_prim[i+N]){
    //     seq.insert(seq.end(), i+N);
    //     i++;
    //     P--;
    //   }else{
    //     i++;
    //   }
    // }
    for (int i = 1, cnti = 0;; i++) {
      if (is_prim[i + N]) {
        for (int j = i, cntj = 0;; j++) {
          if (is_prim[j + N]) {
            seq.insert(seq.end(), i + N + j + N);
            if (cntj > P)
              break;
            cntj++;
          }
        }
        if (cnti > P)
          break;
        cnti++;
      }
    }
    seq.sort();
    list<int>::iterator it = seq.begin();
    for (int i = 1; i < P; i++) {
      ++it;
    }
    cout << *it << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define MAX 222222
using namespace std;
bool is_prim[MAX];

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

void init(void) {
  for (int i = 0; i < MAX; i++) {
    is_prim[i] = true;
  }
  is_prim[0] = false;
  is_prim[1] = false;
  for (int i = 2; i < MAX; i++) {
    for (int j = i + i; j < MAX; j += i) {
      is_prim[j] = false;
    }
  }
}

int main(void) {
  int N, P;
  init();
  while (cin >> N >> P, !(N == -1 && P == -1)) {
    list<int> seq;
    // int i = 1;
    // while(P > 0){
    //   if(is_prim[i+N]){
    //     seq.insert(seq.end(), i+N);
    //     i++;
    //     P--;
    //   }else{
    //     i++;
    //   }
    // }
    for (int i = 1;; i++) {
      if (is_prim[i + N]) {
        for (int j = i;; j++) {
          if (is_prim[j + N]) {
            seq.insert(seq.end(), i + N + j + N);
            if (j + N > max(N * 10, P * 10))
              break;
          }
        }
        if (i + N > max(N * 10, P * 10))
          break;
      }
    }
    seq.sort();
    list<int>::iterator it = seq.begin();
    for (int i = 1; i < P; i++) {
      ++it;
    }
    cout << *it << endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
bool prime[300005];

void init(void) {
  for (int i = 0; i < 300005; i++) {
    prime[i] = true;
  }
  prime[0] = false, prime[1] = false;
  for (int i = 2; i < 300005; i++) {
    if (prime[i] == true) {
      for (int k = 2; i * k < 300005; k++) {
        prime[i * k] = false;
      }
    }
  }
}

int main(void) {
  init();
  for (;;) {
    int N, P;
    cin >> N >> P;
    if (N == -1 && P == -1) {
      return 0;
    }
    int p[100];
    int ptr = 0;
    for (int i = N + 1; ptr < 100; i++) {
      if (prime[i]) {
        p[ptr] = i;
        ptr++;
      }
    }
    int sum[50 * 101];
    ptr = 0;
    for (int i = 0; i < 100; i++) {
      for (int j = i; j < 100; j++) {
        sum[ptr] = p[i] + p[j];
        ptr++;
      }
    }
    sort(sum, sum + 5050);
    cout << sum[P - 1] << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool prime[300005];

void init(void) {
  for (int i = 0; i < 300005; i++) {
    prime[i] = true;
  }
  prime[0] = false, prime[1] = false;
  for (int i = 2; i < 300005; i++) {
    if (prime[i] == true) {
      for (int k = 2; i * k < 300005; k++) {
        prime[i * k] = false;
      }
    }
  }
}

int main(void) {
  init();
  for (;;) {
    int N, P;
    cin >> N >> P;
    if (N == -1 && P == -1) {
      return 0;
    }
    int p[100];
    int ptr = 0;
    for (int i = N + 1; ptr < 100; i++) {
      if (prime[i]) {
        p[ptr] = i;
        ptr++;
      }
    }
    int sum[50 * 101];
    ptr = 0;
    for (int i = 0; i < 100; i++) {
      for (int j = i; j < 100; j++) {
        sum[ptr] = p[i] + p[j];
        ptr++;
      }
    }
    for (int i = 0; i < 5050; i++) {
      for (int j = i + 1; j < 5050; j++) {
        if (sum[i] > sum[j]) {
          int temp = sum[i];
          sum[i] = sum[j];
          sum[j] = temp;
        }
      }
    }
    cout << sum[P - 1] << endl;
  }
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int MAX_N = 200000;

// p[i] := i 番目の素数
vector<int> p;
// is_p[i] := i が素数のとき true
bool is_p[MAX_N + 1] = {false, false};

// エラトステネスのふるい
void sieve() {
  for (int i = 2; i <= MAX_N; i++)
    is_p[i] = true;
  for (int i = 2; i * i <= MAX_N; i++) {
    if (is_p[i]) {
      p.push_back(i);
      for (int j = i + i; j <= MAX_N; j += i) {
        is_p[j] = false;
      }
    }
  }
}

// N より大きい任意の2つ素数を足したものの中で P 番目に小さい数値を返す
int M(int N, int P) {
  int pi[100];
  priority_queue<int, vector<int>, greater<int>> q;
  int pn = 0;
  for (int i = N + 1; pn < P; i++) {
    if (is_p[i]) {
      pi[pn++] = i;
    }
  }
  for (int i = 0; i < P; i++) {
    for (int j = i; j < P; j++) {
      q.push(pi[i] + pi[j]);
    }
  }
  int result;
  for (int i = 0; i < P; i++) {
    result = q.top();
    q.pop();
  }
  return result;
}

int main() {
  int n, p;
  sieve();
  while (cin >> n >> p, n != -1) {
    cout << M(n, p) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int MAX_N = 210000;

// p[i] := i 番目の素数
vector<int> p;
// is_p[i] := i が素数のとき true
bool is_p[MAX_N + 1] = {false, false};

// エラトステネスのふるい
void sieve() {
  for (int i = 2; i <= MAX_N; i++)
    is_p[i] = true;
  for (int i = 2; i * i <= MAX_N; i++) {
    if (is_p[i]) {
      p.push_back(i);
      for (int j = i + i; j <= MAX_N; j += i) {
        is_p[j] = false;
      }
    }
  }
}

// N より大きい任意の2つ素数を足したものの中で P 番目に小さい数値を返す
int M(int N, int P) {
  int id;
  for (int i = 0; i < p.size(); i++) {
    if (p[i] > N) {
      id = i;
      break;
    }
  }
  priority_queue<int> q;
  // vector<int> v;
  for (int i = id; i < id + P; i++) {
    for (int j = i; j < id + P; j++) {
      q.push(-p[i] - p[j]);
      // v.push_back( p[i] + p[j] );
    }
  }
  // sort( v.begin() , v.end() );
  // if( P-1 >= v.size() ) return v[v.size()-1];
  // return v[P-1];
  int result;
  for (int i = 0; i < P; i++) {
    result = q.top();
    q.pop();
  }
  return -result;
}

int main() {
  int n, p;
  sieve();
  while (cin >> n >> p, n || p) {
    cout << M(n, p) << endl;
  }
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int s[1000], k[10000], n, m, t, p, i, j;
int main() {
  while (true) {
    t = 0;
    p = 0;
    cin >> n >> m;
    if (n == -1) {
      break;
    }
    while (t < m) {
      n++;
      if (n < 2) {
        goto E;
      }
      for (i = 2; i * i <= n; i++) {
        if (n % i == 0) {
          goto E;
        }
      }
      s[t] = n;
      t++;
    E:;
    }
    for (i = 0; i < m; i++) {
      for (j = i; j < m; j++) {
        k[p] = s[j] + s[i];
        p++;
      }
    }
    sort(k, k + p);
    cout << k[m - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int s[1000], k[10000], n, m, t, p, i, j;
int main() {
  while (true) {
    t = 0;
    p = 0;
    cin >> n >> m;
    if (n == 0) {
      break;
    }
    while (t <= m) {
      if (n < 2) {
        goto E;
      }
      for (i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
          goto E;
        }
      }
      s[t] = n;
      t++;
    E:;
      n++;
    }
    for (i = 0; i < m; i++) {
      for (j = i; j < m; j++) {
        k[p] = s[j] + s[i];
        p++;
      }
    }
    sort(k, k + p);
    cout << k[m] << endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 2000000;

bitset<MAX_N> sieve;
vector<int> primes;
void eratosthenes() {
  sieve[0] = sieve[1] = true;
  for (int i = 2; i < MAX_N; ++i) {
    if (sieve[i]) {
      continue;
    }
    primes.push_back(i);
    for (int j = i + i; j < MAX_N; j += i) {
      sieve[j] = true;
    }
  }
}

int main() {
  eratosthenes();
  while (true) {
    int n, p;
    cin >> n >> p;
    if (n == -1 && p == -1) {
      break;
    }
    int begin = upper_bound(primes.begin(), primes.end(), n) - primes.begin();
    vector<int> pp;
    for (int i = 0; i < 200; ++i) {
      for (int j = 0; j <= i; ++j) {
        pp.push_back(primes[begin + i] + primes[begin + j]);
      }
    }
    sort(pp.begin(), pp.end());
    cout << pp[p - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 2000000;

bitset<MAX_N> sieve;
vector<int> primes;
void eratosthenes() {
  sieve[0] = sieve[1] = true;
  for (int i = 2; i < MAX_N; ++i) {
    if (sieve[i]) {
      continue;
    }
    primes.push_back(i);
    for (int j = i + i; j < MAX_N; j += i) {
      sieve[j] = true;
    }
  }
}

int main() {
  eratosthenes();
  while (true) {
    int n, p;
    cin >> n >> p;
    if (n == 0 && p == 0) {
      break;
    }
    int begin = upper_bound(primes.begin(), primes.end(), n) - primes.begin();
    vector<int> pp;
    for (int i = 0; i < 200; ++i) {
      for (int j = 0; j <= i; ++j) {
        pp.push_back(primes[begin + i] + primes[begin + j]);
      }
    }
    sort(pp.begin(), pp.end());
    cout << pp[p - 1] << endl;
  }
  return 0;
}
Problem p01268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

bool e[150010];

void erato() {
  fill(e, e + 150010, true);
  e[0] = e[1] = false;

  for (int i = 0; i < 150010; i++) {
    if (!e[i])
      continue;
    for (int j = i + i; j < 150010; j += i) {
      e[j] = false;
    }
  }
}

int main() {
  erato();

  int n, p;
  int primeNumbers[100];
  int addPrimeNumbers[100010];
  while (cin >> n >> p, n != -1 && p != -1) {
    while (1) {
      if (e[++n])
        break;
    }
    for (int i = 0; i < p; n++) {
      if (e[n]) {
        primeNumbers[i] = n;
        i++;
      }
    }
    int countPrimeNumbers = 0;
    for (int i = 0; i < p; i++) {
      for (int j = i; j < p; j++) {
        addPrimeNumbers[countPrimeNumbers] = primeNumbers[i] + primeNumbers[j];
        countPrimeNumbers++;
      }
    }
    sort(addPrimeNumbers, addPrimeNumbers + countPrimeNumbers);

    cout << addPrimeNumbers[p - 1] << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

bool e[100010];

void erato() {
  fill(e, e + 150010, true);
  e[0] = e[1] = false;

  for (int i = 0; i < 150010; i++) {
    if (!e[i])
      continue;
    for (int j = i + i; j < 150010; j += i) {
      e[j] = false;
    }
  }
}

int main() {
  erato();

  int n, p;
  int primeNumbers[100];
  int addPrimeNumbers[100010];
  while (cin >> n >> p, n != -1 && p != -1) {
    while (1) {
      if (e[++n])
        break;
    }
    for (int i = 0; i < p; n++) {
      if (e[n]) {
        primeNumbers[i] = n;
        i++;
      }
    }
    int countPrimeNumbers = 0;
    for (int i = 0; i < p; i++) {
      for (int j = i; j < p; j++) {
        addPrimeNumbers[countPrimeNumbers] = primeNumbers[i] + primeNumbers[j];
        countPrimeNumbers++;
      }
    }
    sort(addPrimeNumbers, addPrimeNumbers + countPrimeNumbers);

    cout << addPrimeNumbers[p - 1] << endl;
  }
}