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

Buggy submission (status: Runtime Error):
//
//  main.cpp

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

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<pair<int, int>> hashira;
    bool check[5001][5001];
    for (int i = 0; i < 5001; i++) {
      for (int j = 0; j < 5001; j++) {
        check[i][j] = false;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      check[a][b] = true;
      hashira.push_back(make_pair(a, b));
    }
    int answer = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int x = hashira[j].first - hashira[i].first;
        int y = hashira[j].second - hashira[i].second;
        if (check[hashira[i].first - y][hashira[i].second + x] &&
            check[hashira[i].first - y + x][hashira[i].second + y + x]) {
          answer = max(answer, x * x + y * y);
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Fixed submission:
//
//  main.cpp

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

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<pair<int, int>> hashira;
    bool check[5001][5001];
    for (int i = 0; i < 5001; i++) {
      for (int j = 0; j < 5001; j++) {
        check[i][j] = false;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      check[a][b] = true;
      hashira.push_back(make_pair(a, b));
    }
    int answer = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int x = hashira[j].first - hashira[i].first;
        int y = hashira[j].second - hashira[i].second;
        if (hashira[i].first - y > 5000 || hashira[i].second + x > 5000 ||
            hashira[i].first - y + x > 5000 ||
            hashira[i].second + y + x > 5000 || hashira[i].first - y < 0 ||
            hashira[i].second + x < 0 || hashira[i].first - y + x < 0 ||
            hashira[i].second + y + x < 0) {
          continue;
        }

        if (check[hashira[i].first - y][hashira[i].second + x] &&
            check[hashira[i].first - y + x][hashira[i].second + y + x]) {
          answer = max(answer, x * x + y * y);
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}
Problem p00442 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int lose[1005] = {0};
bool match[5005][5005];
int main() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  int tf = 0;
  int no_1 = -1;
  for (int i = 0; i < 5005; i++) {
    for (int j = 0; j < 5005; j++)
      match[i][j] = false;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    lose[b]++;
    match[b][a] = true;
  }
  for (int i = 1; i <= n; i++) {
    if (lose[i] == 0) {
      if (no_1 != -1)
        tf = 1;
      no_1 = i;
    }
  }
  int ans[5005] = {0};
  bool used[5005];
  for (int i = 0; i < 5005; i++)
    used[i] = false;
  for (int i = 1; i <= n; i++) {
    ans[i] = no_1;
    used[no_1] = true;
    for (int j = 1; j <= n; j++) {
      if (match[j][no_1])
        lose[j]--;
    }
    no_1 = -1;
    for (int j = 1; j <= n; j++) {
      if (lose[j] == 0 && !used[j]) {
        if (no_1 != -1)
          tf = 1;
        no_1 = j;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  cout << tf << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int lose[5005] = {0};
bool match[5005][5005];
int main() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  int tf = 0;
  int no_1 = -1;
  for (int i = 0; i < 5005; i++) {
    for (int j = 0; j < 5005; j++)
      match[i][j] = false;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    lose[b]++;
    match[b][a] = true;
  }
  for (int i = 1; i <= n; i++) {
    if (lose[i] == 0) {
      if (no_1 != -1)
        tf = 1;
      no_1 = i;
    }
  }
  int ans[5005] = {0};
  bool used[5005];
  for (int i = 0; i < 5005; i++)
    used[i] = false;
  for (int i = 1; i <= n; i++) {
    ans[i] = no_1;
    used[no_1] = true;
    for (int j = 1; j <= n; j++) {
      if (match[j][no_1])
        lose[j]--;
    }
    no_1 = -1;
    for (int j = 1; j <= n; j++) {
      if (lose[j] == 0 && !used[j]) {
        if (no_1 != -1)
          tf = 1;
        no_1 = j;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  cout << tf << endl;
  return 0;
}
Problem p00442 (C++)

Buggy submission (status: Runtime Error):
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 3e3 + 10;
int main() {
  int n, m, l, r, d[N], nxt;
  vector<int> graph[N];
  queue<int> q;
  bool y = false;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    d[i] = 0;
  while (m--) {
    scanf("%d%d", &l, &r);
    graph[l].PB(r);
    d[r]++;
  }
  for (int i = 1; i <= n; i++)
    if (d[i] == 0)
      q.push(i);
  while (!q.empty()) {
    nxt = q.front();
    q.pop();
    printf("%d\n", nxt);
    y |= !q.empty();
    for (int i : graph[nxt]) {
      d[i]--;
      if (d[i] == 0)
        q.push(i);
    }
  }
  printf("%d\n", y ? 1 : 0);
}


Fixed submission:
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 5e3 + 10;
int main() {
  int n, m, l, r, d[N], nxt;
  vector<int> graph[N];
  queue<int> q;
  bool y = false;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    d[i] = 0;
  while (m--) {
    scanf("%d%d", &l, &r);
    graph[l].PB(r);
    d[r]++;
  }
  for (int i = 1; i <= n; i++)
    if (d[i] == 0)
      q.push(i);
  while (!q.empty()) {
    nxt = q.front();
    q.pop();
    printf("%d\n", nxt);
    y |= !q.empty();
    for (int i : graph[nxt]) {
      d[i]--;
      if (d[i] == 0)
        q.push(i);
    }
  }
  printf("%d\n", y ? 1 : 0);
}
Problem p00442 (C++)

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

#define FOR(i, a) for (i = 0; i < (a); i++)
#define FORM(i, a) for (i = (a)i >= 0; i--)
using namespace std;

vector<int> E[1010];
int indeg[1010];
queue<int> q;

void bfs(int s) {
  int i, j;

  FOR(i, E[s].size()) {
    indeg[E[s][i]]--;
    if (indeg[E[s][i]] == 0) {
      q.push(E[s][i]);
    }
  }
}

void ranking(int N) {
  int i, j, k, l, flag = 0;

  FOR(i, N) {
    if (indeg[i + 1] == 0) {
      q.push(i + 1);
      indeg[i + 1]--;
    }
  }

  if (q.size() > 1)
    flag = 1;

  while (q.size() != 0) {
    cout << q.front() << endl;
    bfs(q.front());
    q.pop();
    if (q.size() > 1) {
      flag = 1;
    }
  }

  cout << flag << endl;
}

int main() {
  int i, j, N, K, m, n;

  cin >> n >> m;

  FOR(K, m) {
    cin >> i >> j;
    E[i].push_back(j);
    indeg[j]++;
  }

  ranking(n);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

#define FOR(i, a) for (i = 0; i < (a); i++)
#define FORM(i, a) for (i = (a)i >= 0; i--)
using namespace std;

vector<int> E[5050];
int indeg[5050];
queue<int> q;

void bfs(int s) {
  int i, j;

  FOR(i, E[s].size()) {
    indeg[E[s][i]]--;
    if (indeg[E[s][i]] == 0) {
      q.push(E[s][i]);
    }
  }
}

void ranking(int N) {
  int i, j, k, l, flag = 0;

  FOR(i, N) {
    if (indeg[i + 1] == 0) {
      q.push(i + 1);
      indeg[i + 1]--;
    }
  }

  if (q.size() > 1)
    flag = 1;

  while (q.size() != 0) {
    cout << q.front() << endl;
    bfs(q.front());
    q.pop();
    if (q.size() > 1) {
      flag = 1;
    }
  }

  cout << flag << endl;
}

int main() {
  int i, j, N, K, m, n;

  cin >> n >> m;

  FOR(K, m) {
    cin >> i >> j;
    E[i].push_back(j);
    indeg[j]++;
  }

  ranking(n);

  return 0;
}
Problem p00442 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000005
#define MAX 5001
using namespace std;

queue<int> s;
stack<int> a[MAX];
bool check[MAX][MAX];
int b[MAX];
int main() {
  int n, m, t1, t2;
  scanf("%d %d", &n, &m);
  fill(b, b + MAX, 0);
  fill((int *)check, (int *)(check + MAX), 0);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &t1, &t2);
    check[t1][t2] = 1;
    a[t1].push(t2);
    b[t2]++;
  }

  int ans[MAX], c = 0;
  for (int i = 1; i <= n; i++)
    if (!b[i])
      s.push(i);
  while (!s.empty()) {
    int t = s.front();
    ans[c++] = s.front();
    while (!a[t].empty()) {
      if (--b[a[t].top()] == 0)
        s.push(a[t].top());
      a[t].pop();
    }
    s.pop();
  }

  int f = 0;
  for (int i = 0; i < n - 1; i++) {
    if (check[ans[i]][ans[i + 1]] != 1)
      f = 1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\n", ans[i]);
  printf("%d\n", f);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000005
#define MAX 5001
using namespace std;

queue<int> s;
stack<int> a[MAX];
bool check[MAX][MAX];
int b[MAX];
int main() {
  int n, m, t1, t2;
  scanf("%d %d", &n, &m);
  fill(b, b + MAX, 0);
  rep(i, MAX) rep(j, MAX) check[i][j] = false;
  // fill((int*)check, (int*)(check+MAX), false);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &t1, &t2);
    check[t1][t2] = 1;
    a[t1].push(t2);
    b[t2]++;
  }

  int ans[MAX], c = 0;
  for (int i = 1; i <= n; i++)
    if (!b[i])
      s.push(i);
  while (!s.empty()) {
    int t = s.front();
    ans[c++] = s.front();
    while (!a[t].empty()) {
      if (--b[a[t].top()] == 0)
        s.push(a[t].top());
      a[t].pop();
    }
    s.pop();
  }

  int f = 0;
  for (int i = 0; i < n - 1; i++) {
    if (check[ans[i]][ans[i + 1]] != 1)
      f = 1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\n", ans[i]);
  printf("%d\n", f);
  return 0;
}
Problem p00442 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
#define all(c) c.begin(), c.end()

const int MAX_N = 5000;

// win[i]にjが含まれるならiはjに勝った
// win[i]にjが含まれるならiはjに負けた
vector<int> win[MAX_N + 1];
vector<int> lose[MAX_N + 1];

vector<int> ord;
int used[MAX_N + 1] = {0};

// vがordに追加される時点で，
// vから到達可能な全ての頂点，すなわち，vより順位が低いチームは，必ずordに追加されている
void tsort(int v) {
  used[v] = true;
  for (int i = 0; i < lose[v].size(); i++) {
    if (!used[lose[v][i]])
      tsort(win[v][i]);
  }
  ord.pb(v);
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    // 頂点w(winner), l(loser)を入力
    int w, l;
    cin >> w >> l;
    // win[w]にlを含める
    // lose[l]にwを含める
    win[w].pb(l);
    lose[l].pb(w);
  }
  // この時点では誰が1位か分からないので，
  // 誰かに勝っていれば，自分を1位としてトポロジカルソートを行う
  for (int i = 1; i <= n; i++)
    if (!win[i].size())
      tsort(i);

  // "他のソート結果が存在する"フラグ
  bool exi = 0;
  for (int i = 1; i < n; i++) {
    // "i位のチームにi+1位のチームが勝った"という情報が与えられているか？
    bool ok = find(all(lose[ord[i]]), ord[i - 1]) == lose[ord[i]].end();
    exi |= ok;
  }
  // 順位を出力
  for (int i = 0; i < ord.size(); i++) {
    cout << ord[i] << endl;
  }
  // 存在するかどうかを出力
  cout << exi << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
#define all(c) c.begin(), c.end()

const int MAX_N = 5000;

// win[i]にjが含まれるならiはjに勝った
// win[i]にjが含まれるならiはjに負けた
vector<int> win[MAX_N + 1];
vector<int> lose[MAX_N + 1];

vector<int> ord;
int used[MAX_N + 1] = {0};

// vがordに追加される時点で，
// vから到達可能な全ての頂点，すなわち，vより順位が低いチームは，必ずordに追加されている
void tsort(int v) {
  used[v] = true;
  for (int i = 0; i < lose[v].size(); i++) {
    if (!used[lose[v][i]])
      tsort(lose[v][i]);
  }
  ord.pb(v);
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    // 頂点w(winner), l(loser)を入力
    int w, l;
    cin >> w >> l;
    // win[w]にlを含める
    // lose[l]にwを含める
    win[w].pb(l);
    lose[l].pb(w);
  }
  // この時点では誰が1位か分からないので，
  // 誰かに勝っていれば，自分を1位としてトポロジカルソートを行う
  for (int i = 1; i <= n; i++)
    if (!win[i].size())
      tsort(i);

  // "他のソート結果が存在する"フラグ
  bool exi = 0;
  for (int i = 1; i < n; i++) {
    // "i位のチームにi+1位のチームが勝った"という情報が与えられているか？
    bool ok = find(all(lose[ord[i]]), ord[i - 1]) == lose[ord[i]].end();
    exi |= ok;
  }
  // 順位を出力
  for (int i = 0; i < ord.size(); i++) {
    cout << ord[i] << endl;
  }
  // 存在するかどうかを出力
  cout << exi << endl;
}
Problem p00442 (C++)

Buggy submission (status: Memory Limit Exceeded):
// 0519-Worst Sportwriter
#include <bits/stdc++.h>
using namespace std;
int data[5000][5000];
vector<int> win(5000);
vector<int> unknown(5000);
int main() {
  int n, m;
  cin >> n >> m;
  for (int t = 0; t < m; t++) {
    int i, j;
    cin >> i >> j;
    i--;
    j--;
    data[i][j] = 1;
    data[j][i] = -1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      if (data[i][j] == 1)
        win[i]++;
      else if (data[i][j] == 0)
        unknown[i]++;
    }
  }
  vector<int> rank(n, 0);
  int another = 0;
  for (int i = 1; i <= n; i++) {
    int next = -1;
    int nwin = 0;
    for (int j = 0; j < n; j++) {
      if (win[j] == (n - i) && unknown[j] == 0) {
        rank[j] = i;
        cout << j + 1 << endl;
        next = -1;
        break;
      } else if (win[j] + unknown[j] == (n - i)) {
        if (rank[j] != 0)
          continue;
        if (next == -1) {
          next = j;
          nwin = win[j];
        } else if (nwin < win[j]) {
          next = j;
          nwin = win[j];
          another = 1;
        } else
          another = 1;
      }
    }
    if (next != -1) {
      rank[next] = i;
      cout << next + 1 << endl;
      for (int j = 0; j < n; j++) {
        if (next == j)
          continue;
        if (data[next][j] == 0) {
          data[next][j] = 1;
          data[j][next] = -1;
          unknown[j]--;
        }
      }
      win[next] += unknown[next];
      unknown[next] = 0;
    }
  }
  cout << another << endl;
  return 0;
}


Fixed submission:
// 0519-Worst Sportwriter
#include <bits/stdc++.h>
using namespace std;
short data[5000][5000];
vector<int> win(5000);
vector<int> unknown(5000);
int main() {
  int n, m;
  cin >> n >> m;
  for (int t = 0; t < m; t++) {
    int i, j;
    cin >> i >> j;
    i--;
    j--;
    data[i][j] = 1;
    data[j][i] = -1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      if (data[i][j] == 1)
        win[i]++;
      else if (data[i][j] == 0)
        unknown[i]++;
    }
  }
  vector<int> rank(n, 0);
  int another = 0;
  for (int i = 1; i <= n; i++) {
    int next = -1;
    int nwin = 0;
    for (int j = 0; j < n; j++) {
      if (win[j] == (n - i) && unknown[j] == 0) {
        rank[j] = i;
        cout << j + 1 << endl;
        next = -1;
        break;
      } else if (win[j] + unknown[j] == (n - i)) {
        if (rank[j] != 0)
          continue;
        if (next == -1) {
          next = j;
          nwin = win[j];
        } else if (nwin < win[j]) {
          next = j;
          nwin = win[j];
          another = 1;
        } else
          another = 1;
      }
    }
    if (next != -1) {
      rank[next] = i;
      cout << next + 1 << endl;
      for (int j = 0; j < n; j++) {
        if (next == j)
          continue;
        if (data[next][j] == 0) {
          data[next][j] = 1;
          data[j][next] = -1;
          unknown[j]--;
        }
      }
      win[next] += unknown[next];
      unknown[next] = 0;
    }
  }
  cout << another << endl;
  return 0;
}
Problem p00444 (C++)

Buggy submission (status: Time Limit Exceeded):
//============================================================================
// Name        : 0521.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int main() {
  int x;
  int c[6] = {500, 100, 50, 10, 5, 1};
  while (1) {
    int ans = 0;
    cin >> x;
    x = 1000 - x;
    if (x == 0) {
      break;
    }
    for (int i = 0; i < 6; i++) {
      ans = x / c[i] + ans;
      x = x % c[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
//============================================================================
// Name        : 0521.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int main() {
  int x;
  int c[6] = {500, 100, 50, 10, 5, 1};
  while (1) {
    int ans = 0;
    cin >> x;
    x = 1000 - x;
    if (x == 1000) {
      break;
    }
    for (int i = 0; i < 6; i++) {
      ans = x / c[i] + ans;
      x = x % c[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00444 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n, b;
  while (true) {
    cin >> b;
    n = 1000 - b;
    ;
    if (n == 0) {
      break;
    }
    cout << n / 500 + (n / 100) % 5 + (n / 50) % 2 + (n / 10) % 5 +
                (n / 5) % 2 + n % 5
         << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, b;
  while (true) {
    cin >> b;
    n = 1000 - b;
    ;
    if (b == 0) {
      break;
    }
    cout << n / 500 + (n / 100) % 5 + (n / 50) % 2 + (n / 10) % 5 +
                (n / 5) % 2 + n % 5
         << endl;
  }
}
Problem p00444 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;

int main() {
  int x, y;
  while (scanf("%d", &x) != 0) {
    x = 1000 - x;
    y = 0;
    while (x >= 500) {
      x -= 500;
      y++;
    }
    while (x >= 100) {
      x -= 100;
      y++;
    }
    while (x >= 50) {
      x -= 50;
      y++;
    }
    while (x >= 10) {
      x -= 10;
      y++;
    }
    while (x >= 5) {
      x -= 5;
      y++;
    }
    while (x >= 1) {
      x -= 1;
      y++;
    }
    printf("%d\n", y);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;

int main() {
  int x, y;
  while (scanf("%d", &x)) {
    if (x == 0) {
      break;
    }
    x = 1000 - x;
    y = 0;
    while (x >= 500) {
      x -= 500;
      y++;
    }
    while (x >= 100) {
      x -= 100;
      y++;
    }
    while (x >= 50) {
      x -= 50;
      y++;
    }
    while (x >= 10) {
      x -= 10;
      y++;
    }
    while (x >= 5) {
      x -= 5;
      y++;
    }
    while (x >= 1) {
      x -= 1;
      y++;
    }
    printf("%d\n", y);
  }
  return 0;
}
Problem p00444 (C++)

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

int main() {
  int total;
  int change;
  int count = 0;
  while (true) {
    cin >> total;
    if (total == 0) {
      break;
    }
    change = 1000 - total;
    while (true) {
      if (change >= 500) {
        change -= 500;
        count++;
      } else if (change >= 100) {
        change -= 100;
        count++;
      } else if (change >= 50) {
        change -= 50;
        count++;
      } else if (change >= 10) {
        change -= 10;
        count++;
      } else if (change >= 5) {
        change -= 5;
        count++;
      } else {
        change--;
        count++;
      }
    }
    cout << count << "\n";
    count = 0;
  }
  return 0;
}

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

int main() {
  int total;
  int change;
  int count = 0;
  while (true) {
    cin >> total;
    if (total == 0) {
      break;
    }
    change = 1000 - total;
    while (true) {
      if (change >= 500) {
        change -= 500;
        count++;
      } else if (change >= 100) {
        change -= 100;
        count++;
      } else if (change >= 50) {
        change -= 50;
        count++;
      } else if (change >= 10) {
        change -= 10;
        count++;
      } else if (change >= 5) {
        change -= 5;
        count++;
      } else {
        change--;
        count++;
      }
      if (change == 0) {
        break;
      }
    }
    cout << count << "\n";
    count = 0;
  }
  return 0;
}
Problem p00444 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int a, b, c, d, e, f, g;
  while (1) {
    cin >> a;
    if (a = 0)
      break;
    b = (1000 - a) / 500;
    c = (1000 - (a + 500 * b)) / 100;
    d = (1000 - (a + 500 * b + 100 * c)) / 50;
    e = (1000 - (a + 500 * b + 100 * c + 50 * d)) / 10;
    f = (1000 - (a + 500 * b + 100 * c + 50 * d + 10 * e)) / 5;
    g = (1000 - (a + 500 * b + 100 * c + 50 * d + 10 * e + 5 * f)) / 1;
    cout << b + c + d + e + f + g << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int a, b, c, d, e, f, g;
  while (1) {
    cin >> a;
    if (a == 0)
      break;
    b = (1000 - a) / 500;
    c = (1000 - (a + 500 * b)) / 100;
    d = (1000 - (a + 500 * b + 100 * c)) / 50;
    e = (1000 - (a + 500 * b + 100 * c + 50 * d)) / 10;
    f = (1000 - (a + 500 * b + 100 * c + 50 * d + 10 * e)) / 5;
    g = (1000 - (a + 500 * b + 100 * c + 50 * d + 10 * e + 5 * f)) / 1;
    cout << b + c + d + e + f + g << endl;
  }
  return 0;
}
Problem p00444 (C++)

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

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    n = 1000 - n;
    int ans = 0, i;
    int w[6] = {500, 100, 50, 10, 5, 1};
    for (i = 0; i <= 6; i++) {
      ans = ans + n / w[i];
      n = n - n / w[i] * w[i];
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    n = 1000 - n;
    int ans = 0, i;
    int w[6] = {500, 100, 50, 10, 5, 1};
    for (i = 0; i <= 5; i++) {
      ans = ans + n / w[i];
      n = n - n / w[i] * w[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00445 (C++)

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

int main() {
  char x[10005];
  while (scanf("%s", &x)) {
    int k = 0, JOI = 0, IOI = 0;
    while (x[k + 2] != '\0') {
      if (x[k + 1] == 'O' && x[k + 2] == 'I') {
        if (x[k] == 'J')
          JOI++;
        if (x[k] == 'I')
          IOI++;
      }
      k++;
    }
    printf("%d\n%d\n", JOI, IOI);
    for (int i = 0; i < 10005; i++)
      x[i] = '\0';
  }
}

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

int main() {
  char x[10005];
  while (scanf("%s", &x) != EOF) {
    int k = 0, JOI = 0, IOI = 0;
    while (x[k + 2] != '\0') {
      if (x[k + 1] == 'O' && x[k + 2] == 'I') {
        if (x[k] == 'J')
          JOI++;
        if (x[k] == 'I')
          IOI++;
      }
      k++;
    }
    printf("%d\n%d\n", JOI, IOI);
    for (int i = 0; i < 10005; i++)
      x[i] = '\0';
  }
}
Problem p00445 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  char s[10010] = {0};
  while (cin >> s) {
    int J = 0;
    int I = 0;
    int len = 0;
    while (1) {
      if (s[len] == 0) {
        break;
      }
    }
    for (int i = 0; i < len - 2; i++) {
      if (s[i] == 'J' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        J++;
        i++;
      } else if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        I++;
        i++;
      }
    }
    cout << J << endl << I << endl;
    for (int i = 0; i < 10010; i++) {
      s[i] = 0;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  char s[10010] = {0};
  while (cin >> s) {
    int J = 0;
    int I = 0;
    int len = 0;
    while (1) {
      if (s[len] == 0) {
        break;
      }
      len++;
    }
    for (int i = 0; i < len - 2; i++) {
      if (s[i] == 'J' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        J++;
        i++;
      } else if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        I++;
        i++;
      }
    }
    cout << J << endl << I << endl;
    for (int i = 0; i < 10010; i++) {
      s[i] = 0;
    }
  }
  return 0;
}
Problem p00445 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  while (true) {
    string s;
    cin >> s;
    // if(cin.eof()==false)break;
    int a = 0, b = 0;
    for (int i = 0; i < s.size() - 1; i++) {
      if (s[i] == 'J' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        a += 1;
      }
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        b += 1;
      }
    }
    cout << a << endl << b << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  string s;
  while (cin >> s) {
    int a = 0, b = 0;
    for (int i = 0; i < s.size() - 1; i++) {
      if (s[i] == 'J' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        a += 1;
      }
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        b += 1;
      }
    }
    cout << a << endl << b << endl;
  }
  return 0;
}
Problem p00445 (C++)

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

char ary[10010];
int main() {
  int cnt, cnj, cni;
  while (true) {
    cin >> ary;
    if (ary[0] == (-1))
      break;
    cnj = cni = 0;
    cnt = strlen(ary);
    for (int i = 2; i < cnt; i++) {
      if (ary[i] == 'I' && ary[i - 1] == 'O') {
        if (ary[i - 2] == 'J')
          cnj++;
        else if (ary[i - 2] == 'I')
          cni++;
      }
    }
    cout << cnj << endl << cni << endl;
  }
  return 0;
}

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

char ary[10010];
int main() {
  int cnt, cnj, cni;
  while (true) {
    cin >> ary;
    if (cin.eof())
      break;
    cnj = cni = 0;
    cnt = strlen(ary);
    for (int i = 2; i < cnt; i++) {
      if (ary[i] == 'I' && ary[i - 1] == 'O') {
        if (ary[i - 2] == 'J')
          cnj++;
        else if (ary[i - 2] == 'I')
          cni++;
      }
    }
    cout << cnj << endl << cni << endl;
  }
  return 0;
}
Problem p00445 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
using namespace std;
char s[10002];
int J, I;
int main() {
  while (1) {
    scanf("%s", s);
    if (s[0] == 0xff)
      return 0;
    J = 0;
    I = 0;
    for (int i = 0; i < strlen(s) - 2; i++) {
      if (s[i] == 'J' && s[i + 1] == 'O' && s[i + 2] == 'I')
        J++;
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I')
        I++;
    }
    printf("%d\n%d\n", J, I);
  }
}

Fixed submission:
#include <cstdio>
#include <cstring>
using namespace std;
char s[10002];
int J, I;
int main() {
  while (scanf("%s", s) != EOF) {
    J = 0;
    I = 0;
    for (int i = 0; i < strlen(s) - 2; i++) {
      if (s[i] == 'J' && s[i + 1] == 'O' && s[i + 2] == 'I')
        J++;
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I')
        I++;
    }
    printf("%d\n%d\n", J, I);
  }
}
Problem p00445 (C++)

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

using namespace std;

int main() {
  for (;;) {
    char str[1000];
    int joi = 0, ioi = 0, temp;

    if (scanf("%s", str) == EOF) {
      break;
    }

    // JOIツ板サツ津ィ
    for (int i = 2; str[i] != '\0'; i++) {
      if (str[i - 2] == 'J' && str[i - 1] == 'O' && str[i] == 'I') {
        joi++;
      }
    }
    for (int i = 0; str[i] != '\0'; i++) {
      if (str[i - 2] == 'I' && str[i - 1] == 'O' && str[i] == 'I') {
        ioi++;
      }
    }

    cout << joi << endl;
    cout << ioi << endl;
  }
}

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

using namespace std;

int main() {
  for (;;) {
    char str[10001];
    int joi = 0, ioi = 0, temp;

    if (scanf("%s", str) == EOF) {
      break;
    }

    // JOIツ板サツ津ィ
    for (int i = 2; str[i] != '\0'; i++) {
      if (str[i - 2] == 'J' && str[i - 1] == 'O' && str[i] == 'I') {
        joi++;
      }
    }
    for (int i = 0; str[i] != '\0'; i++) {
      if (str[i - 2] == 'I' && str[i - 1] == 'O' && str[i] == 'I') {
        ioi++;
      }
    }

    cout << joi << endl;
    cout << ioi << endl;
  }
}
Problem p00445 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

int main() {

  while (true) {

    string s;
    cin >> s;
    if (s == "EOF")
      break;

    int n = s.size();
    int a = 0;
    int b = 0;

    rep(i, n - 2) {
      if (s[i] == 'J' && s[i + 1] == 'O' && s[i + 2] == 'I')
        a++;
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I')
        b++;
    }

    cout << a << endl;
    cout << b << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

int main() {

  string s;
  while (cin >> s) {

    int n = s.size();
    int a = 0;
    int b = 0;

    rep(i, n - 2) {
      if (s[i] == 'J' && s[i + 1] == 'O' && s[i + 2] == 'I')
        a++;
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I')
        b++;
    }

    cout << a << endl;
    cout << b << endl;
  }

  return 0;
}
Problem p00445 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://onlinejudge.u-aizu.ac.jp/challenges/sources/JOI/Prelim/0522?year=2008

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;

  while (true) {
    cin >> s;
    if (s.empty())
      return 0;

    int joi = 0, ioi = 0;
    for (int i = 0; i < s.size() - 2; ++i) {
      if (s[i + 1] == 'O' and s[i + 2] == 'I') {
        if (s[i] == 'J') {
          joi++;
        } else if (s[i] == 'I') {
          ioi++;
        }
      }
    }

    cout << joi << endl;
    cout << ioi << endl;
  }
}

/*
 * substrでJOI,IOIをそれぞれ探したらTLEだったので
 * OIが共通なことを利用した。
 *
 */


Fixed submission:
// https://onlinejudge.u-aizu.ac.jp/challenges/sources/JOI/Prelim/0522?year=2008

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;

  while (cin >> s) {
    int joi = 0, ioi = 0;
    for (int i = 0; i < s.size() - 2; ++i) {
      if (s[i + 1] == 'O' and s[i + 2] == 'I') {
        if (s[i] == 'J') {
          joi++;
        } else if (s[i] == 'I') {
          ioi++;
        }
      }
    }

    cout << joi << endl;
    cout << ioi << endl;
  }
}

/*
 * substrでJOI,IOIをそれぞれ探したらTLEだったので
 * OIが共通なことを利用した。
 *
 */
Problem p00445 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
int i;
int ioi = 0, joi = 0;
std::string s;
#define I if (s[i] == 'I')
#define J if (s[i] == 'J')
int main() {
  while (true) {
    std::cin >> s;
    if (s == "EOF" || s == "eof")
      break;
    for (i = 0; i < s.size(); i++) {
      I {
        if (s[i + 1] == 'O') {
          if (s[i + 2] == 'I') {
            ioi++;
          }
        }
      }
      J {
        if (s[i + 1] == 'O') {
          if (s[i + 2] == 'I') {
            joi++;
          }
        }
      }
    }
    std::cout << joi << std::endl;
    std::cout << ioi << std::endl;
    ioi = 0, joi = 0;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
int i;
int ioi = 0, joi = 0;
std::string s;
#define I if (s[i] == 'I')
#define J if (s[i] == 'J')
int main() {
  while (std::cin >> s) {
    for (i = 0; i < s.size(); i++) {
      I {
        if (s[i + 1] == 'O') {
          if (s[i + 2] == 'I') {
            ioi++;
          }
        }
      }
      J {
        if (s[i + 1] == 'O') {
          if (s[i + 2] == 'I') {
            joi++;
          }
        }
      }
    }
    std::cout << joi << std::endl;
    std::cout << ioi << std::endl;
    ioi = 0, joi = 0;
  }
}
Problem p00445 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int I = 0, J = 0;
  char d[10000];
  while (scanf("%s", d) && d[0]) {
    for (int i = 0; i < 10000; i++)
      if (d[i + 2] == 0)
        break;
      else {
        if (d[i + 1] == 'O' && d[i + 2] == 'I') {
          if (d[i] == 'J')
            J++;
          if (d[i] == 'I')
            I++;
        }
      }
    printf("%d\n%d\n", J, I);
    I = 0, J = 0;
  }
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int I = 0, J = 0;
  char d[10000];
  while (cin >> d) {
    for (int i = 0; i < 10000; i++)
      if (d[i + 2] == 0)
        break;
      else {
        if (d[i + 1] == 'O' && d[i + 2] == 'I') {
          if (d[i] == 'J')
            J++;
          if (d[i] == 'I')
            I++;
        }
      }
    printf("%d\n%d\n", J, I);
    I = 0, J = 0;
  }
}
Problem p00445 (C++)

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

int main() {
  while (true) {
    string line;
    cin >> line;
    if (line[0] == 'EOF') {
      break;
    }
    int joi = 0, ioi = 0;

    for (int i = 0; i < line.size() - 2; i++) {
      if (line[i + 1] == 'O' && line[i + 2] == 'I') {
        if (line[i] == 'J') {
          joi++;
        } else if (line[i] == 'I') {
          ioi++;
        }
      }
    }
    cout << joi << endl << ioi << endl;
  }
  return 0;
}

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

int main() {
  while (true) {
    string line;
    cin >> line;
    if (!line.size()) {
      break;
    }
    int joi = 0, ioi = 0;

    for (int i = 0; i < line.size() - 2; i++) {
      if (line[i + 1] == 'O' && line[i + 2] == 'I') {
        if (line[i] == 'J') {
          joi++;
        } else if (line[i] == 'I') {
          ioi++;
        }
      }
    }
    cout << joi << endl << ioi << endl;
  }
  return 0;
}
Problem p00446 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
using namespace std;
int main(void) {
  int n, i, a[10], e[201], c, x, y, flg;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 1; i <= n * 2; i++)
      e[i] = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (i = 0; i < n; i++) {
      e[a[i]] = 1;
    }
    x = n;
    y = n;
    flg = 0;
    while (1) {
      for (i = 1; i <= n * 2; i++) {
        if (flg == 0 && e[i] == 1) {
          x--;
          e[i] = 2;
          flg = 1;
        } else if (flg == 1 && e[i] == 0) {
          y--;
          e[i] = 2;
          flg = 0;
        }
        if (x == 0 || y == 0)
          break;
      }
      if (flg == 0)
        flg = 1;
      else
        flg = 0;
      if (x == 0 || y == 0)
        break;
    }
    printf("%d\n", y);
    printf("%d\n", x);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int main(void) {
  int n, i, a[101], e[201], c, x, y, flg;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 1; i <= n * 2; i++)
      e[i] = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (i = 0; i < n; i++) {
      e[a[i]] = 1;
    }
    x = n;
    y = n;
    flg = 0;
    while (1) {
      for (i = 1; i <= n * 2; i++) {
        if (flg == 0 && e[i] == 1) {
          x--;
          e[i] = 2;
          flg = 1;
        } else if (flg == 1 && e[i] == 0) {
          y--;
          e[i] = 2;
          flg = 0;
        }
        if (x == 0 || y == 0)
          break;
      }
      if (flg == 0)
        flg = 1;
      else
        flg = 0;
      if (x == 0 || y == 0)
        break;
    }
    printf("%d\n", y);
    printf("%d\n", x);
  }
  return 0;
}
Problem p00446 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int N, ba, d, c1, c2;
  int tc, hc, flag;
  int taro[100], hana[100];
  while (1) {
    int F[101] = {0};
    ba = 0;
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> d;
      F[d] = 1;
    }
    c1 = 0;
    c2 = 0;
    for (int i = 1; i <= N * 2; i++) {
      if (F[i] == 1) {
        taro[c1] = i;
        c1++;
      } else if (F[i] == 0) {
        hana[c2] = i;
        c2++;
      }
    }

    /////////////////////////////////////////////
    tc = N;
    hc = N;
    flag = 0;
    while (1) {
      for (int i = 0; i < N; i++) {
        if (ba < taro[i]) {
          ba = taro[i];
          flag = 1;
          taro[i] = 0;
          tc--;
          break;
        }
      }
      if (tc == 0)
        break;
      if (flag == 0)
        ba = 0;
      flag = 0;

      for (int i = 0; i < N; i++) {
        if (ba < hana[i]) {
          ba = hana[i];
          flag = 1;
          hana[i] = 0;
          hc--;
          break;
        }
      }
      if (hc == 0)
        break;
      if (flag == 0)
        ba = 0;
      flag = 0;
    }
    cout << hc << endl << tc << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int N, ba, d, c1, c2;
  int tc, hc, flag;
  int taro[100], hana[100];
  while (1) {
    int F[201] = {0};
    ba = 0;
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> d;
      F[d] = 1;
    }
    c1 = 0;
    c2 = 0;
    for (int i = 1; i <= N * 2; i++) {
      if (F[i] == 1) {
        taro[c1] = i;
        c1++;
      } else if (F[i] == 0) {
        hana[c2] = i;
        c2++;
      }
    }

    /////////////////////////////////////////////
    tc = N;
    hc = N;
    flag = 0;
    while (1) {
      for (int i = 0; i < N; i++) {
        if (ba < taro[i]) {
          ba = taro[i];
          flag = 1;
          taro[i] = 0;
          tc--;
          break;
        }
      }
      if (tc == 0)
        break;
      if (flag == 0)
        ba = 0;
      flag = 0;

      for (int i = 0; i < N; i++) {
        if (ba < hana[i]) {
          ba = hana[i];
          flag = 1;
          hana[i] = 0;
          hc--;
          break;
        }
      }
      if (hc == 0)
        break;
      if (flag == 0)
        ba = 0;
      flag = 0;
    }
    cout << hc << endl << tc << endl;
  }
  return 0;
}
Problem p00446 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
using namespace std;
int main() {
  while (1) {
    int n, x;
    int i, j;
    int tarou[110] = {0}, hanako[110] = {0};
    int field = 0;
    int flg1 = 0, flg2 = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &x);
      tarou[x - 1] = x;
    }
    for (i = 0; i < n * 2; i++) {
      if (tarou[i] == 0)
        hanako[i] = i + 1;
    }

    sort(tarou, tarou + n * 2, greater<int>());
    sort(hanako, hanako + n * 2, greater<int>());
    sort(tarou, tarou + n);
    sort(hanako, hanako + n);

    while (1) {
      // 太郎の番
      for (j = 0; j < n; j++) {
        if (field < tarou[j]) {
          field = tarou[j];
          tarou[j] = -1;
          flg1++;
          goto next;
        }
      }
      field = 0;
    next:
      if (flg1 == n || flg2 == n)
        break;
      for (j = 0; j < n; j++) {
        if (field < hanako[j]) {
          field = hanako[j];
          hanako[j] = -1;
          flg2++;
          goto next2;
        }
      }
      field = 0;
    next2:
      if (flg1 == n || flg2 == n)
        break;
    }
    printf("%d\n%d\n", n - flg2, n - flg1);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
using namespace std;
int main() {
  while (1) {
    int n, x;
    int i, j;
    int tarou[300] = {0}, hanako[300] = {0};
    int field = 0;
    int flg1 = 0, flg2 = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &x);
      tarou[x - 1] = x;
    }
    for (i = 0; i < n * 2; i++) {
      if (tarou[i] == 0)
        hanako[i] = i + 1;
    }

    sort(tarou, tarou + n * 2, greater<int>());
    sort(hanako, hanako + n * 2, greater<int>());
    sort(tarou, tarou + n);
    sort(hanako, hanako + n);

    while (1) {
      // 太郎の番
      for (j = 0; j < n; j++) {
        if (field < tarou[j]) {
          field = tarou[j];
          tarou[j] = -1;
          flg1++;
          goto next;
        }
      }
      field = 0;
    next:
      if (flg1 == n || flg2 == n)
        break;
      for (j = 0; j < n; j++) {
        if (field < hanako[j]) {
          field = hanako[j];
          hanako[j] = -1;
          flg2++;
          goto next2;
        }
      }
      field = 0;
    next2:
      if (flg1 == n || flg2 == n)
        break;
    }
    printf("%d\n%d\n", n - flg2, n - flg1);
  }

  return 0;
}
Problem p00446 (C++)

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

int main() {
  int N;
  while (cin >> N, N) {
    vector<int> card[2];
    int ba = -1, in;
    bool latte[101] = {}, turn = 0;
    for (int i = 0; i < N; i++) {
      cin >> in;
      latte[in] = true;
    }
    for (int i = 1; i <= 2 * N; i++) {
      card[!latte[i]].push_back(i);
    }
    sort(card[0].begin(), card[0].end());
    sort(card[1].begin(), card[1].end());
    while (card[0].size() && card[1].size()) {
      vector<int>::iterator itr =
          upper_bound(card[turn].begin(), card[turn].end(), ba);
      if (itr == card[turn].end())
        ba = -1;
      else {
        ba = *itr;
        card[turn].erase(itr);
      }
      turn = !turn;
    }
    printf("%d\n%d\n", card[1].size(), card[0].size());
  }
  return 0;
}

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

int main() {
  int N;
  while (cin >> N, N) {
    vector<int> card[2];
    int ba = -1, in;
    bool latte[201] = {}, turn = 0;
    for (int i = 0; i < N; i++) {
      cin >> in;
      latte[in] = true;
    }
    for (int i = 1; i <= 2 * N; i++) {
      card[!latte[i]].push_back(i);
    }
    sort(card[0].begin(), card[0].end());
    sort(card[1].begin(), card[1].end());
    while (card[0].size() && card[1].size()) {
      vector<int>::iterator itr =
          upper_bound(card[turn].begin(), card[turn].end(), ba);
      if (itr == card[turn].end())
        ba = -1;
      else {
        ba = *itr;
        card[turn].erase(itr);
      }
      turn = !turn;
    }
    printf("%d\n%d\n", card[1].size(), card[0].size());
  }
  return 0;
}
Problem p00446 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int c[101] = {0}, ba = 0, n, i, a, t = 0, o;
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (i = 1; i <= n * 2; i++) {
      c[i] = 0;
    }
    ba = 0;
    t = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &a);
      c[a] = 1;
    }
    while (1) {
      if (ba == 0) {
        for (i = 1; i <= n * 2; i++) {
          if (c[i] == (t + 1) % 2) {
            ba = i;
            c[i] = 2;
            break;
          }
        }
      } else {
        o = 0;
        for (i = 1; i <= n * 2; i++) {
          if (c[i] == (t + 1) % 2 && ba < i) {
            o = 1;
            ba = i;
            c[i] = 2;
            break;
          }
        }
        if (o == 0) {
          ba = 0;
        }
      }
      o = 0;
      for (i = 1; i <= n * 2; i++) {
        if (c[i] == 1) {
          o = 2;
        }
      }
      if (o == 0) {
        break;
      }
      o = 1;
      for (i = 1; i <= n * 2; i++) {
        if (c[i] == 0) {
          o = 2;
        }
      }
      if (o == 1) {
        break;
      }
      t++;
    }
    int cnt = 0;
    if (o == 0) {
      for (i = 1; i <= n * 2; i++) {
        if (c[i] == o) {
          cnt++;
        }
      }
      printf("%d\n", cnt);
      printf("0\n");
    } else {
      for (i = 1; i <= n * 2; i++) {
        if (c[i] == o) {
          cnt++;
        }
      }
      printf("0\n");
      printf("%d\n", cnt);
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  int c[201] = {0}, ba = 0, n, i, a, t = 0, o;
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (i = 1; i <= n * 2; i++) {
      c[i] = 0;
    }
    ba = 0;
    t = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &a);
      c[a] = 1;
    }
    while (1) {
      if (ba == 0) {
        for (i = 1; i <= n * 2; i++) {
          if (c[i] == (t + 1) % 2) {
            ba = i;
            c[i] = 2;
            break;
          }
        }
      } else {
        o = 0;
        for (i = 1; i <= n * 2; i++) {
          if (c[i] == (t + 1) % 2 && ba < i) {
            o = 1;
            ba = i;
            c[i] = 2;
            break;
          }
        }
        if (o == 0) {
          ba = 0;
        }
      }
      o = 0;
      for (i = 1; i <= n * 2; i++) {
        if (c[i] == 1) {
          o = 2;
        }
      }
      if (o == 0) {
        break;
      }
      o = 1;
      for (i = 1; i <= n * 2; i++) {
        if (c[i] == 0) {
          o = 2;
        }
      }
      if (o == 1) {
        break;
      }
      t++;
    }
    int cnt = 0;
    if (o == 0) {
      for (i = 1; i <= n * 2; i++) {
        if (c[i] == o) {
          cnt++;
        }
      }
      printf("%d\n", cnt);
      printf("0\n");
    } else {
      for (i = 1; i <= n * 2; i++) {
        if (c[i] == o) {
          cnt++;
        }
      }
      printf("0\n");
      printf("%d\n", cnt);
    }
  }
  return 0;
}