prithivMLmods/Coder-Stat huggingface dataset:
Problem p01267 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);

int main() {
  int n, a, b, c, x;
  int y[101];
  while (cin >> n >> a >> b >> c >> x && n) {
    for (int i = 0; i < n; i++) {
      cin >> y[i];
    }
    int cont = 0, icont = 0;
    if (x == y[0])
      icont++;
    while (cont <= 10000 && icont < n) {
      x = (a * x + b) % c;
      if (x == y[icont]) {
        icont++;
      }
      cont++;
    }
    if (cont > 10000) {
      cout << -1 << endl;
    } else
      cout << cont << endl;
  }
  system("pause");
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);

int main() {
  int n, a, b, c, x;
  int y[101];
  while (cin >> n >> a >> b >> c >> x && n) {
    for (int i = 0; i < n; i++) {
      cin >> y[i];
    }
    int cont = 0, icont = 0;
    if (x == y[0])
      icont++;
    while (cont <= 10000 && icont < n) {
      x = (a * x + b) % c;
      if (x == y[icont]) {
        icont++;
      }
      cont++;
    }
    if (cont > 10000) {
      cout << -1 << endl;
    } else
      cout << cont << endl;
  }
}
Problem p01267 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
using namespace std;

int N, A, B, C, X, Y[200];

int main(void) {
  while (cin >> N >> A >> B >> C >> X) {
    REP(i, 0, N) cin >> Y[i];

    int ans = 0, k = 0;
    while (ans <= 10000) {
      if (X == Y[k]) {
        k++;
        if (k == N)
          break;
      }
      X = (((A % C) * X) % C + B) % C;
      ans++;
    }
    cout << (ans <= 10000 ? ans : -1) << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
using namespace std;

int N, A, B, C, X, Y[200];

int main(void) {
  while (cin >> N >> A >> B >> C >> X, N) {
    REP(i, 0, N) cin >> Y[i];

    int ans = 0, k = 0;
    while (ans <= 10000) {
      if (X == Y[k]) {
        k++;
        if (k == N)
          break;
      }
      X = (((A % C) * X) % C + B) % C;
      ans++;
    }
    cout << (ans <= 10000 ? ans : -1) << endl;
  }
  return 0;
}
Problem p01267 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  int N, A, B, C, X;
  while (cin >> N >> A >> B >> C >> X, N || A || B || C || X) {
    int Y[100];
    for (int i = 0; i < N; ++i) {
      cin >> Y[i];
    }
    int need[10001] = {0};
    int cnt = 0;
    for (int i = 0; i < N; ++i) {
      if (!need[Y[i]])
        ++cnt;
      ++need[Y[i]];
    }

    int ans = -1;
    int pos = 0;
    for (int t = 0; t < 10000 * 1000; ++t) {
      if (Y[pos] == X) {
        if (++pos == N) {
          ans = t;
          break;
        }
      }
      X = (A * X + B) % C;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int N, A, B, C, X;
  while (cin >> N >> A >> B >> C >> X, N || A || B || C || X) {
    int Y[100];
    for (int i = 0; i < N; ++i) {
      cin >> Y[i];
    }
    int need[10001] = {0};
    int cnt = 0;
    for (int i = 0; i < N; ++i) {
      if (!need[Y[i]])
        ++cnt;
      ++need[Y[i]];
    }

    int ans = -1;
    int pos = 0;
    for (int t = 0; t <= 10000; ++t) {
      if (Y[pos] == X) {
        if (++pos == N) {
          ans = t;
          break;
        }
      }
      X = (A * X + B) % C;
    }
    cout << ans << endl;
  }
}
Problem p01268 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool is_prime(int n, int k = 2) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 1);
}

int main() {
  vector<int> prime;

  for (int i = 2; i < 150000; i++) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    cin >> n >> m;

    if (n == 0 && m == 0) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;

    for (int i = ptr; i < ptr + m; i++) {
      for (int j = i; j < ptr + m; j++) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool is_prime(int n, int k = 2) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 1);
}

int main() {
  vector<int> prime;

  for (int i = 2; i < 150000; i++) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    cin >> n >> m;

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;

    for (int i = ptr; i < ptr + m; i++) {
      for (int j = i; j < ptr + m; j++) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

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

  return 0;
}
Problem p01268 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p01268 (C++)

Buggy submission (status: 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();
  }
}

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 (status: Runtime Error):
#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;
  }
}

Fixed submission:
#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;
  }
}
Problem p01268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;

int prime[MAX_PRIME + 1];
bool isPrime[MAX_PRIME + 1];
int p;

void erats() {
  // primeツδ環スツトツづーツ渉可甘コツ可サ
  fill(isPrime, isPrime + MAX_PRIME, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i <= MAX_PRIME; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;
    // ツづ督づゥツつ「ツづ可つゥツつッツづゥ
    for (int j = 2 * i; j <= MAX_PRIME; j += i) {
      isPrime[j] = false;
    }
  }
}

int main() {

  int N, P;
  erats();
  while (cin >> N >> P && !(N == -1 && P == -1)) {
    int *it = upper_bound(prime, prime + p, N);
    int prv = it - prime;
    int init = prv;
    vector<int> v;
    for (int i = init; i <= init + 1000; i++) {
      for (int j = i; j <= init + 1000; j++) {
        v.push_back(prime[i] + prime[j]);
      }
    }
    sort(v.begin(), v.end());
    cout << v[P - 1] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;

int prime[MAX_PRIME + 1];
bool isPrime[MAX_PRIME + 1];
int p;

void erats() {
  // primeツδ環スツトツづーツ渉可甘コツ可サ
  fill(isPrime, isPrime + MAX_PRIME, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i <= MAX_PRIME; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;
    // ツづ督づゥツつ「ツづ可つゥツつッツづゥ
    for (int j = 2 * i; j <= MAX_PRIME; j += i) {
      isPrime[j] = false;
    }
  }
}

int main() {

  int N, P;
  erats();
  while (cin >> N >> P && !(N == -1 && P == -1)) {
    int *it = upper_bound(prime, prime + p, N);
    int prv = it - prime;
    int init = prv;
    vector<int> v;
    for (int i = init; i <= init + 100; i++) {
      for (int j = i; j <= init + 100; j++) {
        v.push_back(prime[i] + prime[j]);
      }
    }
    sort(v.begin(), v.end());
    cout << v[P - 1] << endl;
  }

  return 0;
}
Problem p01268 (C++)

Buggy submission (status: 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) {
    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;
}

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 >= 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;
}
Problem p01268 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

bool f[1000002];

int main() {
  for (int i = 3; i < 1000001; i += 2) {
    f[i] = true;
    for (int j = 3; j * j <= i; j += 2)
      if (i % j == 0)
        f[i] = false;
  }
  f[2] = true;
  int n, p;
  while (1) {
    cin >> n >> p;
    if (n == -1 && n == p)
      break;

    int r = 0;
    int q[10001] = {};
    int x = 0;
    int y = n;
    while (1) {
      if (f[y] == true)
        x++;
      if (x == p)
        break;
      y++;
    }
    n++;
    vector<int> anslist;
    for (int i = n; i <= y; i++)
      for (int j = i; j <= y; j++) {
        if (f[i] == true && f[j] == true) {
          anslist.push_back(i + j);

          // cout<<i<<" "<<j<<endl;
        }
      }
    sort(anslist.begin(), anslist.end());
    /*
                    for(int i=n;i<=y;i++)
                            for(int j=i;j<=y;j++)
                            {
                                    if(f[i]==true&&f[j]==true){
                                    q[r]=i+j;r++;
                                    }
                            }

                    /*
                    sort(q,q+r);
                    for(int i=0;i<r;i++)
                    cout<<q[i]<<endl;

                    cout<<q[p-1]<<endl;*/
    cout << anslist[p - 1] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

bool f[1000002];

int main() {
  for (int i = 3; i < 1000001; i += 2) {
    f[i] = true;
    for (int j = 3; j * j <= i; j += 2)
      if (i % j == 0)
        f[i] = false;
  }
  f[2] = true;
  int n, p;
  while (1) {
    cin >> n >> p;
    if (n == -1 && n == p)
      break;

    int r = 0;
    int q[10001] = {};
    int x = 0;
    int y = n + 1;
    while (1) {
      if (f[y] == true)
        x++;
      if (x == p)
        break;
      y++;
    }
    n++;
    vector<int> anslist;
    for (int i = n; i <= y; i++)
      for (int j = i; j <= y; j++) {
        if (f[i] == true && f[j] == true) {
          anslist.push_back(i + j);

          // cout<<i<<" "<<j<<endl;
        }
      }
    sort(anslist.begin(), anslist.end());
    /*
                    for(int i=n;i<=y;i++)
                            for(int j=i;j<=y;j++)
                            {
                                    if(f[i]==true&&f[j]==true){
                                    q[r]=i+j;r++;
                                    }
                            }

                    /*
                    sort(q,q+r);
                    for(int i=0;i<r;i++)
                    cout<<q[i]<<endl;

                    cout<<q[p-1]<<endl;*/
    cout << anslist[p - 1] << endl;
  }

  return 0;
}
Problem p01268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <stdint.h>

typedef uint32_t u32;
typedef uint64_t u64;

class Solver {
public:
  Solver();
  void solve(int N, int P);

private:
  bool isprime[200000];
};

Solver::Solver() {
  for (int i = 0; i < 200000; ++i) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for (int i = 2; i < 2000; ++i) {
    if (isprime[i]) {
      for (int j = i * 2; j < 200000; j += i) {
        isprime[j] = false;
      }
    }
  }
}

void Solver::solve(int N, int P) {
  std::vector<int> can;
  for (int i = N + 1; i < std::min(N + P * 4 + 1000, 200000); ++i) {
    if (not isprime[i])
      continue;
    for (int j = i; j < std::min(N + P * 4 + 1000, 200000); ++j) {
      if (not isprime[j])
        continue;
      can.push_back(i + j);
    }
  }
  std::sort(std::begin(can), std::end(can));
  std::cout << can[P - 1] << std::endl;
}

int main() {

  Solver solver;

  for (;;) {
    int N, P;
    std::cin >> N >> P;
    if (N == 0 and P == 0)
      break;

    solver.solve(N, P);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdint.h>

typedef uint32_t u32;
typedef uint64_t u64;

class Solver {
public:
  Solver();
  void solve(int N, int P);

private:
  bool isprime[200000];
};

Solver::Solver() {
  for (int i = 0; i < 200000; ++i) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for (int i = 2; i < 2000; ++i) {
    if (isprime[i]) {
      for (int j = i * 2; j < 200000; j += i) {
        isprime[j] = false;
      }
    }
  }
}

void Solver::solve(int N, int P) {
  std::vector<int> can;
  for (int i = N + 1; i < std::min(N + P * 4 + 1000, 200000); ++i) {
    if (not isprime[i])
      continue;
    for (int j = i; j < std::min(N + P * 4 + 1000, 200000); ++j) {
      if (not isprime[j])
        continue;
      can.push_back(i + j);
    }
  }
  std::sort(std::begin(can), std::end(can));
  std::cout << can[P - 1] << std::endl;
}

int main() {

  Solver solver;

  for (;;) {
    int N, P;
    std::cin >> N >> P;
    if (N == -1 and P == -1)
      break;

    solver.solve(N, P);
  }

  return 0;
}
Problem p01268 (C++)

Buggy submission (status: Time Limit Exceeded):
// Name: Matsuzaki Number
// Level: 2
// Category: 素数
// Note:

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

using namespace std;

const int MAX_PRIME = 200000;
vector<int> primes;
bool is_prime[MAX_PRIME + 1];

void init() {
  fill_n(is_prime, MAX_PRIME + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= MAX_PRIME; ++i) {
    if (is_prime[i]) {
      primes.push_back(i);
      for (long long j = (long long)i * i; j <= MAX_PRIME; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

bool solve() {
  int N, P;
  cin >> N >> P;
  if (!N && !P)
    return false;

  vector<int>::iterator first = upper_bound(primes.begin(), primes.end(), N);
  vector<int> nums;
  const int start = distance(primes.begin(), first);
  for (int i = start; i < start + 100; ++i) {
    for (int j = i; j < start + 100; ++j) {
      nums.push_back(primes[i] + primes[j]);
    }
  }
  sort(nums.begin(), nums.end());
  cout << nums[P - 1] << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  init();
  for (int case_num = 1; solve(); ++case_num)
    ;
  return 0;
}

Fixed submission:
// Name: Matsuzaki Number
// Level: 2
// Category: 素数
// Note:

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

using namespace std;

const int MAX_PRIME = 200000;
vector<int> primes;
bool is_prime[MAX_PRIME + 1];

void init() {
  fill_n(is_prime, MAX_PRIME + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= MAX_PRIME; ++i) {
    if (is_prime[i]) {
      primes.push_back(i);
      for (long long j = (long long)i * i; j <= MAX_PRIME; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

bool solve() {
  int N, P;
  cin >> N >> P;
  if (N == -1 && P == -1)
    return false;

  vector<int>::iterator first = upper_bound(primes.begin(), primes.end(), N);
  vector<int> nums;
  const int start = distance(primes.begin(), first);
  for (int i = start; i < start + 100; ++i) {
    for (int j = i; j < start + 100; ++j) {
      nums.push_back(primes[i] + primes[j]);
    }
  }
  sort(nums.begin(), nums.end());
  cout << nums[P - 1] << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  init();
  for (int case_num = 1; solve(); ++case_num)
    ;
  return 0;
}
Problem p01268 (C++)

Buggy submission (status: 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 + 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;
}

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 + 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;
}
Problem p01268 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool is_prime(int n, int k = 3) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 2);
}

int main() {
  vector<int> prime = {2};

  for (int i = 3; i < 101300; i += 2) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;
    int r = min(22, m / 2) - 1;

    for (int i = ptr + r; i >= ptr; i--) {
      for (int j = ptr + r; j >= i; j--) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    printf("%d\n", c[m - 1]);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool is_prime(int n, int k = 3) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 2);
}

int main() {
  vector<int> prime = {2};

  for (int i = 3; i < 101300; i += 2) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;
    int r = min(22, max(m / 2, 12)) - 1;

    for (int i = ptr + r; i >= ptr; i--) {
      for (int j = ptr + r; j >= i; j--) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    printf("%d\n", c[m - 1]);
  }

  return 0;
}
Problem p01268 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p01268 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool is_prime(int n, int k = 3) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 2);
}

int main() {
  vector<int> prime = {2};

  for (int i = 3; i < 101300; i += 2) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;

    for (int i = ptr; i < ptr + m / 2; i++) {
      for (int j = i; j < ptr + m / 2; j++) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    printf("%d\n", c[m - 1]);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool is_prime(int n, int k = 3) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return is_prime(n, k + 2);
}

int main() {
  vector<int> prime = {2};

  for (int i = 3; i < 101300; i += 2) {
    if (is_prime(i)) {
      prime.push_back(i);
    }
  }

  int n, m;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == -1 && m == -1) {
      break;
    }

    int ptr =
        distance(prime.begin(), lower_bound(prime.begin(), prime.end(), n + 1));

    vector<int> c;

    for (int i = ptr + m - 1; i >= ptr; i--) {
      for (int j = ptr + m - 1; j >= i; j--) {
        c.push_back(prime[i] + prime[j]);
      }
    }

    sort(c.begin(), c.end());

    printf("%d\n", c[m - 1]);
  }

  return 0;
}
Problem p01268 (C++)

Buggy submission (status: 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 == 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;
}

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 == -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;
}
Problem p01268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define pb push_back
#define all(v) v.begin(), v.end()
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define MAXBOOL 1000000
using namespace std;

int main() {
  int n, p;
  vector<int> prime;
  bool check[MAXBOOL];
  while (cin >> n >> p, n != -1 || p != -1) {
    prime.clear();
    fill(check, check + MAXBOOL, true);
    check[0] = check[1] = false;
    loop(i, 2, sqrt(MAXBOOL)) {
      for (int j = i * 2; j < MAXBOOL; j += i) {
        check[j] = false;
      }
    }
    int cnt = 0;
    while (prime.size() <= 200) {
      if (check[n + 1 + cnt])
        prime.pb(n + 1 + cnt);
      cnt++;
    }
    vector<int> ans;
    rep(i, prime.size()) {
      loop(j, i, prime.size()) { ans.pb(prime[i] + prime[j]); }
    }
    sort(all(ans));
    cout << ans[p - 1] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define pb push_back
#define all(v) v.begin(), v.end()
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define MAXBOOL 500000
using namespace std;

int main() {
  int n, p;
  vector<int> prime;
  bool check[MAXBOOL];
  while (cin >> n >> p, n != -1 || p != -1) {
    prime.clear();
    fill(check, check + MAXBOOL, true);
    check[0] = check[1] = false;
    loop(i, 2, sqrt(MAXBOOL)) {
      for (int j = i * 2; j < MAXBOOL; j += i) {
        check[j] = false;
      }
    }
    int cnt = 0;
    while (prime.size() <= 200) {
      if (check[n + 1 + cnt])
        prime.pb(n + 1 + cnt);
      cnt++;
    }
    vector<int> ans;
    rep(i, prime.size()) {
      loop(j, i, prime.size()) { ans.pb(prime[i] + prime[j]); }
    }
    sort(all(ans));
    cout << ans[p - 1] << endl;
  }
  return 0;
}
Problem p01269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef struct {
  int to;
  int D;
  int E;
} Edge;

class State {
public:
  int now;
  int money;
  int damaged;

  State(int now, int money, int damaged) {
    this->now = now;
    this->money = money;
    this->damaged = damaged;
  }
};

const int INF = 999999;

vector<Edge> G[101];

int d[101][101];

int N, M, L;

int toV(int n, int l) { return n * L + l; }

int main() {
  while (cin >> N >> M >> L, N + M + L != 0) {
    queue<State> q;
    for (int i = 0; i < 101; i++) {
      G[i].clear();
      for (int j = 0; j < 101; j++) {
        d[i][j] = INF;
      }
    }
    for (int i = 0; i < M; i++) {
      int A, B, D, E;
      cin >> A >> B >> D >> E;
      Edge e{B, D, E};
      Edge e2{A, D, E};
      G[A].push_back(e);
      G[B].push_back(e2);
    }
    q.push(State(1, L, 0));
    while (!q.empty()) {
      State s = q.front();
      q.pop();
      if (s.damaged > d[s.now][s.money])
        continue;
      d[s.now][s.money] = s.damaged;

      for (Edge e : G[s.now]) {
        q.push(State(e.to, s.money, s.damaged + e.E));
        if (s.money - e.D >= 0) {
          q.push(State(e.to, s.money - e.D, s.damaged));
        }
      }
    }
    int m = INF;
    for (int i = 0; i <= L; i++) {
      m = min(m, d[N][i]);
    }
    cout << m << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef struct {
  int to;
  int D;
  int E;
} Edge;

class State {
public:
  int now;
  int money;
  int damaged;

  State(int now, int money, int damaged) {
    this->now = now;
    this->money = money;
    this->damaged = damaged;
  }
};

const int INF = 999999;

vector<Edge> G[101];

int d[101][101];

int N, M, L;

int toV(int n, int l) { return n * L + l; }

int main() {
  while (cin >> N >> M >> L, N + M + L != 0) {
    queue<State> q;
    for (int i = 0; i < 101; i++) {
      G[i].clear();
      for (int j = 0; j < 101; j++) {
        d[i][j] = INF;
      }
    }
    for (int i = 0; i < M; i++) {
      int A, B, D, E;
      cin >> A >> B >> D >> E;
      Edge e{B, D, E};
      Edge e2{A, D, E};
      G[A].push_back(e);
      G[B].push_back(e2);
    }
    q.push(State(1, L, 0));
    while (!q.empty()) {
      State s = q.front();
      q.pop();
      if (s.damaged >= d[s.now][s.money])
        continue;
      d[s.now][s.money] = s.damaged;

      for (Edge e : G[s.now]) {
        q.push(State(e.to, s.money, s.damaged + e.E));
        if (s.money - e.D >= 0) {
          q.push(State(e.to, s.money - e.D, s.damaged));
        }
      }
    }
    int m = INF;
    for (int i = 0; i <= L; i++) {
      m = min(m, d[N][i]);
    }
    cout << m << endl;
  }
  return 0;
}
Problem p01269 (C++)

Buggy submission (status: Runtime Error):
#include <queue>
#include <stdio.h>
#include <string.h>
struct S {
  int i, c, m;
  S(int i, int c, int m) : i(i), c(c), m(m) {}
  bool operator<(const S &r) const { return c > r.c; }
};
#define C(d) memset(d, X, sizeof(d))
int main() {
  int N, M, L, A, B, D, E, X = 0x7f7f7f7f;
  int d[100][100], e[100][100], x[100][100], i, j;
  while (scanf("%d%d%d", &N, &M, &L), N) {
    C(d), C(e), C(x);
    while (M--) {
      scanf("%d%d%d%d", &A, &B, &D, &E);
      A--, B--;
      d[A][B] = d[B][A] = D;
      e[A][B] = e[B][A] = E;
    }
    std::priority_queue<S> q;
    q.push(S(0, 0, L));
    for (;;) {
      S p(q.top());
      q.pop();
      if (x[p.i][p.m] <= p.c)
        continue;
      if (p.i == N - 1) {
        printf("%d\n", p.c);
        break;
      }
      x[p.i][p.m] = p.c;
      for (i = 0; i < N; ++i) {
        if (d[p.i][i] == X)
          continue;
        if (d[p.i][i] <= p.m)
          q.push(S(i, p.c, p.m - d[p.i][i]));
        q.push(S(i, p.c + e[p.i][i], p.m));
      }
    }
  }
  return 0;
}

Fixed submission:
#include <queue>
#include <stdio.h>
#include <string.h>
struct S {
  int i, c, m;
  S(int i, int c, int m) : i(i), c(c), m(m) {}
  bool operator<(const S &r) const { return c > r.c; }
};
#define C(d) memset(d, X, sizeof(d))
int main() {
  int N, M, L, A, B, D, E, X = 0x7f7f7f7f;
  int d[100][100], e[100][100], x[100][101], i;
  while (scanf("%d%d%d", &N, &M, &L), N) {
    C(d), C(e), C(x);
    while (M--) {
      scanf("%d%d%d%d", &A, &B, &D, &E);
      A--, B--;
      d[A][B] = d[B][A] = D;
      e[A][B] = e[B][A] = E;
    }
    std::priority_queue<S> q;
    q.push(S(0, 0, L));
    for (;;) {
      S p(q.top());
      q.pop();
      if (x[p.i][p.m] <= p.c)
        continue;
      if (p.i == N - 1) {
        printf("%d\n", p.c);
        break;
      }
      x[p.i][p.m] = p.c;
      for (i = 0; i < N; ++i) {
        if (d[p.i][i] == X)
          continue;
        if (d[p.i][i] <= p.m)
          q.push(S(i, p.c, p.m - d[p.i][i]));
        q.push(S(i, p.c + e[p.i][i], p.m));
      }
    }
  }
  return 0;
}
Problem p01269 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define fr first
#define sc second
#define INF (1 << 25)
using namespace std;

typedef pair<int, int> P;
typedef pair<P, int> iP;
struct edge {
  int to, dis, enemy;
  edge() {}
  edge(int to, int dis, int enemy) : to(to), dis(dis), enemy(enemy) {}
};

int n, m, money;
int costed[128][128];
vector<vector<edge>> info(128);

void init() {
  for (int i = 0; i < 128; i++) {
    for (int j = 0; j < 128; j++)
      costed[i][j] = INF;
  }
  info.resize(0);
  info.resize(128);
}

void add_info() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  info[a].push_back(edge(b, c, d));
  info[b].push_back(edge(a, c, d));
}

int Dijkstra() {
  priority_queue<iP, vector<iP>, greater<iP>> que;
  que.push(iP(P(0, 1), money));
  costed[money][1] = 0;
  while (!que.empty()) {
    iP p = que.top();
    que.pop();
    int now = p.fr.sc, att = p.fr.fr, mo = p.sc;
    if (now == n)
      return att;
    for (int i = 0; i < info[now].size(); i++) {
      edge e = info[now][i];
      if (e.enemy + att < costed[mo][e.to]) {
        que.push(iP(P(att + e.enemy, e.to), mo));
        costed[mo][e.to] = e.enemy + att;
      }
      if (mo >= e.dis) {
        que.push(iP(P(att, e.to), mo - e.dis));
        costed[mo - e.dis][e.to] = att;
      }
    }
  }
}

int main() {
  while (cin >> n >> m >> money, n || m || money) {
    init();
    while (m--)
      add_info();
    cout << Dijkstra() << endl;
  }
}

Fixed submission:
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define fr first
#define sc second
#define INF (1 << 25)
using namespace std;

typedef pair<int, int> P;
typedef pair<P, int> iP;
struct edge {
  int to, dis, enemy;
  edge() {}
  edge(int to, int dis, int enemy) : to(to), dis(dis), enemy(enemy) {}
};

int n, m, money;
int costed[128][128];
vector<vector<edge>> info(128);

void init() {
  for (int i = 0; i < 128; i++) {
    for (int j = 0; j < 128; j++)
      costed[i][j] = INF;
  }
  info.resize(0);
  info.resize(128);
}

void add_info() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  info[a].push_back(edge(b, c, d));
  info[b].push_back(edge(a, c, d));
}

int Dijkstra() {
  priority_queue<iP, vector<iP>, greater<iP>> que;
  que.push(iP(P(0, 1), money));
  costed[money][1] = 0;
  while (!que.empty()) {
    iP p = que.top();
    que.pop();
    int now = p.fr.sc, att = p.fr.fr, mo = p.sc;
    if (now == n)
      return att;
    for (int i = 0; i < info[now].size(); i++) {
      edge e = info[now][i];
      if (e.enemy + att < costed[mo][e.to]) {
        que.push(iP(P(att + e.enemy, e.to), mo));
        costed[mo][e.to] = e.enemy + att;
      }
      if (mo >= e.dis && att < costed[mo - e.dis][e.to]) {
        que.push(iP(P(att, e.to), mo - e.dis));
        costed[mo - e.dis][e.to] = att;
      }
    }
  }
}

int main() {
  while (cin >> n >> m >> money, n || m || money) {
    init();
    while (m--)
      add_info();
    cout << Dijkstra() << endl;
  }
}
Problem p01269 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int num[101][101];
int dist[101][101];
#define INF 100000000

class state {
public:
  int now, money, cost;
  state(int now, int money, int cost) : now(now), cost(cost), money(money) {}
  bool operator<(const state &a) { return cost > a.cost; }
};

bool operator<(state a, state b) { return a.cost > b.cost; }

int main() {
  int n, m, l;
  while (cin >> n >> m >> l && n != 0) {

    int C[101][101];
    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++)
        num[i][j] = dist[i][j] = C[i][j] = INF;

    for (int i = 0; i < m; i++) {
      int in, out, d, e;
      cin >> in >> out >> d >> e;
      num[in][out] = num[out][in] = e;
      dist[in][out] = dist[out][in] = d;
    }

    priority_queue<state> Q;
    Q.push(state(1, l, 0));
    while (!Q.empty()) {
      state t = Q.top();
      Q.pop();
      if (C[t.now][t.money] <= t.cost)
        continue;
      if (t.now == n) {
        cout << t.cost << endl;
        break;
      }
      for (int i = 1; i <= n; i++) {
        if (dist[t.now][i] < INF) {
          if (t.money - dist[t.now][i] >= 0)
            Q.push(state(i, t.money - dist[t.now][i], t.cost));
          Q.push(state(i, t.money, t.cost + num[t.now][i]));
        }
      }
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int num[101][101];
int dist[101][101];
#define INF 100000000

class state {
public:
  int now, money, cost;
  state(int now, int money, int cost) : now(now), cost(cost), money(money) {}
  bool operator<(const state &a) { return cost > a.cost; }
};

bool operator<(state a, state b) { return a.cost > b.cost; }

int main() {
  int n, m, l;
  while (cin >> n >> m >> l && n != 0) {

    int C[101][101];
    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++)
        num[i][j] = dist[i][j] = C[i][j] = INF;

    for (int i = 0; i < m; i++) {
      int in, out, d, e;
      cin >> in >> out >> d >> e;
      num[in][out] = num[out][in] = e;
      dist[in][out] = dist[out][in] = d;
    }

    priority_queue<state> Q;
    Q.push(state(1, l, 0));
    while (!Q.empty()) {
      state t = Q.top();
      Q.pop();
      if (C[t.now][t.money] <= t.cost)
        continue;
      C[t.now][t.money] = t.cost;
      if (t.now == n) {
        cout << t.cost << endl;
        break;
      }
      for (int i = 1; i <= n; i++) {
        if (dist[t.now][i] < INF) {
          if (t.money - dist[t.now][i] >= 0)
            Q.push(state(i, t.money - dist[t.now][i], t.cost));
          Q.push(state(i, t.money, t.cost + num[t.now][i]));
        }
      }
    }
  }
  return 0;
}
Problem p01270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#define rep(i, n) for (int i = 0; i < n; i++)
#define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define mp make_pair
using namespace std;
typedef map<pair<int, int>, int> S;
int main() {
  int n, t1, t2;
  char c;
  while (cin >> n, n) {
    S s;
    rep(i, n) {
      cin >> c;
      if (c == 'W') {
        cin >> t1 >> t2;
        int p = 0, q;
        if (s.empty())
          s.insert(mp(mp(0, t2), t1)), t2 = 0;
        else {
          foreach (j, s) {
            if (j->first.first > p) {
              q = min(p + t2, j->first.first);
              s.insert(mp(mp(p, q), t1));
              t2 -= q - p;
              p = q;
              j--;
            } else
              p = j->first.second;
          }
          if (t2 > 0)
            s.insert(mp(mp(p, p + t2), t1));
        }
      } else if (c == 'D') {
        cin >> t1;
        foreach (j, s)
          if (j->second == t1)
            s.erase(j);
      } else {
        cin >> t1;
        foreach (j, s)
          if (j->first.first <= t1 && t1 < j->first.second) {
            cout << j->second << endl;
            goto END;
          }
        cout << -1 << endl;
      END:;
      }
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#define rep(i, n) for (int i = 0; i < n; i++)
#define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define mp make_pair
using namespace std;
typedef map<pair<int, int>, int> S;
int main() {
  int n, t1, t2;
  char c;
  while (cin >> n, n) {
    S s;
    rep(i, n) {
      cin >> c;
      if (c == 'W') {
        cin >> t1 >> t2;
        int p = 0, q;
        if (s.empty())
          s.insert(mp(mp(0, t2), t1)), t2 = 0;
        else {
          foreach (j, s) {
            if (j->first.first > p) {
              q = min(p + t2, j->first.first);
              s.insert(mp(mp(p, q), t1));
              t2 -= q - p;
              p = q;
              j--;
            } else
              p = j->first.second;
            if (t2 == 0)
              break;
          }
          if (t2 > 0)
            s.insert(mp(mp(p, p + t2), t1));
        }
      } else if (c == 'D') {
        cin >> t1;
        foreach (j, s)
          if (j->second == t1)
            s.erase(j);
      } else {
        cin >> t1;
        foreach (j, s)
          if (j->first.first <= t1 && t1 < j->first.second) {
            cout << j->second << endl;
            goto END;
          }
        cout << -1 << endl;
      END:;
      }
    }
    cout << endl;
  }
  return 0;
}