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

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

const int N = 100;
int bit[N + 10];

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= N) {
    bit[i] += x;
    i += i & -i;
  }
}

vector<int> compress(vector<int> v) {
  map<int, int> mp;
  for (int i = 0; i < v.size(); i++)
    mp[i] = 0;

  int id = 1;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
    it->second = id++;
  }

  for (int i = 0; i < v.size(); i++)
    v[i] = mp[v[i]];

  return v;
}

int main() {
  int n;
  while (cin >> n && n) {
    memset(bit, 0, sizeof(bit));

    vector<int> v(n);
    for (int i = 0; i < n; i++)
      cin >> v[i];

    v = compress(v);

    int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      ans += sum(v[i] - 1);
      add(v[i], 1);
    }

    cout << ans << endl;
  }

  return 0;
}

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

const int N = 100;
int bit[N + 10];

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= N) {
    bit[i] += x;
    i += i & -i;
  }
}

vector<int> compress(vector<int> v) {
  map<int, int> mp;
  for (int i = 0; i < v.size(); i++)
    mp[v[i]] = 0;

  int id = 1;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
    it->second = id++;
  }

  for (int i = 0; i < v.size(); i++)
    v[i] = mp[v[i]];

  return v;
}

int main() {
  int n;
  while (cin >> n && n) {
    memset(bit, 0, sizeof(bit));

    vector<int> v(n);
    for (int i = 0; i < n; i++)
      cin >> v[i];

    v = compress(v);

    int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      ans += sum(v[i] - 1);
      add(v[i], 1);
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00167 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 10000010
int main() {
  int n;
  int list[MAX];
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> list[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n - i - 1; j++) {
        if (list[j] > list[j + 1]) {
          swap(list[j], list[j + 1]);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 110
int main() {
  int n;
  int list[MAX];
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> list[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n - i - 1; j++) {
        if (list[j] > list[j + 1]) {
          swap(list[j], list[j + 1]);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00167 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int n[1000001] = {};
int main() {

  int N;

  int c = 0;
  while (1) {
    scanf("%d", &N);

    int c = 0;

    for (int i = 0; i < N; i++) {
      scanf("%d", &n[i]);
    }

    for (int i = 0; i < N; i++)
      for (int j = N - 1; j > i; j--) {
        if (n[j] < n[j - 1]) {
          int T = n[j];
          n[j] = n[j - 1];
          n[j - 1] = T;
          c++;
        }
      }
    printf("%d\n", c);
    for (int i = 0; i < N; i++)
      n[i] = 0;
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int n[1000001] = {};
int main() {

  int N;

  int c = 0;
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    int c = 0;

    for (int i = 0; i < N; i++) {
      scanf("%d", &n[i]);
    }

    for (int i = 0; i < N; i++)
      for (int j = N - 1; j > i; j--) {
        if (n[j] < n[j - 1]) {
          int T = n[j];
          n[j] = n[j - 1];
          n[j - 1] = T;
          c++;
        }
      }
    printf("%d\n", c);
    for (int i = 0; i < N; i++)
      n[i] = 0;
  }
  return 0;
}
Problem p00168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int dp[31] = {1, 1, 2, 4};
    int n;
    cin >> n;
    for (int i = 4; i <= n; ++i) {
      dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
    }
    cout << ((dp[n] - 1) / 3650) + 1 << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int dp[31] = {1, 1, 2, 4};
    int n;
    cin >> n;
    if (!n)
      return 0;
    for (int i = 4; i <= n; ++i) {
      dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
    }
    cout << ((dp[n] - 1) / 3650) + 1 << endl;
  }
}
Problem p00168 (C++)

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

using namespace std;

int n;
int memo[31];

int dfs(int num) {

  int sum = 0;

  if (memo[num] != -1) {
    return memo[num];
  }

  if (num == n) {
    return 1;
  }

  if (num < n)
    sum += dfs(num + 1);

  if (num < n)
    sum += dfs(num + 2);

  if (num < n)
    sum += dfs(num + 3);

  memo[num] = sum;

  return sum;
}

int main() {

  while (1) {
    cin >> n;

    for (int i = 0; i <= 31; i++) {
      memo[i] = -1;
    }

    if (n == 0) {
      break;
    }

    cout << dfs(0) / 10 / 365 + 1 << endl;
  }
}

Fixed submission:
#include <iostream>

using namespace std;

int n;
int memo[366];

int dfs(int num) {

  int sum = 0;

  if (memo[num] != -1) {
    return memo[num];
  }

  if (num == n) {
    return 1;
  }

  if (num < n)
    sum += dfs(num + 1);

  if (num < n)
    sum += dfs(num + 2);

  if (num < n)
    sum += dfs(num + 3);

  memo[num] = sum;

  return sum;
}

int main() {

  while (1) {
    cin >> n;

    for (int i = 0; i <= 31; i++) {
      memo[i] = -1;
    }

    if (n == 0) {
      break;
    }

    cout << dfs(0) / 10 / 365 + 1 << endl;
  }
}
Problem p00169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  while (getline(cin, s), s != "0") {
    stringstream ss(s);
    int x = 0, i, ans, k;
    int si = 0;
    vector<int> v;
    v.clear();
    v.push_back(0);

    while (ss >> x) {
      si = v.size();
      vector<int> n;
      for (i = 0; i < si; i++) {

        if (x == 1) {
          k = v[i];
          n.push_back(k + 11);
          v[i]++;
        } else if (x > 10) {
          v[i] += 10;
        } else {
          v[i] += x;
        }
      }
      for (i = 0; i < n.size(); i++)
        v.push_back(n[i]);
      n.clear();
    }

    sort(v.begin(), v.end());
    ans = 0;
    for (i = 0; i < v.size(); i++) {
      if (v[i] <= 21)
        ans = v[i];
    }
    cout << ans << endl;
    s = "";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  while (getline(cin, s), s != "0") {
    stringstream ss(s);
    int x = 0, i, ans, k;
    int si = 0;
    vector<int> v;
    v.clear();
    v.push_back(0);

    while (ss >> x) {
      si = v.size();
      vector<int> n;
      for (i = 0; i < si; i++) {

        if (x == 1) {
          k = v[i];
          n.push_back(k + 11);
          v[i]++;
        } else if (x > 10) {
          v[i] += 10;
        } else {
          v[i] += x;
        }
      }
      for (i = 0; i < n.size(); i++)
        v.push_back(n[i]);
      n.clear();

      sort(v.begin(), v.end());
      v.erase(unique(v.begin(), v.end()), v.end());
      v.erase(lower_bound(v.begin(), v.end(), 22), v.end());
    }

    sort(v.begin(), v.end());
    ans = 0;
    for (i = 0; i < v.size(); i++) {
      if (v[i] <= 21)
        ans = v[i];
    }
    cout << ans << endl;
    s = "";
  }
  return 0;
}
Problem p00169 (C++)

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

using namespace std;

int rec(int sum, int one) {
  if (one == 0 && sum > 21)
    return -1;
  else if (one == 0 && sum <= 21)
    return sum;

  int res = rec(sum + 1, one - 1);
  res = max(res, rec(sum + 11, one - 1));

  return res;
}

bool solve(string &s) {
  int sum = 0, one = 0;
  int index = 0;

  s += " ";
  while (index < s.size()) {
    int num = 0;

    while (index < s.size()) {
      if ('0' <= s[index] && s[index] <= '9') {
        num *= 10;
        num += (int)(s[index] - '0');
        index++;
      } else
        break;
    }

    if (num == 1)
      one++;
    else if (num / 10 == 1)
      sum += 10;
    else
      sum += num;
    index++;
  }

  if (sum == 0 && one == 0)
    return false;

  if (sum > 21) {
    cout << 0 << endl;
    return true;
  } else if (one == 0) {
    cout << sum << endl;
    return true;
  } else {

    int ans = rec(sum, one);
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << 0 << endl;
    return true;
  }
}

int main() {
  string s;

  while (true) {
    getline(cin, s);
    if (!solve(s))
      break;
  }

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int rec(int sum, int one) {
  if (sum > 21)
    return -1;
  else if (one == 0 && sum > 21)
    return -1;
  else if (one == 0 && sum <= 21)
    return sum;

  int res = rec(sum + 1, one - 1);
  res = max(res, rec(sum + 11, one - 1));

  return res;
}

bool solve(string &s) {
  int sum = 0, one = 0;
  int index = 0;

  s += " ";
  while (index < s.size()) {
    int num = 0;

    while (index < s.size()) {
      if ('0' <= s[index] && s[index] <= '9') {
        num *= 10;
        num += (int)(s[index] - '0');
        index++;
      } else
        break;
    }

    if (num == 1)
      one++;
    else if (num / 10 == 1)
      sum += 10;
    else
      sum += num;
    index++;
  }

  if (sum == 0 && one == 0)
    return false;

  if (sum > 21) {
    cout << 0 << endl;
    return true;
  } else if (one == 0) {
    cout << sum << endl;
    return true;
  } else {

    int ans = rec(sum, one);
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << 0 << endl;
    return true;
  }
}

int main() {
  string s;

  while (true) {
    getline(cin, s);
    if (!solve(s))
      break;
  }

  return 0;
}
Problem p00169 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int rec(int sum, const vector<int> &v, int k) {
  if (k == v.size())
    return sum <= 21 ? sum : 0;
  if (v[k] == 1)
    return max(rec(sum + 1, v, k + 1), rec(sum + 11, v, k + 1));
  else
    return rec(sum + v[k], v, k + 1);
}

int main() {
  string s;
  while (getline(cin, s) && s != "0") {
    stringstream ss(s);
    int a;
    vector<int> v;
    while (ss >> a) {
      v.push_back(min(a, 10));
    }
    cout << rec(0, v, 0) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int rec(int sum, const vector<int> &v, int k) {
  if (v.size() > 21)
    return 0;
  if (k == v.size())
    return sum <= 21 ? sum : 0;
  if (v[k] == 1)
    return max(rec(sum + 1, v, k + 1), rec(sum + 11, v, k + 1));
  else
    return rec(sum + v[k], v, k + 1);
}

int main() {
  string s;
  while (getline(cin, s) && s != "0") {
    stringstream ss(s);
    int a;
    vector<int> v;
    while (ss >> a) {
      v.push_back(min(a, 10));
    }
    cout << rec(0, v, 0) << endl;
  }
  return 0;
}
Problem p00169 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  string S;
  while (getline(cin, S)) {
    priority_queue<int, vector<int>, greater<int>> pq[2];
    pq[0].push(0);
    int cnt = 0;
    for (int i = 0; i < S.size(); i++) {
      if (i == 0 || S[i] == ' ') {
        int n = stoi(&S[i]);
        if (n == 0)
          return 0;
        while (!pq[cnt & 1].empty()) {
          int pq_c = pq[cnt & 1].top();
          pq[cnt & 1].pop();
          if (n == 1)
            pq[(cnt + 1) & 1].push(pq_c + 1), pq[(cnt + 1) & 1].push(pq_c + 11);
          else if (n < 10)
            pq[(cnt + 1) & 1].push(pq_c + n);
          else
            pq[(cnt + 1) & 1].push(pq_c + 10);
        }
        cnt++;
      }
    }
    int ans = 0;
    while (!pq[cnt & 1].empty()) {
      if (pq[cnt & 1].top() <= 21)
        ans = max(ans, pq[cnt & 1].top());
      pq[cnt & 1].pop();
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  string S;
  while (getline(cin, S)) {
    priority_queue<int, vector<int>, greater<int>> pq[2];
    pq[0].push(0);
    int cnt = 0;
    for (int i = 0; i < S.size(); i++) {
      if (i == 0 || S[i] == ' ') {
        int n = stoi(&S[i]);
        if (n == 0)
          return 0;
        while (!pq[cnt & 1].empty()) {
          int pq_c = pq[cnt & 1].top();
          pq[cnt & 1].pop();
          if (pq_c > 21)
            continue;
          if (n == 1)
            pq[(cnt + 1) & 1].push(pq_c + 1), pq[(cnt + 1) & 1].push(pq_c + 11);
          else if (n < 10)
            pq[(cnt + 1) & 1].push(pq_c + n);
          else
            pq[(cnt + 1) & 1].push(pq_c + 10);
        }
        cnt++;
      }
    }
    int ans = 0;
    while (!pq[cnt & 1].empty()) {
      if (pq[cnt & 1].top() <= 21)
        ans = max(ans, pq[cnt & 1].top());
      pq[cnt & 1].pop();
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int power(int n) {
  int num = 1;
  for (int i = 0; i < n; i++)
    num *= 10;
  return num;
}
int serch(int sum, int n) {
  if (sum > 21)
    return 0;
  else if (n == 0)
    return sum;
  else
    return max(serch(sum + 11, n - 1), serch(sum + 1, n - 1));
}
int main() {
  string str;
  while (getline(cin, str)) {
    if (str[0] == '0')
      break;
    int card[20] = {}, co = 0, point = 0;
    for (int i = 0;; i++) {
      if (str[i] == ' ' || str[i] == '\0') {
        for (int j = point; j < i; j++)
          card[co] += (str[j] - '0') * power(i - 1 - j);
        co++;
        point = i + 1;
      }
      if (str[i] == '\0')
        break;
    }
    int cou = 0, sum = 0;
    for (int i = 0; i < co; i++) {
      if (card[i] == 1)
        cou++;
      else if (card[i] < 10)
        sum += card[i];
      else
        sum += 10;
    }
    cout << serch(sum, cou) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int power(int n) {
  int num = 1;
  for (int i = 0; i < n; i++)
    num *= 10;
  return num;
}
int serch(int sum, int n) {
  if (sum > 21)
    return 0;
  else if (n == 0)
    return sum;
  else
    return max(serch(sum + 11, n - 1), serch(sum + 1, n - 1));
}
int main() {
  string str;
  while (getline(cin, str)) {
    if (str[0] == '0')
      break;
    int card[1000] = {}, co = 0, point = 0;
    for (int i = 0;; i++) {
      if (str[i] == ' ' || str[i] == '\0') {
        for (int j = point; j < i; j++)
          card[co] += (str[j] - '0') * power(i - 1 - j);
        co++;
        point = i + 1;
      }
      if (str[i] == '\0')
        break;
    }
    int cou = 0, sum = 0;
    for (int i = 0; i < co; i++) {
      if (card[i] == 1)
        cou++;
      else if (card[i] < 10)
        sum += card[i];
      else
        sum += 10;
    }
    cout << serch(sum, cou) << endl;
  }
}
Problem p00170 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const double EPS = 1e-6;

int main(void) {
  int N;
  while (cin >> N, N) {
    ;
    vector<string> F(N);
    VI W(N), S(N);
    for (int i = 0; i < N; i++) {
      cin >> F[i] >> W[i] >> S[i];
    }

    VI p(N);
    iota(p.begin(), p.end(), 0);

    double ans = 1e+8;
    VI best_p(p.begin(), p.end());

    do {
      double g = 0, gt = 0;
      for (int i = 0; i < N; i++) {
        g += (i + 1) * W[p[i]];
        gt += W[p[i]];
      }
      g /= gt;

      bool flg = true;
      for (int i = N - 1; i >= 0; i--) {
        int sum = 0;
        for (int j = i + 1; j < N; j++) {
          sum += W[p[j]];
        }
        flg &= sum <= S[p[i]];
      }

      if (flg && g < ans + EPS) {
        ans = g;
        swap(best_p, p);
      }
    } while (next_permutation(p.begin(), p.end()));

    for (auto i : best_p) {
      cout << F[i] << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const double EPS = 1e-6;

int main(void) {
  int N;
  while (cin >> N, N) {
    ;
    vector<string> F(N);
    VI W(N), S(N);
    for (int i = 0; i < N; i++) {
      cin >> F[i] >> W[i] >> S[i];
    }

    VI p(N);
    iota(p.begin(), p.end(), 0);

    double ans = 1e+8;
    VI best_p(p.begin(), p.end());

    do {
      double g = 0, gt = 0;
      for (int i = 0; i < N; i++) {
        g += (i + 1) * W[p[i]];
        gt += W[p[i]];
      }
      g /= gt;

      if (g - EPS > ans) {
        continue;
      }

      bool flg = true;
      for (int i = N - 1; i >= 0; i--) {
        int sum = 0;
        for (int j = i + 1; j < N; j++) {
          sum += W[p[j]];
        }
        flg &= sum <= S[p[i]];
      }

      if (flg && g < ans + EPS) {
        ans = g;
        swap(best_p, p);
      }
    } while (next_permutation(p.begin(), p.end()));

    for (auto i : best_p) {
      cout << F[i] << endl;
    }
  }

  return 0;
}
Problem p00173 (C++)

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

int main() {
  char n[32];
  int m, a;
  for (int i = 0; i < 9; i++) {
    scanf("%s%d%d", n, &m, &a);
    printf("%s %d %d\n", m + a, m * 200 + a * 300);
  }
  return 0;
}

Fixed submission:
#include <cstdio>

int main() {
  char n[32];
  int m, a;
  for (int i = 0; i < 9; i++) {
    scanf("%s%d%d", n, &m, &a);
    printf("%s %d %d\n", n, m + a, m * 200 + a * 300);
  }
  return 0;
}
Problem p00173 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main(void) {
  char a;
  int s, z, i, f, g;
  while (scanf("%s %d %d", &a, &s, &z) != EOF) {
    f = s + z;
    g = (s * 200) + (z * 300);
    printf("%c %d %d\n", a, f, g);
    s = 0;
    z = 0;
    f = 0;
    g = 0;
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int a, s, d, f[10], h[10], i, j;
  char g[16][10];
  for (i = 1; i <= 9; i++) {
    scanf("%s %d %d", g[i], &s, &d);
    f[i] = s * 200 + d * 300;
    h[i] = s + d;
    printf("%s %d %d\n", g[i], h[i], f[i]);
  }
  return 0;
}
Problem p00174 (C++)

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

int main() {
  char ss[40];
  int i, j, a, b;

  while (~scanf(" %s ", ss)) {
    if (ss[0] == '0')
      break;
    i = 1;
    a = 0;
    b = 0;
    while (1) {
      if (ss[i] == '\0')
        break;
      if (ss[i] == 'A')
        ++a;
      if (ss[i] == 'B')
        ++b;
      ++i;
    }
    if (a > b) {
      ++a;
    } else {
      ++b;
    }
    printf("%d %d\n", a, b);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>

int main() {
  char ss[110];
  int i, j, a, b;

  while (~scanf(" %s ", ss)) {
    if (ss[0] == '0')
      break;
    i = 1;
    a = 0;
    b = 0;
    while (1) {
      if (ss[i] == '\0')
        break;
      if (ss[i] == 'A')
        ++a;
      if (ss[i] == 'B')
        ++b;
      ++i;
    }
    if (a > b) {
      ++a;
    } else {
      ++b;
    }
    printf("%d %d\n", a, b);
  }
  return 0;
}
Problem p00174 (C++)

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

int main() {
  string s;
  int a, b;
  while (true) {
    for (int i = 0; i < 3; i++) {
      a = b = 0;
      cin >> s;
      if (s[0] == '0')
        break;

      for (int j = 1; j < s.length(); j++) {
        if (s[j] == 'A')
          a++;
        else
          b++;
      }
      if (a > b)
        a++;
      else
        b++;
      cout << a << " " << b << endl;
    }
  }
}

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

int main() {
  string s;
  int a, b;
  while (true) {
    for (int i = 0; i < 3; i++) {
      a = b = 0;
      cin >> s;
      if (s[0] == '0')
        return 0;

      for (int j = 1; j < s.length(); j++) {
        if (s[j] == 'A')
          a++;
        else
          b++;
      }
      if (a > b)
        a++;
      else
        b++;
      cout << a << " " << b << endl;
    }
  }
}
Problem p00175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, j, k, cnt = 1, flg;
  int n;
  int fo[10] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144};
  while (1) {
    cin >> n;
    flg = 0;
    if (n == -1)
      break;
    for (i = 10; i >= 0; i--) {
      if (n == 0 && flg == 0) {
        cout << "0";
        break;
      }
      if (n / fo[i] > 0 && n != 0) {
        cout << n / fo[i];
        n = n % fo[i];
        flg = 1;
      } else if (flg == 1)
        cout << "0";
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, j, k, cnt = 1, flg;
  int n;
  int fo[10] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144};
  while (1) {
    cin >> n;
    flg = 0;
    if (n == -1)
      break;
    for (i = 9; i >= 0; i--) {
      if (n == 0 && flg == 0) {
        cout << "0";
        break;
      }
      if (n / fo[i] > 0 && n != 0) {
        cout << n / fo[i];
        n = n % fo[i];
        flg = 1;
      } else if (flg == 1)
        cout << "0";
    }
    cout << endl;
  }
  return 0;
}
Problem p00175 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int main(void) {
  int a[100];
  int n;
  int index;

  while (1) {
    scanf("%d", &n);
    if (n == -1)
      break;
    index = 0;
    do {
      a[index++] = (n & 0x3);
      n >> 2;
    } while (n != 0);

    index--;
    while (index >= 0) {
      printf("%d", a[index--]);
    }

    puts("");
  }
}

Fixed submission:
#include <stdio.h>

int main(void) {
  int a[100];
  int n;
  int index;

  while (1) {
    scanf("%d", &n);
    if (n == -1)
      break;
    index = 0;
    do {
      a[index++] = (n & 0x3);
      n >>= 2;
    } while (n != 0);

    index--;
    while (index >= 0) {
      printf("%d", a[index--]);
    }

    puts("");
  }
}
Problem p00178 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MH 100
using namespace std;
bool ck(int *f) {
  rep(i, 5) if (!f[i]) return 0;
  return 1;
}
int main() {
  int n;
  while (cin >> n, n) {
    int d, l, x, y, c = 0;
    int field[MH][5] = {0};
    rep(i, n) {
      cin >> d >> l >> x, y = MH, x--, d--;
      for (; y > 0; y--)
        rep(j, (d ? 1 : l)) if (field[y - 1][x + j]) goto END;
    END:
      rep(j, l) field[y + d * j][x + (1 - d) * j] = 1;
      rep(i, MH) while (ck(field[i])) {
        rep(j, MH - i - 1) rep(k, 5) field[j + i][k] = field[j + i + 1][k];
        rep(k, 5) field[MH - 1][k] = 0;
      }
    }
    rep(i, MH) rep(j, 5) c += field[i][j];
    cout << c << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MH 5000
using namespace std;
bool ck(int *f) {
  rep(i, 5) if (!f[i]) return 0;
  return 1;
}
int main() {
  int n;
  while (cin >> n, n) {
    int d, l, x, y, c = 0;
    int field[MH][5] = {0};
    rep(i, n) {
      cin >> d >> l >> x, y = MH, x--, d--;
      for (; y > 0; y--)
        rep(j, (d ? 1 : l)) if (field[y - 1][x + j]) goto END;
    END:
      rep(j, l) field[y + d * j][x + (1 - d) * j] = 1;
      rep(i, MH) while (ck(field[i])) {
        rep(j, MH - i - 1) rep(k, 5) field[j + i][k] = field[j + i + 1][k];
        rep(k, 5) field[MH - 1][k] = 0;
      }
    }
    rep(i, MH) rep(j, 5) c += field[i][j];
    cout << c << endl;
  }
  return 0;
}
Problem p00179 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <string>
using namespace std;
queue<int> q;
int t[59049];
int INF = 100000;
int encode(vector<int> vc) {
  int p = 0;
  for (int i = 0; i < vc.size(); i++) {
    p = p * 3 + vc[i];
  }
  return p;
}
vector<int> decode(int p, int n) {
  vector<int> vc(n);
  for (int i = 0; i < n; i++) {
    vc[n - i - 1] = p % 3;
    p /= 3;
  }
  return vc;
}
int main() {
  while (true) {
    for (int i = 0; i < 59049; i++)
      t[i] = INF;
    string s;
    cin >> s;
    int n = s.size(), start = 0, goal = 1;
    for (int i = 0; i < n; i++)
      goal *= 3;
    goal -= 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'r')
        start = start * 3 + 2;
      if (s[i] == 'g')
        start = start * 3 + 1;
      if (s[i] == 'b')
        start = start * 3;
    }
    q.push(start);
    t[start] = 0;
    int ok = -1;
    while (q.size()) {
      int p = q.front();
      if (p == 0 || p == goal / 2 || p == goal) {
        ok = p;
        while (q.size())
          q.pop();
        break;
      }
      q.pop();
      vector<int> vc = decode(p, n);
      for (int i = 0; i < n - 1; i++) {
        if (vc[i] == vc[i + 1])
          continue;
        vector<int> vc_c = vc;
        vc_c[i] = vc_c[i + 1] = ((vc[i] + vc[i + 1]) * 2) % 3;
        int hoge = encode(vc_c);
        if (t[hoge] != INF)
          continue;
        q.push(hoge);
        t[hoge] = t[p] + 1;
      }
    }
    if (ok == -1)
      cout << "NA" << endl;
    else
      cout << t[ok] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>
using namespace std;
queue<int> q;
int t[59049];
int INF = 100000;
int encode(vector<int> vc) {
  int p = 0;
  for (int i = 0; i < vc.size(); i++) {
    p = p * 3 + vc[i];
  }
  return p;
}
vector<int> decode(int p, int n) {
  vector<int> vc(n);
  for (int i = 0; i < n; i++) {
    vc[n - i - 1] = p % 3;
    p /= 3;
  }
  return vc;
}
int main() {
  while (true) {
    for (int i = 0; i < 59049; i++)
      t[i] = INF;
    string s;
    cin >> s;
    if (s == "0")
      break;
    int n = s.size(), start = 0, goal = 1;
    for (int i = 0; i < n; i++)
      goal *= 3;
    goal -= 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'r')
        start = start * 3 + 2;
      if (s[i] == 'g')
        start = start * 3 + 1;
      if (s[i] == 'b')
        start = start * 3;
    }
    q.push(start);
    t[start] = 0;
    int ok = -1;
    while (q.size()) {
      int p = q.front();
      if (p == 0 || p == goal / 2 || p == goal) {
        ok = p;
        while (q.size())
          q.pop();
        break;
      }
      q.pop();
      vector<int> vc = decode(p, n);
      for (int i = 0; i < n - 1; i++) {
        if (vc[i] == vc[i + 1])
          continue;
        vector<int> vc_c = vc;
        vc_c[i] = vc_c[i + 1] = ((vc[i] + vc[i + 1]) * 2) % 3;
        int hoge = encode(vc_c);
        if (t[hoge] != INF)
          continue;
        q.push(hoge);
        t[hoge] = t[p] + 1;
      }
    }
    if (ok == -1)
      cout << "NA" << endl;
    else
      cout << t[ok] << endl;
  }
  return 0;
}
Problem p00179 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
class Data {
public:
  string bc;
  int t;
  Data(string bc, int t) : bc(bc), t(t) {}
  Data() {}
};
bool judge(string s, int l) {
  for (int i = 0; i < l - 1; ++i) {
    if (s[i] != s[i + 1])
      return false;
  }
  return true;
}
char cj(char a, char b) {
  if (a == 'b' && b == 'g')
    return 'r';
  if (a == 'b' && b == 'r')
    return 'g';
  if (a == 'g' && b == 'r')
    return 'b';
  swap(a, b);
  if (a == 'b' && b == 'g')
    return 'r';
  if (a == 'b' && b == 'r')
    return 'g';
  if (a == 'g' && b == 'r')
    return 'b';
}
int main() {
  string s;
  while (cin >> s, s != "s") {
    int l = s.length();
    int res = -1;
    queue<Data> Q;
    set<string> visited;
    Q.push(Data(s, 0));
    while (!Q.empty()) {
      Data t = Q.front();
      Q.pop();
      if (visited.count(t.bc))
        continue;
      if (judge(t.bc, l)) {
        res = t.t;
        break;
      }
      visited.insert(t.bc);
      for (int i = 0; i < l - 1; ++i) {
        if (t.bc[i] == t.bc[i + 1])
          continue;
        char nc = cj(t.bc[i], t.bc[i + 1]);
        string nbc = t.bc;
        nbc[i] = nbc[i + 1] = nc;
        Q.push(Data(nbc, t.t + 1));
      }
    }
    if (res == -1)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
class Data {
public:
  string bc;
  int t;
  Data(string bc, int t) : bc(bc), t(t) {}
  Data() {}
};
bool judge(string s, int l) {
  for (int i = 0; i < l - 1; ++i) {
    if (s[i] != s[i + 1])
      return false;
  }
  return true;
}
char cj(char a, char b) {
  if (a == 'b' && b == 'g')
    return 'r';
  if (a == 'b' && b == 'r')
    return 'g';
  if (a == 'g' && b == 'r')
    return 'b';
  swap(a, b);
  if (a == 'b' && b == 'g')
    return 'r';
  if (a == 'b' && b == 'r')
    return 'g';
  if (a == 'g' && b == 'r')
    return 'b';
}
int main() {
  string s;
  while (cin >> s, s != "0") {
    int l = s.length();
    int res = -1;
    queue<Data> Q;
    set<string> visited;
    Q.push(Data(s, 0));
    while (!Q.empty()) {
      Data t = Q.front();
      Q.pop();
      if (visited.count(t.bc))
        continue;
      if (judge(t.bc, l)) {
        res = t.t;
        break;
      }
      visited.insert(t.bc);
      for (int i = 0; i < l - 1; ++i) {
        if (t.bc[i] == t.bc[i + 1])
          continue;
        char nc = cj(t.bc[i], t.bc[i + 1]);
        string nbc = t.bc;
        nbc[i] = nbc[i + 1] = nc;
        Q.push(Data(nbc, t.t + 1));
      }
    }
    if (res == -1)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
  return 0;
}
Problem p00179 (C++)

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

using namespace std;

typedef pair<string, int> P;

//????????????????????????????????????
bool judge(string str) {
  for (int i = 0; i < str.size(); ++i) {
    if (str[0] != str[i])
      return false;
  }
  return true;
}

//??£?????????2?????????????????§?????????????????????
char color(char a, char b) {
  if ((a == 'g' && b == 'r') || (a == 'r' && b == 'g'))
    return 'b';
  else if ((a == 'b' && b == 'r') || (a == 'r' && b == 'b'))
    return 'g';
  else if ((a == 'g' && b == 'b') || (a == 'b' && b == 'g'))
    return 'r';
}

int solve(string s) {

  queue<P> q;
  map<string, int> m;
  q.push(P(s, 0)); //?????\??????????????¶????????\??????

  while (!q.empty()) { //?????\??????????????¶?????????????????§
    P p = q.front();
    q.pop();
    if (m[p.first])
      continue; //??????????????????
    string now = p.first;
    if (judge(now))
      return p.second;
    for (int i = 0; i < now.size() - 1; ++i) {
      if (now[i] != now[i + 1]) { //??£?????????2????????°????????¨???
        char tmp = color(now[i], now[i + 1]);
        string D = now;
        D[i] = tmp;
        D[i + 1] = tmp;
        q.push(P(D, p.second + 1));
      }
    }
  }
  return -1;
}

int main() {
  string worm;
  while (cin >> worm) {
    if (worm == "0")
      break;
    int count = solve(worm);
    if (count == -1)
      cout << "NA" << endl;
    else
      cout << count << endl;
  }
  return 0;
}

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

using namespace std;

typedef pair<string, int> P;

//????????????????????????????????????
bool judge(string str) {
  for (int i = 0; i < str.size(); ++i) {
    if (str[0] != str[i])
      return false;
  }
  return true;
}

//??£?????????2?????????????????§?????????????????????
char color(char a, char b) {
  if ((a == 'g' && b == 'r') || (a == 'r' && b == 'g'))
    return 'b';
  else if ((a == 'b' && b == 'r') || (a == 'r' && b == 'b'))
    return 'g';
  else if ((a == 'g' && b == 'b') || (a == 'b' && b == 'g'))
    return 'r';
}

int solve(string s) {

  queue<P> q;
  map<string, int> m;
  q.push(P(s, 0)); //?????\??????????????¶????????\??????

  while (!q.empty()) { //?????\??????????????¶?????????????????§
    P p = q.front();
    q.pop();
    if (m[p.first])
      continue; //??????????????????
    string now = p.first;
    if (judge(now))
      return p.second;
    m[p.first] = p.second;
    for (int i = 0; i < now.size() - 1; ++i) {
      if (now[i] != now[i + 1]) { //??£?????????2????????°????????¨???
        char tmp = color(now[i], now[i + 1]);
        string D = now;
        D[i] = tmp;
        D[i + 1] = tmp;
        q.push(P(D, p.second + 1));
      }
    }
  }
  return -1;
}

int main() {
  string worm;
  while (cin >> worm) {
    if (worm == "0")
      break;
    int count = solve(worm);
    if (count == -1)
      cout << "NA" << endl;
    else
      cout << count << endl;
  }
  return 0;
}
Problem p00180 (C++)

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

using namespace std;

int n, m;
vector<set<pair<int, int>>> cost(100, set<pair<int, int>>());

int prim() {
  int res = 0;
  set<int> v;
  v.insert(0);
  while (v.size() < n) {
    pair<int, int> mini(100000007, -1);
    for (set<int>::iterator it = v.begin(); it != v.end(); it++) {
      for (set<pair<int, int>>::iterator it2 = cost[*it].begin();
           it2 != cost[*it].end(); it2++)
        if (v.find((*it2).second) == v.end()) {
          if (mini.first > (*it2).first)
            mini = (*it2);
          break;
        }
    }
    v.insert(mini.second);
    res += mini.first;
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      cost[a].insert(pair<int, int>(c, b));
      cost[b].insert(pair<int, int>(c, a));
    }
    cout << prim() << endl;
    for (int i = 0; i < m; i++)
      cost[i].clear();
  }
  return 0;
}

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

using namespace std;

int n, m;
vector<set<pair<int, int>>> cost(200, set<pair<int, int>>());

int prim() {
  int res = 0;
  set<int> v;
  v.insert(0);
  while (v.size() < n) {
    pair<int, int> mini(100000007, -1);
    for (set<int>::iterator it = v.begin(); it != v.end(); it++) {
      for (set<pair<int, int>>::iterator it2 = cost[*it].begin();
           it2 != cost[*it].end(); it2++)
        if (v.find((*it2).second) == v.end()) {
          if (mini.first > (*it2).first)
            mini = (*it2);
          break;
        }
    }
    v.insert(mini.second);
    res += mini.first;
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      cost[a].insert(pair<int, int>(c, b));
      cost[b].insert(pair<int, int>(c, a));
    }
    cout << prim() << endl;
    for (int i = 0; i < m; i++)
      cost[i].clear();
  }
  return 0;
}
Problem p00180 (C++)

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

using namespace std;

// disjoint-set
int parent[100];

void init(int n) {
  for (int i = 0; i < n; i++)
    parent[i] = i;
}

int root(int a) {
  if (parent[a] == a)
    return a;
  return (parent[a] = root(parent[a]));
}

bool same_set_p(int a, int b) { return root(a) == root(b); }

void unite(int a, int b) { parent[root(a)] = root(b); }

// graph
class Edge {
public:
  int cost, a, b;
  Edge(int c = 0, int d = 0, int e = 0) : cost(c), a(d), b(e) {}
  bool operator<(const Edge &o) const { return cost < o.cost; }
};

// main
int n, m;
Edge graph[100];

int main() {
  while (scanf("%d %d", &n, &m), n) {
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      scanf("%d %d %d", &a, &b, &cost);
      graph[i] = Edge(cost, a, b);
    }

    sort(graph, graph + m);

    int answer = 0;
    init(n);

    for (int i = 0; i < m; i++) {
      Edge e = graph[i];
      if (!same_set_p(e.a, e.b)) {
        answer += e.cost;
        unite(e.a, e.b);
      }
    }

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

Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

// disjoint-set
int parent[100];

void init(int n) {
  for (int i = 0; i < n; i++)
    parent[i] = i;
}

int root(int a) {
  if (parent[a] == a)
    return a;
  return (parent[a] = root(parent[a]));
}

bool same_set_p(int a, int b) { return root(a) == root(b); }

void unite(int a, int b) { parent[root(a)] = root(b); }

// graph
class Edge {
public:
  int cost, a, b;
  Edge(int c = 0, int d = 0, int e = 0) : cost(c), a(d), b(e) {}
  bool operator<(const Edge &o) const { return cost < o.cost; }
};

// main
int n, m;
Edge graph[10000];

int main() {
  while (scanf("%d %d", &n, &m), n) {
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      scanf("%d %d %d", &a, &b, &cost);
      graph[i] = Edge(cost, a, b);
    }

    sort(graph, graph + m);

    int answer = 0;
    init(n);

    for (int i = 0; i < m; i++) {
      Edge e = graph[i];
      if (!same_set_p(e.a, e.b)) {
        answer += e.cost;
        unite(e.a, e.b);
      }
    }

    printf("%d\n", answer);
  }
}
Problem p00180 (C++)

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

struct Dset {
  Dset *parent;
  int rank;

  Dset() { clear(); }

  void clear() {
    parent = NULL;
    rank = 0;
  }

  Dset *find() {
    if (!parent) {
      return this;
    } else {
      parent = parent->find();
      return parent;
    }
  }

  bool merge(Dset *a) {
    Dset *myRoot = find();
    Dset *yourRoot = a->find();
    if (myRoot->rank < yourRoot->rank) {
      myRoot->parent = yourRoot;
    } else if (myRoot->rank > yourRoot->rank) {
      yourRoot->parent = myRoot;
    } else if (myRoot != yourRoot) {
      yourRoot->parent = myRoot;
      myRoot->rank++;
    } else {
      return false;
    }
    return true;
  }
};

struct Bridge {
  int a;
  int b;
  int cost;

  bool operator<(const Bridge &obj) const { return (cost < obj.cost); }
};

int main() {
  int n, m, ans;
  Bridge bridge[6000];
  Dset x[100];
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < m; i++) {
      cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
    }
    sort(bridge, bridge + m);
    for (int i = 0; i < m; i++) {
      x[i].clear();
    }
    ans = 0;
    for (int i = 0; i < m; i++) {
      if (x[bridge[i].a].merge(&x[bridge[i].b])) {
        ans += bridge[i].cost;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

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

struct Dset {
  Dset *parent;
  int rank;

  Dset() { clear(); }

  void clear() {
    parent = NULL;
    rank = 0;
  }

  Dset *find() {
    if (!parent) {
      return this;
    } else {
      parent = parent->find();
      return parent;
    }
  }

  bool merge(Dset *a) {
    Dset *myRoot = find();
    Dset *yourRoot = a->find();
    if (myRoot->rank < yourRoot->rank) {
      myRoot->parent = yourRoot;
    } else if (myRoot->rank > yourRoot->rank) {
      yourRoot->parent = myRoot;
    } else if (myRoot != yourRoot) {
      yourRoot->parent = myRoot;
      myRoot->rank++;
    } else {
      return false;
    }
    return true;
  }
};

struct Bridge {
  int a;
  int b;
  int cost;

  bool operator<(const Bridge &obj) const { return (cost < obj.cost); }
};

int main() {
  int n, m, ans;
  Bridge bridge[6000];
  Dset x[100];
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < m; i++) {
      cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
    }
    sort(bridge, bridge + m);
    for (int i = 0; i < n; i++) {
      x[i].clear();
    }
    ans = 0;
    for (int i = 0; i < m; i++) {
      if (x[bridge[i].a].merge(&x[bridge[i].b])) {
        ans += bridge[i].cost;
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00180 (C++)

Buggy submission (status: Runtime Error):
// kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 101
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) {
  if (parent[a] == a)
    return a;
  return parent[a] = root(parent[a]);
}
int unite(int a, int b) {
  if (root(a) == root(b))
    return 0;
  parent[root(a)] = root(b);
  return 1;
}
int main() {
  int i, s, n, m;
  for (; cin >> n >> m, n; cout << s << endl) {
    for (i = 0; i < m; i++)
      cin >> a[i] >> b[i] >> node[i].first, node[i].second = i;
    sort(node, node + m);
    for (i = 0; i < n; i++)
      parent[i] = i;
    for (s = i = 0; i < m; i++)
      if (unite(a[node[i].second], b[node[i].second]))
        s += node[i].first;
  }
}

Fixed submission:
// kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 9999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) {
  if (parent[a] == a)
    return a;
  return parent[a] = root(parent[a]);
}
int unite(int a, int b) {
  if (root(a) == root(b))
    return 0;
  parent[root(a)] = root(b);
  return 1;
}
int main() {
  int i, s, n, m;
  for (; cin >> n >> m, n; cout << s << endl) {
    for (i = 0; i < m; i++)
      cin >> a[i] >> b[i] >> node[i].first, node[i].second = i;
    sort(node, node + m);
    for (i = 0; i < n; i++)
      parent[i] = i;
    for (s = i = 0; i < m; i++)
      if (unite(a[node[i].second], b[node[i].second]))
        s += node[i].first;
  }
}
Problem p00180 (C++)

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

int main() {
  int br[100][100];
  bool f[100];
  int tr[100];
  int n, m;

  while (1) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        br[i][j] = -1;
      tr[i] = INT_MAX;
      f[i] = 0;
    }
    for (int k = 0; k < m; k++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      br[a][b] = cost;
      br[b][a] = cost;
    }
    int comin = 0, r, trmin;
    tr[0] = 0;

    for (int k = 0; k < n; k++) {
      trmin = INT_MAX;
      for (int i = 0; i < n; i++)
        if (f[i] == 0 && trmin > tr[i])
          r = i, trmin = tr[i];
      // cout << trmin<<endl;
      comin += trmin;
      f[r] = 1;
      for (int i = 0; i < n; i++) {
        if (f[i] == 0 && tr[i] > br[r][i] && br[r][i] != -1)
          tr[i] = br[r][i];
      }
    }
    cout << comin << endl;
  }

  return 0;
}

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

int main() {
  int br[100][100];
  bool f[100];
  int tr[100];
  int n, m;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        br[i][j] = -1;
      tr[i] = INT_MAX;
      f[i] = 0;
    }
    for (int k = 0; k < m; k++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      br[a][b] = cost;
      br[b][a] = cost;
    }
    int comin = 0, r, trmin;
    tr[0] = 0;

    for (int k = 0; k < n; k++) {
      trmin = INT_MAX;
      for (int i = 0; i < n; i++)
        if (f[i] == 0 && trmin > tr[i])
          r = i, trmin = tr[i];
      // cout << trmin<<endl;
      comin += trmin;
      f[r] = 1;
      for (int i = 0; i < n; i++) {
        if (f[i] == 0 && tr[i] > br[r][i] && br[r][i] != -1)
          tr[i] = br[r][i];
      }
    }
    cout << comin << endl;
  }

  return 0;
}
Problem p00180 (C++)

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

using namespace std;

const int inf = 999999;

int prim(int a[101][101], int n) {
  int r = 0, mcost[101];
  bool vis[101];

  fill(mcost, mcost + 101, inf);
  fill(vis, vis + 101, false);
  mcost[0] = 0;

  while (1) {
    int v = -1;

    for (int i = 0; i < n; i++) {
      if (!vis[i] && (v == -1 || mcost[v] > mcost[i])) {
        v = i;
      }
    }

    if (v == -1) {
      break;
    }
    vis[v] = true;
    r += mcost[v];

    for (int i = 0; i < n; i++) {
      mcost[i] = min(mcost[i], a[v][i]);
    }
  }

  return r;
}

int main() {
  int n, m, a[101][101], s, t, u;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

    fill(&a[0][0], &a[101][101], inf);
    for (int i = 0; i <= n; i++) {
      a[i][i] = 0;
    }

    while (m--) {
      cin >> s >> t >> u;
      a[s][t] = a[t][s] = u;
    }

    cout << prim(a, n) << endl;
  }

  return 0;
}

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

using namespace std;

const int inf = 999999;

int prim(int a[101][101], int n) {
  int r = 0, mcost[101];
  bool vis[101];

  fill(mcost, mcost + 101, inf);
  fill(vis, vis + 101, false);
  mcost[0] = 0;

  while (1) {
    int v = -1;

    for (int i = 0; i < n; i++) {
      if (!vis[i] && (v == -1 || mcost[v] > mcost[i])) {
        v = i;
      }
    }

    if (v == -1) {
      break;
    }
    vis[v] = true;
    r += mcost[v];

    for (int i = 0; i < n; i++) {
      mcost[i] = min(mcost[i], a[v][i]);
    }
  }

  return r;
}

int main() {
  int n, m, a[101][101], s, t, u;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

    fill(&a[0][0], &a[100][100], inf);
    for (int i = 0; i <= n; i++) {
      a[i][i] = 0;
    }

    while (m--) {
      cin >> s >> t >> u;
      a[s][t] = a[t][s] = u;
    }

    cout << prim(a, n) << endl;
  }

  return 0;
}
Problem p00181 (C++)

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

#include <string.h>

#define INT_INF 2000000

int m, n;

int a[100 + 1];

int memo[20 + 1][100 + 1];

int search(int i, int j) {
  if (memo[i][j] >= 0)
    return memo[i][j];

  if (i == 1)
    return memo[i][j] = std::accumulate(a + j, a + n, 0);

  int am = INT_INF;

  for (int s = 0; j < n; s += a[j], j++)
    am = std::min(std::max(search(i - 1, j), s), am);

  return memo[i][j] = am;
}

int main(int argc, char **argv) {
  while (1) {
    std::cin >> m >> n;

    if (m == 0)
      break;

    for (int i = 0; i < n; i++)
      std::cin >> a[i];

    memset(memo, -1, sizeof(memo));

    std::cout << search(m, 0) << std::endl;
  }
}

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

#include <string.h>

#define INT_INF 2000000

int m, n;

int a[100 + 1];

int memo[20 + 1][100 + 1];

int search(int i, int j) {
  if (memo[i][j] >= 0)
    return memo[i][j];

  if (i == 1)
    return memo[i][j] = std::accumulate(a + j, a + n, 0);

  int am = INT_INF;

  for (int k = j, s = 0; k < n; s += a[k], k++)
    am = std::min(std::max(search(i - 1, k), s), am);

  return memo[i][j] = am;
}

int main(int argc, char **argv) {
  while (1) {
    std::cin >> m >> n;

    if (m == 0)
      break;

    for (int i = 0; i < n; i++)
      std::cin >> a[i];

    memset(memo, -1, sizeof(memo));

    std::cout << search(m, 0) << std::endl;
  }
}