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

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int MAX = 100000;
int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    int a, b, cost, timer;
    int costs[m][m];
    int timers[m][m];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        costs[i][j] = MAX;
        timers[i][j] = MAX;
      }
      costs[i][i] = 0;
      timers[i][i] = 0;
    }

    for (int i = 0; i < n; i++) {
      cin >> a >> b >> cost >> timer;
      costs[a - 1][b - 1] = cost;
      timers[a - 1][b - 1] = timer;
      costs[b - 1][a - 1] = cost;
      timers[b - 1][a - 1] = timer;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        for (int k = 0; k < m; k++) {
          if (costs[j][k] > (costs[j][i] + costs[i][k])) {
            costs[j][k] = costs[j][i] + costs[i][k];
            costs[k][j] = costs[j][i] + costs[i][k];
          }
          if (timers[j][k] > (timers[j][i] + timers[i][k])) {
            timers[j][k] = timers[j][i] + timers[i][k];
            timers[k][j] = timers[j][i] + timers[i][k];
          }
        }
      }
    }
    int k;
    int p;
    int q;
    bool r;
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> p >> q >> r;
      if (r) {
        cout << timers[p - 1][q - 1] << endl;
      } else if (!r) {
        cout << costs[p - 1][q - 1] << endl;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int MAX = 100000;
int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      break;
    int a, b, cost, timer;
    int costs[m][m];
    int timers[m][m];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        costs[i][j] = MAX;
        timers[i][j] = MAX;
      }
      costs[i][i] = 0;
      timers[i][i] = 0;
    }

    for (int i = 0; i < n; i++) {
      cin >> a >> b >> cost >> timer;
      costs[a - 1][b - 1] = cost;
      timers[a - 1][b - 1] = timer;
      costs[b - 1][a - 1] = cost;
      timers[b - 1][a - 1] = timer;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        for (int k = 0; k < m; k++) {
          if (costs[j][k] > (costs[j][i] + costs[i][k])) {
            costs[j][k] = costs[j][i] + costs[i][k];
            costs[k][j] = costs[j][i] + costs[i][k];
          }
          if (timers[j][k] > (timers[j][i] + timers[i][k])) {
            timers[j][k] = timers[j][i] + timers[i][k];
            timers[k][j] = timers[j][i] + timers[i][k];
          }
        }
      }
    }
    int k;
    int p;
    int q;
    bool r;
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> p >> q >> r;
      if (r) {
        cout << timers[p - 1][q - 1] << endl;
      } else if (!r) {
        cout << costs[p - 1][q - 1] << endl;
      }
    }
  }
  return 0;
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define INF (1145141919)
using namespace std;

int n;

int from[3000];
int to[3000];
int cost[3000][2];

int d[100];
int r;
void Search(int s) {
  for (int i = 0; i < 100; i++) {
    d[i] = INF;
  }
  d[s] = 0;

  while (true) {
    bool update = false;

    for (int i = 0; i < n * 2; i++) {
      if (d[from[i]] != INF && d[to[i]] > d[from[i]] + cost[i][r]) {
        d[to[i]] = d[from[i]] + cost[i][r];
        update = true;
      }
    }

    if (!update) {
      break;
    }
  }
}

int main() {
  int m;
  while (scanf("%d %d", &n, &m), n + m) {
    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      scanf("%d %d %d %d", &a, &b, &c, &t);
      a--;
      b--;

      from[i * 2] = a;
      to[i * 2] = b;
      cost[i * 2][0] = c;
      cost[i * 2][1] = t;

      from[i * 2 + 1] = b;
      to[i * 2 + 1] = a;
      cost[i * 2 + 1][0] = c;
      cost[i * 2 + 1][1] = t;
    }

    int k;
    scanf("%d", &k);

    for (int i = 0; i < k; i++) {
      int p, q;
      scanf("%d %d %d", &p, &q, &r);
      p--;
      q--;

      Search(p);
      printf("%d\n", d[q]);
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define INF (1145141919)
using namespace std;

int n;

int from[6000];
int to[6000];
int cost[6000][2];

int d[100];
int r;
void Search(int s) {
  for (int i = 0; i < 100; i++) {
    d[i] = INF;
  }
  d[s] = 0;

  while (true) {
    bool update = false;

    for (int i = 0; i < n * 2; i++) {
      if (d[from[i]] != INF && d[to[i]] > d[from[i]] + cost[i][r]) {
        d[to[i]] = d[from[i]] + cost[i][r];
        update = true;
      }
    }

    if (!update) {
      break;
    }
  }
}

int main() {
  int m;
  while (scanf("%d %d", &n, &m), n + m) {
    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      scanf("%d %d %d %d", &a, &b, &c, &t);
      a--;
      b--;

      from[i * 2] = a;
      to[i * 2] = b;
      cost[i * 2][0] = c;
      cost[i * 2][1] = t;

      from[i * 2 + 1] = b;
      to[i * 2 + 1] = a;
      cost[i * 2 + 1][0] = c;
      cost[i * 2 + 1][1] = t;
    }

    int k;
    scanf("%d", &k);

    for (int i = 0; i < k; i++) {
      int p, q;
      scanf("%d %d %d", &p, &q, &r);
      p--;
      q--;

      Search(p);
      printf("%d\n", d[q]);
    }
  }

  return 0;
}
Problem p00200 (C++)

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

#define MAX 256
#define INF 1000000

struct edge {
  int money;
  int time;
};

int main() {
  int n, m;
  int count;
  edge G[120][120];

  while (cin >> n >> m, n || m) {
    for (int i = 0; i < 120; i++) {
      for (int j = 0; j < 120; j++) {
        G[i][j].money = INF;
        G[i][j].time = INF;
      }
    }
    for (int i = 0; i < 120; i++) {
      G[i][i].money = 0;
      G[i][i].time = 0;
    }

    for (int i = 0; i < n; i++) {
      int a, b, mo, t;
      cin >> a >> b >> mo >> t;

      G[a][b].time = t;
      G[a][b].money = mo;
      G[b][a].time = t;
      G[b][a].money = mo;
    }

    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          G[i][j].time = min(G[i][j].time, G[i][k].time + G[k][j].time);
          G[i][j].money = min(G[i][j].money, G[i][k].money + G[k][j].money);
        }
      }
    }

    cin >> count;
    for (int i = 0; i < count; i++) {
      int p;
      cin >> n >> m >> p;

      if (p)
        cout << G[n][m].time << endl;
      else
        cout << G[n][m].money << endl;
    }
  }
}

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

#define MAX 256
#define INF 1000000

struct edge {
  int money;
  int time;
};

int main() {
  int n, m;
  int count;
  edge G[120][120];

  while (cin >> n >> m, n || m) {
    for (int i = 0; i < 120; i++) {
      for (int j = 0; j < 120; j++) {
        G[i][j].money = INF;
        G[i][j].time = INF;
      }
    }
    for (int i = 0; i < 120; i++) {
      G[i][i].money = 0;
      G[i][i].time = 0;
    }

    for (int i = 0; i < n; i++) {
      int a, b, mo, t;
      cin >> a >> b >> mo >> t;

      G[a][b].time = t;
      G[a][b].money = mo;
      G[b][a].time = t;
      G[b][a].money = mo;
    }

    for (int k = 1; k <= m; k++) {
      for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= m; j++) {
          G[i][j].time = min(G[i][j].time, G[i][k].time + G[k][j].time);
          G[i][j].money = min(G[i][j].money, G[i][k].money + G[k][j].money);
        }
      }
    }

    cin >> count;
    for (int i = 0; i < count; i++) {
      int p;
      cin >> n >> m >> p;

      if (p)
        cout << G[n][m].time << endl;
      else
        cout << G[n][m].money << endl;
    }
  }
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  const int MAX = 1e5;
  for (int n, m; cin >> n >> m, n;) {
    int cost[m][m];
    int time[m][m];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = MAX;
        time[i][j] = MAX;
      }
    }
    int a, b, c, t;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> t;
      cost[max(a - 1, b - 1)][min(a - 1, b - 1)] = c;
      time[max(a - 1, b - 1)][min(a - 1, b - 1)] = t;
    }
    for (int k = 0; k < m; k++) {
      for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= i; j++) {
          int mai = max(i, k), mii = min(i, k);
          int maj = max(j, k), mij = min(j, k);
          if (cost[i][j] > cost[mai][mii] + cost[maj][mij])
            cost[i][j] = cost[mai][mii] + cost[maj][mij];
          if (time[i][j] > time[mai][mii] + time[maj][mij])
            time[i][j] = time[mai][mii] + time[maj][mij];
        }
      }
    }
    int l, r;
    cin >> l;
    for (int i = 0; i < l; i++) {
      cin >> a >> b >> r;
      if (r)
        cout << time[max(a - 1, b - 1)][min(a - 1, b - 1)] << endl;
      else
        cout << cost[max(a - 1, b - 1)][min(a - 1, b - 1)] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  const int MAX = 1e5;
  for (int n, m; cin >> n >> m, n;) {
    int cost[m][m];
    int time[m][m];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = MAX;
        time[i][j] = MAX;
      }
    }
    int a, b, c, t;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> t;
      cost[max(a - 1, b - 1)][min(a - 1, b - 1)] = c;
      time[max(a - 1, b - 1)][min(a - 1, b - 1)] = t;
    }
    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j <= i; j++) {
          int mai = max(i, k), mii = min(i, k);
          int maj = max(j, k), mij = min(j, k);
          if (cost[i][j] > cost[mai][mii] + cost[maj][mij])
            cost[i][j] = cost[mai][mii] + cost[maj][mij];
          if (time[i][j] > time[mai][mii] + time[maj][mij])
            time[i][j] = time[mai][mii] + time[maj][mij];
        }
      }
    }
    int l, r;
    cin >> l;
    for (int i = 0; i < l; i++) {
      cin >> a >> b >> r;
      if (r)
        cout << time[max(a - 1, b - 1)][min(a - 1, b - 1)] << endl;
      else
        cout << cost[max(a - 1, b - 1)][min(a - 1, b - 1)] << endl;
    }
  }
  return 0;
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  const int MAX = 1e5;
  for (int n, m; cin >> n >> m, n;) {
    int cost[m][m];
    int time[m][m];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = MAX;
        time[i][j] = MAX;
      }
    }
    int a, b, c, t;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> t;
      cost[a - 1][b - 1] = c;
      cost[b - 1][a - 1] = c;
      time[a - 1][b - 1] = t;
      time[b - 1][a - 1] = t;
    }
    for (int k = 0; k < m; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (cost[i][j] > cost[i][k] + cost[k][j])
            cost[i][j] = cost[i][k] + cost[k][j];
          if (time[i][j] > time[i][k] + time[k][j])
            time[i][j] = time[i][k] + time[k][j];
        }
      }
    }
    int l, r;
    cin >> l;
    for (int i = 0; i < l; i++) {
      cin >> a >> b >> r;
      if (r)
        cout << time[a - 1][b - 1] << endl;
      else
        cout << cost[a - 1][b - 1] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  const int MAX = 1e5;
  for (int n, m; cin >> n >> m, n;) {
    int cost[m][m];
    int time[m][m];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = MAX;
        time[i][j] = MAX;
      }
    }
    int a, b, c, t;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> t;
      cost[a - 1][b - 1] = c;
      cost[b - 1][a - 1] = c;
      time[a - 1][b - 1] = t;
      time[b - 1][a - 1] = t;
    }
    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          if (cost[i][j] > cost[i][k] + cost[k][j])
            cost[i][j] = cost[i][k] + cost[k][j];
          if (time[i][j] > time[i][k] + time[k][j])
            time[i][j] = time[i][k] + time[k][j];
        }
      }
    }
    int l, r;
    cin >> l;
    for (int i = 0; i < l; i++) {
      cin >> a >> b >> r;
      if (r)
        cout << time[a - 1][b - 1] << endl;
      else
        cout << cost[a - 1][b - 1] << endl;
    }
  }
  return 0;
}
Problem p00200 (C++)

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

constexpr int INF = 1000000000;

using Pair = std::pair<int, int>;

int n, m;
std::vector<Pair> cost[101];
std::vector<Pair> time_[101];

int d[101];
int dijkstra(int st, int gl, std::vector<Pair> *elm) {
  std::fill(d, d + 101, INF);

  d[st] = 0;

  std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> queue;

  queue.push({0, st});

  while (!queue.empty()) {
    auto p = queue.top();
    queue.pop();

    if (p.first > d[p.second]) {
      continue;
    }

    for (int i = 0; i < elm[p.second].size(); ++i) {
      auto e = elm[p.second][i];

      // To, cost

      if (d[e.first] > d[p.second] + e.second) {
        d[e.first] = d[p.second] + e.second;

        queue.push({d[e.first], e.first});
      }
    }
  }

  return d[gl];
}

int main() {
  while (std::cin >> n >> m, n + m) {
    int a, b, c, t;

    for (int i = 0; i < n; ++i) {
      cost[i].clear();
      time_[i].clear();
    }

    for (int i = 0; i < n; ++i) {
      std::cin >> a >> b >> c >> t;

      --a, --b;

      cost[a].push_back({b, c});
      cost[b].push_back({a, c});
      time_[a].push_back({b, t});
      time_[b].push_back({a, t});
    }

    int k;
    std::cin >> k;
    for (int i = 0; i < k; ++i) {
      int p, q, r;

      std::cin >> p >> q >> r;

      if (r == 0) {
        std::cout << dijkstra(p - 1, q - 1, cost) << std::endl;
      } else {
        std::cout << dijkstra(p - 1, q - 1, time_) << std::endl;
      }
    }
  }
}

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

constexpr int INF = 1000000000;

using Pair = std::pair<int, int>;

int n, m;
std::vector<Pair> cost[101];
std::vector<Pair> time_[101];

int d[101];
int dijkstra(int st, int gl, std::vector<Pair> *elm) {
  std::fill(d, d + 101, INF);

  d[st] = 0;

  std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> queue;

  queue.push({0, st});

  while (!queue.empty()) {
    auto p = queue.top();
    queue.pop();

    if (p.first > d[p.second]) {
      continue;
    }

    for (int i = 0; i < elm[p.second].size(); ++i) {
      auto e = elm[p.second][i];

      // To, cost

      if (d[e.first] > d[p.second] + e.second) {
        d[e.first] = d[p.second] + e.second;

        queue.push({d[e.first], e.first});
      }
    }
  }

  return d[gl];
}

int main() {
  while (std::cin >> n >> m, n + m) {
    int a, b, c, t;

    for (int i = 0; i < m; ++i) {
      cost[i].clear();
      time_[i].clear();
    }

    for (int i = 0; i < n; ++i) {
      std::cin >> a >> b >> c >> t;

      --a, --b;

      cost[a].push_back({b, c});
      cost[b].push_back({a, c});
      time_[a].push_back({b, t});
      time_[b].push_back({a, t});
    }

    int k;
    std::cin >> k;
    for (int i = 0; i < k; ++i) {
      int p, q, r;

      std::cin >> p >> q >> r;

      if (r == 0) {
        std::cout << dijkstra(p - 1, q - 1, cost) << std::endl;
      } else {
        std::cout << dijkstra(p - 1, q - 1, time_) << std::endl;
      }
    }
  }
}
Problem p00200 (C++)

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

using namespace std;

int n, m;

long long gc[110][110];
long long gt[110][110];

void warshallfloyd() {
  for (int k = 0; k < m; k++) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
        gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
      }
    }
  }
}

int main(void) {
  while (cin >> n >> m, n || m) {

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        gc[i][j] = 9999999;
        gt[i][j] = 9999999;
      }
    }

    for (int i = 0; i < n; i++) {
      int a, b, c, t;

      cin >> a >> b >> c >> t;

      a--;
      b--;

      gc[a][b] = gc[b][a] = c;
      gt[a][b] = gt[b][a] = t;
    }

    warshallfloyd();

    int k;

    cin >> k;

    for (int i = 0; i < k; i++) {
      int p, q, r;

      cin >> p >> q >> r;

      p--;
      q--;

      if (r == 0) {
        cout << gc[p][q] << endl;
      } else {
        cout << gt[p][q] << endl;
      }
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>

using namespace std;

int n, m;

long long gc[110][110];
long long gt[110][110];

void warshallfloyd() {
  for (int k = 0; k < m; k++) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
        gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
      }
    }
  }
}

int main(void) {
  while (cin >> n >> m, n || m) {

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        gc[i][j] = 9999999;
        gt[i][j] = 9999999;
      }
    }

    for (int i = 0; i < n; i++) {
      int a, b, c, t;

      cin >> a >> b >> c >> t;

      a--;
      b--;

      gc[a][b] = gc[b][a] = c;
      gt[a][b] = gt[b][a] = t;
    }

    warshallfloyd();

    int k;

    cin >> k;

    for (int i = 0; i < k; i++) {
      int p, q, r;

      cin >> p >> q >> r;

      p--;
      q--;

      if (r == 0) {
        cout << gc[p][q] << endl;
      } else {
        cout << gt[p][q] << endl;
      }
    }
  }

  return 0;
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
// 青春の片道切符：：ワーシャルフロイド法

/*for(int k=0;k<N;k++){//経由箇所(N<200)
        for(int i=0;i<N;i++){//スタート(N<200)
                for(int j=0;j<N;j++){//目的(N<200)
                        d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
                }
        }
}*/

#include <algorithm>
#include <cctype>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
using namespace std;

long long gc[110][110];
long long gt[110][110];
int n, m;

void warshallfroid() {
  for (int k = 0; k < m; k++) {     // 経由箇所(N<200)
    for (int i = 0; i < m; i++) {   // スタート(N<200)
      for (int j = 0; j < n; j++) { // 目的(N<200)
        gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
        gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
      }
    }
  }
}

int main() {

  while (cin >> n >> m, n || m /*n,mが両方0のときお終わり*/) {
    for (int i = 0; i < m; i++) {   // スタート(N<200)
      for (int j = 0; j < m; j++) { // 目的(N<200)
        gc[i][j] = 9999999;         // 初期化
        gt[i][j] = 9999999;         // 初期化
      }
    }

    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--, b--;
      gc[a][b] = gc[b][a] = c;
      gt[a][b] = gt[b][a] = t;
    }
    warshallfroid();

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--, q--;
      if (r == 0)
        cout << gc[p][q] << endl;
      else
        cout << gt[p][q] << endl;
    }
  }
}


Fixed submission:
// 青春の片道切符：：ワーシャルフロイド法

/*for(int k=0;k<N;k++){//経由箇所(N<200)
        for(int i=0;i<N;i++){//スタート(N<200)
                for(int j=0;j<N;j++){//目的(N<200)
                        d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
                }
        }
}*/

#include <algorithm>
#include <cctype>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
using namespace std;

long long gc[110][110];
long long gt[110][110];
int n, m;

void warshallfroid() {
  for (int k = 0; k < m; k++) {     // 経由箇所(N<200)
    for (int i = 0; i < m; i++) {   // スタート(N<200)
      for (int j = 0; j < m; j++) { // 目的(N<200)
        gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
        gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
      }
    }
  }
}

int main() {

  while (cin >> n >> m, n || m /*n,mが両方0のときお終わり*/) {
    for (int i = 0; i < m; i++) {   // スタート(N<200)
      for (int j = 0; j < m; j++) { // 目的(N<200)
        gc[i][j] = 9999999;         // 初期化
        gt[i][j] = 9999999;         // 初期化
      }
    }

    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--, b--;
      gc[a][b] = gc[b][a] = c;
      gt[a][b] = gt[b][a] = t;
    }
    warshallfroid();

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--, q--;
      if (r == 0)
        cout << gc[p][q] << endl;
      else
        cout << gt[p][q] << endl;
    }
  }
}
Problem p00201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int, bool> P;
map<string, P> material;
vector<string> vec[110];
map<string, P> index;

int rec(string target) {

  int value = MAX;
  map<string, P>::iterator it = material.begin();
  while (it != material.end()) {
    if ((*it).F == target)
      value = min(value, (*it).S.F);
    it++;
  }

  int X = 10000000;
  int id = -1, pre = X;
  if (index[target].S)
    id = index[target].F;

  if (id != -1) {
    for (int i = 0; i < vec[id].size(); i++) {
      pre += rec(vec[id][i]);
    }
  }

  value = min(id == -1 ? X : pre - X, value);
  assert(value != MAX);
  return value;
}

int main() {
  int n, m;

  while (true) {
    cin >> n;
    if (n == 0)
      break;

    material.clear();
    for (int i = 0; i < n; i++) {
      vec[i].clear();
      string s;
      int val;
      cin >> s >> val;

      material[s] = P(val, true);
    }

    index.clear();
    cin >> m;
    for (int i = 0; i < m; i++) {
      string s, ss;
      int cn;
      cin >> s >> cn;
      index[s] = P(i, true);
      for (int j = 0; j < cn; j++) {
        cin >> ss;
        vec[i].push_back(ss);
      }
    }

    string st;
    cin >> st;
    cout << rec(st) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int, bool> P;
map<string, P> material;
vector<string> vec[110];
map<string, P> index;

int rec(string target) {

  int value = MAX;
  map<string, P>::iterator it = material.begin();
  while (it != material.end()) {
    if ((*it).F == target)
      value = min(value, (*it).S.F);
    it++;
  }

  int X = 10000000;
  int id = -1, pre = X;
  if (index[target].S)
    id = index[target].F;

  if (id != -1) {
    for (int i = 0; i < vec[id].size(); i++) {
      pre += rec(vec[id][i]);
    }
  }

  value = min(id == -1 ? X : pre - X, value);

  return value;
}

int main() {
  int n, m;

  while (true) {
    cin >> n;
    if (n == 0)
      break;

    material.clear();
    for (int i = 0; i < n; i++) {
      vec[i].clear();
      string s;
      int val;
      cin >> s >> val;

      material[s] = P(val, true);
    }

    index.clear();
    cin >> m;
    for (int i = 0; i < m; i++) {
      string s, ss;
      int cn;
      cin >> s >> cn;
      index[s] = P(i, true);
      for (int j = 0; j < cn; j++) {
        cin >> ss;
        vec[i].push_back(ss);
      }
    }

    string st;
    cin >> st;
    cout << rec(st) << endl;
  }
  return 0;
}
Problem p00201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int n, m;
map<string, int> t;
int c[111];
vector<int> v[111];

int solve(int k) {
  int sum = 0;
  if (v[k].empty())
    return c[k];
  for (int i = 0; i < (int)v[k].size(); i++) {
    sum += solve(v[k][i]);
  }
  return min(sum, c[k]);
}

int main(void) {
  while (cin >> n && n) {
    t.clear();
    for (int i = 0; i < 111; i++)
      v[i].clear();
    for (int i = 0; i < n; i++) {
      string s;
      int a;
      cin >> s >> a;
      t[s] = n;
      c[n] = a;
    }
    cin >> m;
    for (int j = 0; j < m; j++) {
      string s;
      int a;
      cin >> s >> a;
      int id = t[s];
      for (int i = 0; i < a; i++) {
        cin >> s;
        v[id].push_back(t[s]);
      }
    }
    string s;
    cin >> s;
    cout << solve(t[s]) << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int n, m;
map<string, int> t;
int c[111];
vector<int> v[111];

int solve(int k) {
  int sum = 0;
  if (v[k].empty())
    return c[k];
  for (int i = 0; i < (int)v[k].size(); i++) {
    sum += solve(v[k][i]);
  }
  return min(sum, c[k]);
}

int main(void) {
  while (cin >> n && n) {
    t.clear();
    for (int i = 0; i < 111; i++)
      v[i].clear();
    for (int i = 0; i < n; i++) {
      string s;
      int a;
      cin >> s >> a;
      t[s] = i;
      c[i] = a;
    }
    cin >> m;
    for (int j = 0; j < m; j++) {
      string s;
      int a;
      cin >> s >> a;
      int id = t[s];
      for (int i = 0; i < a; i++) {
        cin >> s;
        v[id].push_back(t[s]);
      }
    }
    string s;
    cin >> s;
    cout << solve(t[s]) << endl;
  }
}
Problem p00201 (C++)

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

using namespace std;

map<string, vector<string>> recipe;
map<string, int> cost;

int search(string item) {
  if (recipe[item].size() == 0)
    return cost[item];
  else {
    int sum = 0;
    for (int i = 0; i < recipe[item].size(); i++) {
      sum += search(recipe[item][i]);
    }
    return min(cost[item], sum);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      string name;
      int m;
      cin >> name >> m;
      cost[name] = m;
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      string item;
      cin >> item;
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        string material;
        cin >> material;
        recipe[item].push_back(material);
      }
    }
    string item;
    cin >> item;
    cout << search(item) << endl;
  }
}

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

using namespace std;

map<string, vector<string>> recipe;
map<string, int> cost;

int search(string item) {
  if (recipe[item].size() == 0)
    return cost[item];
  else {
    int sum = 0;
    for (int i = 0; i < recipe[item].size(); i++) {
      sum += search(recipe[item][i]);
    }
    return min(cost[item], sum);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    recipe.clear();
    cost.clear();
    for (int i = 0; i < n; i++) {
      string name;
      int m;
      cin >> name >> m;
      cost[name] = m;
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      string item;
      cin >> item;
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        string material;
        cin >> material;
        recipe[item].push_back(material);
      }
    }
    string item;
    cin >> item;
    cout << search(item) << endl;
  }
}
Problem p00201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int N, M;
int cost[100];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];

int main() {
  while (cin >> N, N) {
    REP(i, N) cin >> S[i] >> P[i];
    cin >> M;
    REP(i, M) {
      cin >> O[i] >> K[i];
      REP(j, K[i]) cin >> Q[i][j];
    }
    cin >> T;

    map<string, int> conv;
    REP(i, N) conv[S[i]] = conv.size() - 1;

    REP(i, M) g[i].clear();
    REP(i, M) {
      g[i].push_back(conv[O[i]]);
      REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
    }

    fill(cost, cost + 200, INF);
    REP(i, N) chmin(cost[conv[S[i]]], P[i]);

    REP(t, M - 1) {
      REP(i, M) {
        ll sum = 0;
        FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
        chmin(cost[g[i][0]], sum);
      }
    }

    cout << cost[conv[T]] << endl;
  }
  return 0;
}

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int N, M;
int cost[100];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];

int main() {
  while (cin >> N, N) {
    REP(i, N) cin >> S[i] >> P[i];
    cin >> M;
    REP(i, M) {
      cin >> O[i] >> K[i];
      REP(j, K[i]) cin >> Q[i][j];
    }
    cin >> T;

    map<string, int> conv;
    REP(i, N) conv[S[i]] = conv.size() - 1;

    REP(i, M) g[i].clear();
    REP(i, M) {
      g[i].push_back(conv[O[i]]);
      REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
    }

    fill(cost, cost + 100, INF);
    REP(i, N) chmin(cost[conv[S[i]]], P[i]);

    REP(t, M - 1) {
      REP(i, M) {
        ll sum = 0;
        FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
        chmin(cost[g[i][0]], sum);
      }
    }

    cout << cost[conv[T]] << endl;
  }
  return 0;
}
Problem p00201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)

using namespace std;

map<string, int> name_id;
vector<int> recipe[100];
int price[100];

int solve(int x) {

  int len = recipe[x].size();

  if (len == 0) {
    return price[x];
  }

  int ans = 0;
  rep(i, len) { ans += solve(recipe[x][i]); }
  // printf("%d->%d\n",x,price[x]);
  return min(ans, price[x]);
}

int main() {
  int n;
  while (cin >> n, n) {
    rep(i, n) {
      string name;
      int p;
      cin >> name >> p;
      name_id[name] = i;
      price[i] = p;
    }

    int m;
    cin >> m;

    rep(i, m) {
      string name;
      int num;
      cin >> name >> num;
      int id = name_id[name];
      rep(k, num) {
        string r;
        cin >> r;
        recipe[id].push_back(name_id[r]);
      }
    }

    string a;
    cin >> a;
    cout << solve(name_id[a]) << endl;
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)

using namespace std;

map<string, int> name_id;
vector<int> recipe[100];
int price[100];

int solve(int x) {

  int len = recipe[x].size();

  if (len == 0) {
    return price[x];
  }

  int ans = 0;
  rep(i, len) { ans += solve(recipe[x][i]); }
  // printf("%d->%d\n",x,price[x]);
  return min(ans, price[x]);
}

int main() {
  int n;
  while (cin >> n, n) {
    name_id.clear();
    rep(i, 100) recipe[i].clear();
    rep(i, n) {
      string name;
      int p;
      cin >> name >> p;
      name_id[name] = i;
      price[i] = p;
    }

    int m;
    cin >> m;

    rep(i, m) {
      string name;
      int num;
      cin >> name >> num;
      int id = name_id[name];
      rep(k, num) {
        string r;
        cin >> r;
        recipe[id].push_back(name_id[r]);
      }
    }

    string a;
    cin >> a;
    cout << solve(name_id[a]) << endl;
  }
  return 0;
}
Problem p00201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

map<string, int> normal;
map<string, vector<string>> rec;

int f(string x) {
  int ans = normal[x], sum = 0;
  if (rec[x].size() == 0)
    return ans;
  for (int i = 0; i < rec[x].size(); i++) {
    sum += f(rec[x][i]);
  }
  return min(ans, sum);
}

int main() {
  int n;
  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      string a;
      int b;
      cin >> a >> b;
      normal[a] = b;
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
      string x, y;
      cin >> x;
      int t;
      cin >> t;
      for (int j = 0; j < t; j++) {
        cin >> y;
        rec[x].push_back(y);
      }
    }
    string want;
    cin >> want;
    cout << f(want) << endl;
  }
}

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

map<string, int> normal;
map<string, vector<string>> rec;

int f(string x) {
  int ans = normal[x], sum = 0;
  if (rec[x].size() == 0)
    return ans;
  for (int i = 0; i < rec[x].size(); i++) {
    sum += f(rec[x][i]);
  }
  return min(ans, sum);
}

int main() {
  int n;
  while (cin >> n && n) {
    normal.clear();
    rec.clear();
    for (int i = 0; i < n; i++) {
      string a;
      int b;
      cin >> a >> b;
      normal[a] = b;
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
      string x, y;
      cin >> x;
      int t;
      cin >> t;
      for (int j = 0; j < t; j++) {
        cin >> y;
        rec[x].push_back(y);
      }
    }
    string want;
    cin >> want;
    cout << f(want) << endl;
  }
}
Problem p00202 (C++)

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

bool prime[1000001];

int main() {
  int n, x;
  rep(i, 1000002) prime[i] = true;
  prime[0] = prime[1] = false;
  loop(i, 2, 1010) for (int j = i * 2; j < 1000001; j += i) prime[j] = false;
  while (cin >> n >> x, n || x) {
    int dish[n];
    rep(i, n) cin >> dish[i];
    bool cost[x + 10];
    rep(i, x + 10) cost[i] = false;
    rep(i, n) cost[dish[i]] = true;
    cost[0] = true;
    rep(i, x + 1) {
      if (!cost[i])
        continue;
      rep(j, n) {
        if (i + dish[j] > x)
          continue;
        cost[i + dish[j]] = true;
      }
    }
    bool check = true;
    for (int i = x; i >= 0; i--) {
      if (prime[i] && cost[i]) {
        check = false;
        cout << i << endl;
        break;
      }
    }
    if (check)
      cout << "NA" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

bool prime[1000001];

int main() {
  int n, x;
  rep(i, 1000002) prime[i] = true;
  prime[0] = prime[1] = false;
  loop(i, 2, 1010) for (int j = i * 2; j < 1000001; j += i) prime[j] = false;
  while (cin >> n >> x, n || x) {
    int dish[n];
    rep(i, n) cin >> dish[i];
    bool cost[x + 10];
    rep(i, x + 10) cost[i] = false;
    rep(i, n) if (dish[i] < x + 1) cost[dish[i]] = true;
    cost[0] = true;
    rep(i, x + 1) {
      if (!cost[i])
        continue;
      rep(j, n) {
        if (i + dish[j] > x)
          continue;
        cost[i + dish[j]] = true;
      }
    }
    bool check = true;
    for (int i = x; i >= 0; i--) {
      if (prime[i] && cost[i]) {
        check = false;
        cout << i << endl;
        break;
      }
    }
    if (check)
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 1000001;

int main(void) {
  vector<bool> isP(N);
  fill(ALL(isP), true);
  rep(i, 2) isP[i] = false;
  for (int i = 2; i * i < N; i++)
    if (isP[i])
      for (int j = i * i; j < N; j += i)
        isP[j] = false;

  for (int n, x; cin >> n >> x, n;) {
    vector<bool> dp(x + 1);
    fill(ALL(dp), false);
    dp[0] = true;
    while (n--) {
      int m;
      cin >> m;

      rep(i, x) if (dp[i]) dp[i + m] = true;
    }
    while (~x && !(dp[x] && isP[x]))
      x--;

    if (~x)
      cout << x << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 1000001;

int main(void) {
  vector<bool> isP(N);
  fill(ALL(isP), true);
  rep(i, 2) isP[i] = false;
  for (int i = 2; i * i < N; i++)
    if (isP[i])
      for (int j = i * i; j < N; j += i)
        isP[j] = false;

  for (int n, x; cin >> n >> x, n;) {
    vector<bool> dp(x + 1);
    fill(ALL(dp), false);
    dp[0] = true;
    while (n--) {
      int m;
      cin >> m;

      rep(i, x - m + 1) if (dp[i]) dp[i + m] = true;
    }
    while (~x && !(dp[x] && isP[x]))
      x--;

    if (~x)
      cout << x << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}
Problem p00202 (C++)

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

#define rep(i, n) for (int i = 0; i < n; i++)

bool is_prime(int n) {
  if (n % 2 == 0)
    return false;
  for (int i = 3; i <= std::sqrt(n); i += 2)
    if (n % i == 0)
      return false;
  return true;
}

int main() {
  int n, x;
  while (std::cin >> n >> x && n > 0 && x > 0) {
    auto course = new bool[x + 1]();
    rep(i, x + 1) course[i] = false;

    int prices[n];
    rep(i, n) { std::cin >> prices[i]; }
    course[0] = true;

    int max = -1;
    rep(i, x + 1) rep(j, n) {
      if (i - prices[j] >= 0 && course[i - prices[j]]) {
        if (is_prime(i))
          max = i;
        course[i] = true;
        break;
      }
    }
    if (max < 0)
      std::cout << "NA" << std::endl;
    else
      std::cout << max << std::endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)

bool is_prime(int n) {
  if (n % 2 == 0)
    return false;
  for (int i = 3; i <= std::sqrt(n); i += 2)
    if (n % i == 0)
      return false;
  return true;
}

int main() {
  int n, x;
  while (std::cin >> n >> x && n > 0 && x > 0) {
    auto course = new bool[x + 1]();
    rep(i, x + 1) course[i] = false;

    int prices[n];
    rep(i, n) { std::cin >> prices[i]; }
    course[0] = true;

    int max = -1;
    rep(i, x + 1) rep(j, n) {
      if (i - prices[j] >= 0 && course[i - prices[j]]) {
        if (is_prime(i))
          max = i;
        course[i] = true;
        break;
      }
    }
    if (max < 0)
      std::cout << "NA" << std::endl;
    else
      std::cout << max << std::endl;
    delete[] course;
  }
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

bool isPrime[1000001];
bool dp[1000001];
int main() {
  fill(isPrime, isPrime + 1000001, true);
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < 1000001; ++i) {
    if (isPrime[i]) {
      for (int j = 2 * i; j < 1000001; j += i)
        isPrime[j] = false;
    }
  }

  int n, x;
  while (scanf("%d %d", &n, &x), n | x) {
    int v[30];
    for (int i = 0; i < n; ++i) {
      int t;
      scanf("%d", &t);
      v[i] = t;
    }

    fill(dp, dp + x + 1, false);
    dp[0] = true;
    for (int i = 0; i <= x; ++i)
      if (dp[i])
        for (int j = 0; j < n; ++j)
          dp[i + v[j]] = true;

    int ans;
    for (ans = x; ans > 0; --ans)
      if (dp[ans] && isPrime[ans])
        break;
    if (ans)
      printf("%d\n", ans);
    else
      puts("NA");
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

bool isPrime[1000001];
bool dp[1000001];
int main() {
  fill(isPrime, isPrime + 1000001, true);
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < 1000001; ++i) {
    if (isPrime[i]) {
      for (int j = 2 * i; j < 1000001; j += i)
        isPrime[j] = false;
    }
  }

  int n, x;
  while (scanf("%d %d", &n, &x), n | x) {
    int v[30];
    for (int i = 0; i < n; ++i) {
      int t;
      scanf("%d", &t);
      v[i] = t;
    }

    fill(dp, dp + x + 1, false);
    dp[0] = true;
    for (int i = 0; i <= x; ++i)
      if (dp[i])
        for (int j = 0; j < n; ++j)
          if (i + v[j] <= x)
            dp[i + v[j]] = true;

    int ans;
    for (ans = x; ans > 0; --ans)
      if (dp[ans] && isPrime[ans])
        break;
    if (ans)
      printf("%d\n", ans);
    else
      puts("NA");
  }

  return 0;
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

bool isp(int n) {
  if (n == 2)
    return true;
  if (n == 1)
    return false;
  if (n == 0)
    return false;

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

int main() {
  int n, x;
  while (cin >> n >> x) {
    if (n == 0 && x == 0)
      break;

    int data[40];
    rep(i, n) { cin >> data[i]; }

    bool dp[1000001] = {};
    rep(i, 1000001) dp[i] = false;

    dp[0] = true;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= x; j++) {
        //                dp[i][j] = dp[i-1][j];

        dp[j] |= dp[j - data[i - 1]];
      }
    }
    int ans = -1;
    for (int i = x; i >= 0; i--) {
      if (dp[i] == true) {
        if (isp(i)) {
          ans = i;
          break;
        }
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

bool isp(int n) {
  if (n == 2)
    return true;
  if (n == 1)
    return false;
  if (n == 0)
    return false;

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

int main() {
  int n, x;
  while (cin >> n >> x) {
    if (n == 0 && x == 0)
      break;

    int data[40];
    rep(i, n) { cin >> data[i]; }

    bool dp[1000001] = {};
    rep(i, 1000001) dp[i] = false;

    dp[0] = true;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= x; j++) {
        //                dp[i][j] = dp[i-1][j];
        if (j - data[i - 1] >= 0)
          dp[j] |= dp[j - data[i - 1]];
      }
    }
    int ans = -1;
    for (int i = x; i >= 0; i--) {
      if (dp[i] == true) {
        if (isp(i)) {
          ans = i;
          break;
        }
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
#define INF 999999999
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
bool sosu(int n) {
  if (n <= 1)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
int main() {
  int n, x;
  while (cin >> n >> x, n) {
    bool dp[1000001] = {};
    dp[0] = 1;
    int a[50];
    rep(i, n) { cin >> a[i]; }
    rep(i, n) rep(j, x) {
      if (dp[j])
        dp[j + a[i]] = 1;
    }
    for (int i = x; i > 1; i--) {
      if (dp[i])
        if (sosu(i)) {
          cout << i << endl;
          goto end;
        }
    }
    cout << "NA" << endl;
  end:;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
#define INF 999999999
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
bool sosu(int n) {
  if (n <= 1)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
int main() {
  int n, x;
  while (cin >> n >> x, n) {
    bool dp[1000001] = {};
    dp[0] = 1;
    int a[50];
    rep(i, n) { cin >> a[i]; }
    rep(i, n) rep(j, x) {
      if (j + a[i] < 1000001)
        if (dp[j])
          dp[j + a[i]] = 1;
    }
    for (int i = x; i > 1; i--) {
      if (dp[i])
        if (sosu(i)) {
          cout << i << endl;
          goto end;
        }
    }
    cout << "NA" << endl;
  end:;
  }
  return 0;
}