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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  while (1) {
    int t, h;
    cin >> t >> h;
    vector<int> td;
    vector<int> hd;
    int tsum = 0;
    int hsum = 0;
    REP(i, t) {
      int tmp;
      cin >> tmp;
      td.pb(tmp);
      tsum += tmp;
    }
    REP(i, h) {
      int tmp;
      cin >> tmp;
      hd.pb(tmp);
      hsum += tmp;
    }
    int ansx = 114514;
    int ansy = 114514;
    REP(i, t) {
      REP(j, h) {
        if (tsum - td[i] + hd[j] == hsum - hd[j] + td[i]) {
          if ((ansx + ansy) > (i + j)) {
            ansx = i;
            ansy = j;
            i = t;
            j = h;
          }
        }
      }
    }
    if (ansx == 114514)
      cout << -1 << endl;
    else
      cout << td[ansx] << " " << hd[ansy] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  while (1) {
    int t, h;
    cin >> t >> h;
    if (t == 0 && h == 0)
      break;
    vector<int> td;
    vector<int> hd;
    int tsum = 0;
    int hsum = 0;
    REP(i, t) {
      int tmp;
      cin >> tmp;
      td.pb(tmp);
      tsum += tmp;
    }
    REP(i, h) {
      int tmp;
      cin >> tmp;
      hd.pb(tmp);
      hsum += tmp;
    }
    int ansx = 114514;
    int ansy = 114514;
    REP(i, t) {
      REP(j, h) {
        if (tsum - td[i] + hd[j] == hsum - hd[j] + td[i]) {
          if ((ansx + ansy) > (i + j)) {
            ansx = i;
            ansy = j;
            i = t;
            j = h;
          }
        }
      }
    }
    if (ansx == 114514)
      cout << -1 << endl;
    else
      cout << td[ansx] << " " << hd[ansy] << endl;
  }
}
Problem p00734 (C++)

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

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair<int, int> iP;
typedef pair<iP, int> iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

int main() {

  int n, m;
  while (cin >> n >> m, n) {
    int sumt = 0, sumh = 0;
    vector<int> ts(n), hs(n);
    for (int i = 0; i < n; i++) {
      cin >> ts[i];
      sumt += ts[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> hs[i];
      sumh += hs[i];
    }
    int mint = INF, minh = INF;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (sumt - ts[i] + hs[j] == sumh - hs[j] + ts[i] &&
            mint + minh > ts[i] + hs[j])
          mint = ts[i], minh = hs[j];
      }
    }
    if (mint == INF)
      cout << -1 << endl;
    else
      cout << mint << " " << minh << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair<int, int> iP;
typedef pair<iP, int> iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

int main() {

  int n, m;
  while (cin >> n >> m, n) {
    int sumt = 0, sumh = 0;
    vector<int> ts(n), hs(m);
    for (int i = 0; i < n; i++) {
      cin >> ts[i];
      sumt += ts[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> hs[i];
      sumh += hs[i];
    }
    int mint = INF, minh = INF;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (sumt - ts[i] + hs[j] == sumh - hs[j] + ts[i] &&
            mint + minh > ts[i] + hs[j])
          mint = ts[i], minh = hs[j];
      }
    }
    if (mint == INF)
      cout << -1 << endl;
    else
      cout << mint << " " << minh << endl;
  }
}
Problem p00734 (C++)

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

using namespace std;

int main() {
  int n, m;

  while (true) {
    cin >> n >> m;
    vector<int> a(n);
    vector<int> b(n);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < m; i++)
      cin >> b[i];

    // ??????????????????
    int suma = 0, sumb = 0;
    for (int i = 0; i < n; i++)
      suma += a[i];
    for (int i = 0; i < m; i++)
      sumb += b[i];

    vector<pair<int, int>> res;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if ((suma - a[i] + b[j]) == (sumb - b[j] + a[i])) {
          res.push_back(make_pair(i, j));
        }

    if (res.size() == 0)
      cout << -1 << endl;
    else {
      int tmpa, tmpb, r = 999999999, tmp;
      for (int i = 0; i < res.size(); i++) {
        tmp = a[res[i].first] + b[res[i].second];
        if (tmp < r) {
          r = tmp;
          tmpa = res[i].first;
          tmpb = res[i].second;
        }
      }
      cout << a[tmpa] << " " << b[tmpb] << endl;
    }
  }
}

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

using namespace std;

int main() {
  int n, m;

  while (true) {
    cin >> n >> m;
    vector<int> a(n);
    vector<int> b(m);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < m; i++)
      cin >> b[i];

    // ??????????????????
    int suma = 0, sumb = 0;
    for (int i = 0; i < n; i++)
      suma += a[i];
    for (int i = 0; i < m; i++)
      sumb += b[i];

    vector<pair<int, int>> res;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if ((suma - a[i] + b[j]) == (sumb - b[j] + a[i])) {
          res.push_back(make_pair(i, j));
        }

    if (res.size() == 0)
      cout << -1 << endl;
    else {
      int tmpa, tmpb, r = 999999999, tmp;
      for (int i = 0; i < res.size(); i++) {
        tmp = a[res[i].first] + b[res[i].second];
        if (tmp < r) {
          r = tmp;
          tmpa = res[i].first;
          tmpb = res[i].second;
        }
      }
      cout << a[tmpa] << " " << b[tmpb] << endl;
    }
  }
}
Problem p00734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
typedef pair<int, int> P;
int main() {
  int n, m;
  while (cin >> n >> m && n) {
    int sum = 0;
    vector<int> p(n), q(n);
    rep(i, n) {
      cin >> p[i];
      sum += p[i];
    }
    rep(i, m) {
      cin >> q[i];
      sum -= q[i];
    }
    int ap = INF, aq = INF;
    for (int pn : p)
      for (int qn : q)
        if (2 * (pn - qn) == sum && pn + qn < ap + aq) {
          ap = pn;
          aq = qn;
        }
    if (ap == INF) {
      puts("-1");
    } else {
      cout << ap << ' ' << aq << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
typedef pair<int, int> P;
int main() {
  int n, m;
  while (cin >> n >> m && n) {
    int sum = 0;
    vector<int> p(n), q(m);
    rep(i, n) {
      cin >> p[i];
      sum += p[i];
    }
    rep(i, m) {
      cin >> q[i];
      sum -= q[i];
    }
    int ap = INF, aq = INF;
    for (int pn : p)
      for (int qn : q)
        if (2 * (pn - qn) == sum && pn + qn < ap + aq) {
          ap = pn;
          aq = qn;
        }
    if (ap == INF) {
      puts("-1");
    } else {
      cout << ap << ' ' << aq << endl;
    }
  }
  return 0;
}
Problem p00734 (C++)

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

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

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

const int INF = 1 << 29;
const double EPS = 1e-10;

int main() {
  for (int n, m; cin >> n >> m, (n | m) != 0;) {
    int taro[100], hanako[100];
    int t_sum = 0, h_sum = 0;
    rep(i, n) {
      cin >> taro[i];
      t_sum += taro[i];
    }
    rep(i, m) {
      cin >> hanako[i];
      h_sum += hanako[i];
    }

    int ans_t, ans_h;
    int min = INF;
    rep(i, n) rep(j, m) {
      int t = t_sum - taro[i] + hanako[j];
      int h = h_sum - hanako[j] + taro[i];

      if (t == h && min > taro[i] + hanako[j]) {
        min = taro[i] + hanako[j];
        ans_t = taro[i];
        ans_h = hanako[j];
      }
    }

    dump("#");
    if (min == INF)
      cout << -1 << endl;
    else
      cout << ans_t << ' ' << ans_h << endl;
  }

  return 0;
}

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

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

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

const int INF = 1 << 29;
const double EPS = 1e-10;

int main() {
  for (int n, m; cin >> n >> m, (n | m) != 0;) {
    int taro[100], hanako[100];
    int t_sum = 0, h_sum = 0;
    rep(i, n) {
      cin >> taro[i];
      t_sum += taro[i];
    }
    rep(i, m) {
      cin >> hanako[i];
      h_sum += hanako[i];
    }

    int ans_t, ans_h;
    int min = INF;
    rep(i, n) rep(j, m) {
      int t = t_sum - taro[i] + hanako[j];
      int h = h_sum - hanako[j] + taro[i];

      if (t == h && min > taro[i] + hanako[j]) {
        min = taro[i] + hanako[j];
        ans_t = taro[i];
        ans_h = hanako[j];
      }
    }

    if (min == INF)
      cout << -1 << endl;
    else
      cout << ans_t << ' ' << ans_h << endl;
  }

  return 0;
}
Problem p00734 (C++)

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

int main() {
  int n, m, mi, mih, mit;
  while (cin >> n >> m && n && m) {
    vector<int> taro(n);
    vector<int> hanako(n);
    int t = 0, h = 0;
    for (int i = 0; i < n; i++) {
      cin >> taro[i];
      t += taro[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> hanako[i];
      h += hanako[i];
    }
    mi = (1 << 21);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (t - taro[i] + hanako[j] == h - hanako[j] + taro[i]) {
          if (taro[i] + hanako[j] < mi) {
            mi = taro[i] + hanako[j];
            mit = taro[i];
            mih = hanako[j];
          }
        }
      }
    }
    if (mi == 1 << 21)
      cout << -1 << endl;
    else
      cout << mit << " " << mih << endl;
  }
}

Fixed submission:
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;

int main() {
  int n, m, mi, mih, mit;
  while (cin >> n >> m && n && m) {
    vector<int> taro(n);
    vector<int> hanako(m);
    int t = 0, h = 0;
    for (int i = 0; i < n; i++) {
      cin >> taro[i];
      t += taro[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> hanako[i];
      h += hanako[i];
    }
    mi = (1 << 21);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (t - taro[i] + hanako[j] == h - hanako[j] + taro[i]) {
          if (taro[i] + hanako[j] < mi) {
            mi = taro[i] + hanako[j];
            mit = taro[i];
            mih = hanako[j];
          }
        }
      }
    }
    if (mi == 1 << 21)
      cout << -1 << endl;
    else
      cout << mit << " " << mih << endl;
  }
}
Problem p00734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;

int main() {

  int d, m, n, t[51], h[51];
  while (1) {
    int st = 0, sh = 0, mint = 101;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < m; i++)
      cin >> h[i];
    for (int i = 0; i < n; i++)
      st += t[i];
    for (int i = 0; i < m; i++)
      sh += h[i];
    d = st - sh;
    if (d % 2 != 0) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (t[i] - h[j] == d / 2 && t[i] < mint)
          mint = t[i];
      }
    if (mint < 101)
      cout << mint << " " << mint - d / 2 << endl;
    else
      cout << -1 << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;

int main() {

  int d, m, n, t[101], h[101];
  while (1) {
    int st = 0, sh = 0, mint = 101;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < m; i++)
      cin >> h[i];
    for (int i = 0; i < n; i++)
      st += t[i];
    for (int i = 0; i < m; i++)
      sh += h[i];
    d = st - sh;
    if (d % 2 != 0) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (t[i] - h[j] == d / 2 && t[i] < mint)
          mint = t[i];
      }
    if (mint < 101)
      cout << mint << " " << mint - d / 2 << endl;
    else
      cout << -1 << endl;
  }
}
Problem p00734 (C++)

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

using namespace std;
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int i, j, temp = 0, k, count = 0;
    int t[n], h[m], sum1 = 0, sum2 = 0;
    int d[100], e[100];
    bool cando = false;
    for (i = 0; i < n; i++) {
      cin >> t[i];
    }
    for (i = 0; i < m; i++) {
      cin >> h[i];
    }

    sort(t, t + n);
    sort(h, h + m);

    for (i = 0; i < n; i++) {
      sum1 += t[i];
    }
    for (i = 0; i < m; i++) {
      sum2 += h[i];
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (sum1 - t[i] + h[j] == sum2 - h[j] + t[i]) {
          cando = true;
          d[count] = t[i];
          e[count] = h[j];
          count++;
        }
      }
    }
    if (!cando) {
      cout << "-1" << endl;
    } else if (cando) {
      for (i = 0; i < count; i++) {
        for (j = i + 1; j < count; j++) {
          if (d[i] + e[i] > d[j] + e[j]) {
            temp = d[i];
            d[i] = d[i + 1];
            d[i + 1] = temp;

            temp = e[i];
            e[i] = e[i + 1];
            e[i + 1] = temp;
          }
        }
      }
      cout << d[0] << " " << e[0] << endl;
    }
  }
  return 0;
}

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

using namespace std;
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int i, j, temp = 0, k, count = 0;
    int t[n], h[m], sum1 = 0, sum2 = 0;
    int d[10000], e[10000];
    bool cando = false;
    for (i = 0; i < n; i++) {
      cin >> t[i];
    }
    for (i = 0; i < m; i++) {
      cin >> h[i];
    }

    sort(t, t + n);
    sort(h, h + m);

    for (i = 0; i < n; i++) {
      sum1 += t[i];
    }
    for (i = 0; i < m; i++) {
      sum2 += h[i];
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (sum1 - t[i] + h[j] == sum2 - h[j] + t[i]) {
          cando = true;
          d[count] = t[i];
          e[count] = h[j];
          count++;
        }
      }
    }
    if (!cando) {
      cout << "-1" << endl;
    } else if (cando) {
      for (i = 0; i < count; i++) {
        for (j = i + 1; j < count; j++) {
          if (d[i] + e[i] > d[j] + e[j]) {
            temp = d[i];
            d[i] = d[i + 1];
            d[i + 1] = temp;

            temp = e[i];
            e[i] = e[i + 1];
            e[i + 1] = temp;
          }
        }
      }
      cout << d[0] << " " << e[0] << endl;
    }
  }
  return 0;
}
Problem p00734 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "cmath"
#include "iomanip"
#include "iostream"
#include "map"
#include "set"
#include "string"
using namespace std;

#define TEST 1
#if TEST
FILE *in = freopen("./Q/question.txt", "r", stdin);
FILE *out = freopen("./A/answer.txt", "w", stdout);
#endif

int main() {
  int n, m;
  while (true) {
    int sa = 0, ans = 201, idxi = -1, idxj = -1;
    cin >> n >> m;
    set<int> tarous, hanakos;
    if (!n && !m)
      break;
    for (int i = 0; i < n + m; ++i) {
      int a;
      cin >> a;
      if (i < n) {
        tarous.insert(a);
        sa += a;
      } else {
        hanakos.insert(a);
        sa -= a;
      }
    }
    // cout <<sa<<endl;
    // cout <<tarousum <<" "<<hanakosum <<endl;
    if (!(sa & 1)) {
      bool FLAG = false;
      for (auto &&itr1 = tarous.begin(); itr1 != tarous.end(); ++itr1) {
        for (auto &&itr2 = hanakos.begin(); itr2 != hanakos.end(); ++itr2) {
          if (*itr1 - *itr2 == sa / 2) {
            if (ans > *itr1 + *itr2) {
              ans = *itr1 + *itr2;
              idxi = *itr1;
              idxj = *itr2;
              FLAG = true;
              break;
            }
          }
        }
        if (FLAG)
          break;
      }
    }
    if (ans == 201)
      cout << -1 << endl;
    else
      cout << idxi << " " << idxj << endl;
    tarous.clear();
    hanakos.clear();
  }
#if TEST
  fclose(in);
  fclose(out);
#endif
  return 0;
}

Fixed submission:
#include "algorithm"
#include "cmath"
#include "iomanip"
#include "iostream"
#include "map"
#include "set"
#include "string"
using namespace std;

#define TEST 0
#if TEST
FILE *in = freopen("./Q/question.txt", "r", stdin);
FILE *out = freopen("./A/answer.txt", "w", stdout);
#endif

int main() {
  int n, m;
  while (true) {
    int sa = 0, ans = 201, idxi = -1, idxj = -1;
    cin >> n >> m;
    set<int> tarous, hanakos;
    if (!n && !m)
      break;
    for (int i = 0; i < n + m; ++i) {
      int a;
      cin >> a;
      if (i < n) {
        tarous.insert(a);
        sa += a;
      } else {
        hanakos.insert(a);
        sa -= a;
      }
    }
    // cout <<sa<<endl;
    // cout <<tarousum <<" "<<hanakosum <<endl;
    if (!(sa & 1)) {
      bool FLAG = false;
      for (auto &&itr1 = tarous.begin(); itr1 != tarous.end(); ++itr1) {
        for (auto &&itr2 = hanakos.begin(); itr2 != hanakos.end(); ++itr2) {
          if (*itr1 - *itr2 == sa / 2) {
            if (ans > *itr1 + *itr2) {
              ans = *itr1 + *itr2;
              idxi = *itr1;
              idxj = *itr2;
              FLAG = true;
              break;
            }
          }
        }
        if (FLAG)
          break;
      }
    }
    if (ans == 201)
      cout << -1 << endl;
    else
      cout << idxi << " " << idxj << endl;
    tarous.clear();
    hanakos.clear();
  }
#if TEST
  fclose(in);
  fclose(out);
#endif
  return 0;
}
Problem p00734 (C++)

Buggy submission (status: Time Limit Exceeded):

#include "iostream"
using namespace std;
int main() {
  while (1) {
    int n, m, t[100], h[100], sumt = 0, sumh = 0, mint = 101, minh = 101;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> t[i];
      sumt += t[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> h[i];
      sumh += h[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; i++) {
        if (sumt - t[i] + h[j] == sumh - h[j] + t[i]) {
          if (h[j] + t[i] < mint + minh) {
            mint = t[i];
            minh = h[j];
          }
        }
      }
    }
    if (mint == 101 && minh == 101)
      cout << "-1" << endl;
    else
      cout << mint << " " << minh << endl;
  }
  return 0;
}


Fixed submission:

#include "iostream"
using namespace std;
int main() {
  while (1) {
    int n, m, t[100], h[100], sumt = 0, sumh = 0, mint = 101, minh = 101;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> t[i];
      sumt += t[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> h[i];
      sumh += h[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (sumt - t[i] + h[j] == sumh - h[j] + t[i]) {
          if (h[j] + t[i] < mint + minh) {
            mint = t[i];
            minh = h[j];
          }
        }
      }
    }
    if (mint == 101 && minh == 101)
      cout << "-1" << endl;
    else
      cout << mint << " " << minh << endl;
  }
  return 0;
}
Problem p00734 (C++)

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

int main() {
  int n, m;
  while (cin >> n >> m, n && m) {
    int sum1 = 0, sum2 = 0;
    int s1[50], s2[50];
    int a = 1e8, b = 1e8;

    for (int i = 0; i < n; i++)
      cin >> s1[i], sum1 += s1[i];
    for (int i = 0; i < m; i++)
      cin >> s2[i], sum2 += s2[i];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if ((s1[i] - s2[j]) * 2 == sum1 - sum2 && a + b > s1[i] + s2[j])
          a = s1[i], b = s2[j];
      }
    }

    if (a == 1e8)
      cout << -1 << endl;
    else
      cout << a << " " << b << endl;
  }
}

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

int main() {
  int n, m;
  while (cin >> n >> m, n && m) {
    int sum1 = 0, sum2 = 0;
    int s1[100], s2[100];
    int a = 1e8, b = 1e8;

    for (int i = 0; i < n; i++)
      cin >> s1[i], sum1 += s1[i];
    for (int i = 0; i < m; i++)
      cin >> s2[i], sum2 += s2[i];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if ((s1[i] - s2[j]) * 2 == sum1 - sum2 && a + b > s1[i] + s2[j])
          a = s1[i], b = s2[j];
      }
    }

    if (a == 1e8)
      cout << -1 << endl;
    else
      cout << a << " " << b << endl;
  }
}
Problem p00735 (C++)

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

#define TEST 0
#if TEST
FILE *in = freopen("./Q.txt", "r", stdin);
FILE *out = freopen("./A.txt", "w", stdout);
#endif

#define MAXN 300000

vector<int> MSPrimes;
bool isNotMSPrime[MAXN];

void calMSPrime(int n) {
  for (int i = 2; i * n <= MAXN; i++) {
    isNotMSPrime[i * n] = true;
  }
}

int main(int argc, const char *argv[]) {

  for (int i = 2; i < MAXN; i++) {
    if (!isNotMSPrime[i] && (i % 7 == 1 || i % 7 == 6)) {
      calMSPrime(i);
      MSPrimes.push_back(i);
    }
  }

  while (1) {
    int n;
    cin >> n;
    if (n == 1)
      break;

    set<int> primes;
    for (unsigned id = 0; id < MSPrimes.size() && n >= MSPrimes[id]; id++) {
      if (n % MSPrimes[id] != 0)
        continue;

      int nowId = id, nowN = n;
      while (nowId < MSPrimes.size() && nowN >= MSPrimes[nowId]) {
        int nowPrime = MSPrimes[nowId];
        bool ok = false;

        while (nowN % nowPrime == 0) {
          ok = true;
          nowN /= nowPrime;
        }
        if (ok)
          primes.insert(nowPrime);
        nowId++;
      }
    }

    cout << n << ":";
    for (auto i = primes.begin(); i != primes.end(); i++) {
      cout << " " << *i;
    }
    cout << endl;
    primes.clear();
  }
#if TEST
  fclose(in);
  fclose(out);
#endif
}

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

#define TEST 0
#if TEST
FILE *in = freopen("./Q.txt", "r", stdin);
FILE *out = freopen("./A.txt", "w", stdout);
#endif

#define MAXN 300000

vector<int> MSPrimes;
bool isNotMSPrime[MAXN];

void calMSPrime(int n) {
  for (int i = 2; i * n < MAXN; i++) {
    isNotMSPrime[i * n] = true;
  }
}

int main(int argc, const char *argv[]) {

  for (int i = 2; i < MAXN; i++) {
    if (!isNotMSPrime[i] && (i % 7 == 1 || i % 7 == 6)) {
      calMSPrime(i);
      MSPrimes.push_back(i);
    }
  }

  while (1) {
    int n;
    cin >> n;
    if (n == 1)
      break;

    set<int> primes;
    for (unsigned id = 0; id < MSPrimes.size() && n >= MSPrimes[id]; id++) {
      if (n % MSPrimes[id] != 0)
        continue;

      int nowId = id, nowN = n;
      while (nowId < MSPrimes.size() && nowN >= MSPrimes[nowId]) {
        int nowPrime = MSPrimes[nowId];
        bool ok = false;

        while (nowN % nowPrime == 0) {
          ok = true;
          nowN /= nowPrime;
        }
        if (ok)
          primes.insert(nowPrime);
        nowId++;
      }
    }

    cout << n << ":";
    for (auto i = primes.begin(); i != primes.end(); i++) {
      cout << " " << *i;
    }
    cout << endl;
    primes.clear();
  }
#if TEST
  fclose(in);
  fclose(out);
#endif
}
Problem p00735 (C++)

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

using namespace std;

bool a[300001];

int main() {
  vector<int> p;
  p.push_back(6);
  for (int i = 1; i <= 50000; i++) {
    a[6 * i] = 1;
  }
  for (int i = 1; 7 * i + 1 <= 300000; i++) {
    if (a[7 * i + 1] == 0) {
      p.push_back(7 * i + 1);
      for (int j = 7 * i + 1; j <= 300000; j += 7 * i + 1) {
        a[j] = 1;
      }
    }
    if (a[7 * i + 6] == 0) {
      p.push_back(7 * i + 6);
      for (int j = 7 * i + 6; j <= 300000; j += 7 * i + 6) {
        a[j] = 1;
      }
    }
  }
  int n;
  while (cin >> n && n != 1) {
    vector<int> v;
    for (int i = 0; i < p.size(); i++) {
      if (n % p[i] == 0) {
        v.push_back(p[i]);
      }
    }
    cout << n << ":";
    for (int i = 0; i < v.size(); i++) {
      cout << " " << v[i];
    }
    cout << endl;
  }
  return 0;
}

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

using namespace std;

bool a[500001];

int main() {
  vector<int> p;
  p.push_back(6);
  for (int i = 1; i <= 50000; i++) {
    a[6 * i] = 1;
  }
  for (int i = 1; 7 * i + 1 <= 300000; i++) {
    if (a[7 * i + 1] == 0) {
      p.push_back(7 * i + 1);
      for (int j = 7 * i + 1; j <= 300000; j += 7 * i + 1) {
        a[j] = 1;
      }
    }
    if (a[7 * i + 6] == 0) {
      p.push_back(7 * i + 6);
      for (int j = 7 * i + 6; j <= 300000; j += 7 * i + 6) {
        a[j] = 1;
      }
    }
  }
  int n;
  while (cin >> n && n != 1) {
    vector<int> v;
    for (int i = 0; i < p.size(); i++) {
      if (n % p[i] == 0) {
        v.push_back(p[i]);
      }
    }
    cout << n << ":";
    for (int i = 0; i < v.size(); i++) {
      cout << " " << v[i];
    }
    cout << endl;
  }
  return 0;
}
Problem p00735 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const int MAX = 300005;
bool isPrime[MAX];
vector<int> mt;
void makePrime() {
  for (int i = 6; i < MAX; i++) {
    isPrime[i] = true;
  }
  for (int i = 2; i < MAX; i++) {
    if (isPrime[i] && (i % 7 == 6 || i % 7 == 1)) {
      mt.push_back(i);
      for (int j = 2 * i; j < MAX; j += i) {
        isPrime[j] = false;
      }
    }
  }
  return;
}
int main() {
  int n;
  makePrime();
  int len = mt.size();
  while (cin >> n, n) {
    set<int> SET;
    for (int i = 0; i < len; i++) {
      if (n % mt[i] == 0) {
        SET.insert(mt[i]);
      }
    }
    cout << n << ":";
    for (auto s : SET) {
      cout << " " << s;
    }
    cout << '\n';
  }
  return 0;
}

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const int MAX = 300005;
bool isPrime[MAX];
vector<int> mt;
void makePrime() {
  for (int i = 6; i < MAX; i++) {
    isPrime[i] = true;
  }
  for (int i = 2; i < MAX; i++) {
    if (isPrime[i] && (i % 7 == 6 || i % 7 == 1)) {
      mt.push_back(i);
      for (int j = 2 * i; j < MAX; j += i) {
        isPrime[j] = false;
      }
    }
  }
  return;
}
int main() {
  int n;
  makePrime();
  int len = mt.size();
  while (cin >> n, n != 1) {
    set<int> SET;
    for (int i = 0; i < len; i++) {
      if (n % mt[i] == 0) {
        SET.insert(mt[i]);
      }
    }
    cout << n << ":";
    for (auto s : SET) {
      cout << " " << s;
    }
    cout << '\n';
  }
  return 0;
}
Problem p00735 (C++)

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

const int size = 300000;

bool a[size] = {false};
std::vector<int> MS;

void eratos() {

  for (int i = 6; i < size; i++)
    a[i] = true;

  for (int i = 6; i < size; i++) {

    if (a[i] && (i % 7 == 1 || i % 7 == 6)) {

      MS.push_back(i);
      for (int j = i + i; j < size; j += i)
        a[j] = false;

    } else
      a[i] = false;
  }
}

void solve(int x) {
  std::vector<int>::iterator it = MS.begin();

  std::cout << x << ':';

  for (; it != MS.end(); ++it)
    if (x % (*it) == 0)
      std::cout << ' ' << *it;

  std::cout << std::endl;
}

int main() {
  int x;

  eratos();

  while (true) {

    std::cin >> x;

    if (x == 0)
      break;

    solve(x);
  }

  return 0;
}

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

const int size = 300000;

bool a[size] = {false};
std::vector<int> MS;

void eratos() {

  for (int i = 6; i < size; i++)
    a[i] = true;

  for (int i = 6; i < size; i++) {

    if (a[i] && (i % 7 == 1 || i % 7 == 6)) {

      MS.push_back(i);
      for (int j = i + i; j < size; j += i)
        a[j] = false;

    } else
      a[i] = false;
  }
}

void solve(int x) {
  std::vector<int>::iterator it = MS.begin();

  std::cout << x << ':';

  for (; it != MS.end(); ++it)
    if (x % (*it) == 0)
      std::cout << ' ' << *it;

  std::cout << std::endl;
}

int main() {
  int x;

  eratos();

  while (true) {

    std::cin >> x;

    if (x == 1)
      break;

    solve(x);
  }

  return 0;
}
Problem p00735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define r(i, a, n) for (int i = a; i < n; i++)
using namespace std;
int main() {
  int a[200000], c = 0, n;
  r(i, 3, 300000) if (i % 7 == 1 || i % 7 == 6) {
    int p = 1;
    if (i > 10 && (i % 6 == 0 || i % 8 == 0))
      continue;
    r(k, 0, c) if (i % a[k] == 0) p = 0;
    if (p)
      a[c++] = i;
  }
  r(i, 0, c) cout << a[i] << endl;
  while (cin >> n, n != 1) {
    int s = 0;
    cout << n << ':';
    r(i, 0, c) if (n % a[i] == 0) cout << ' ' << a[i];
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define r(i, a, n) for (int i = a; i < n; i++)
using namespace std;
int main() {
  int a[200000], c = 0, n;
  r(i, 3, 300000) if (i % 7 == 1 || i % 7 == 6) {
    int p = 1;
    if (i > 10 && (i % 6 == 0 || i % 8 == 0))
      continue;
    r(k, 0, c) if (i % a[k] == 0) p = 0;
    if (p)
      a[c++] = i;
  }
  while (cin >> n, n != 1) {
    int s = 0;
    cout << n << ':';
    r(i, 0, c) if (n % a[i] == 0) cout << ' ' << a[i];
    cout << endl;
  }
}
Problem p00740 (C++)

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

using namespace std;

int main() {
  while (true) {
    int n, pmax;
    int stone[50] = {};

    cin >> n >> pmax;

    int i;
    int p = pmax;
    for (i = 0; 1; i++) {
      if (p) {
        p--;
        stone[i % n]++;
        if (!p && stone[i % n] == pmax) {
          break;
        }
      } else {
        p += stone[i % n];
        stone[i % n] = 0;
      }
    }

    cout << i % n << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n, pmax;
    int stone[50] = {};

    cin >> n >> pmax;
    if (!n && !pmax)
      break;

    int i;
    int p = pmax;
    for (i = 0; 1; i++) {
      if (p) {
        p--;
        stone[i % n]++;
        if (!p && stone[i % n] == pmax) {
          break;
        }
      } else {
        p += stone[i % n];
        stone[i % n] = 0;
      }
    }

    cout << i % n << endl;
  }

  return 0;
}
Problem p00740 (C++)

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

using namespace std;

bool search(int p[], int N) {
  int count = 0;
  for (int i = 0; i < N; ++i) {
    if (p[i] != 0) {
      count++;
    }
  }
  if (count == 1) {
    return true;
  } else {
    return false;
  }
}

int main(void) {
  int n, p;
  while (cin >> n >> p) {
    int *people = new int[n];
    for (int i = 0; i < n; ++i) {
      people[i] = 0;
    }
    // finish initialization
    int turn = 0;
    while (1) {
      if (p >= 1) {
        ++people[turn];
        --p;
        if (p == 0) {
          if (search(people, n) == true) {
            break;
          }
        }
      } else { // p<=0]
        p = people[turn];
        people[turn] = 0;
      }
      if (turn == (n - 1)) {
        turn = 0;
      } else {
        ++turn;
      }
    }
    cout << turn << endl;
    delete[] people;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

bool search(int p[], int N) {
  int count = 0;
  for (int i = 0; i < N; ++i) {
    if (p[i] != 0) {
      count++;
    }
  }
  if (count == 1) {
    return true;
  } else {
    return false;
  }
}

int main(void) {
  int n, p;
  while (cin >> n >> p) {
    if ((n == 0) && (p == 0)) {
      return 0;
    }
    int *people = new int[n];
    for (int i = 0; i < n; ++i) {
      people[i] = 0;
    }
    // finish initialization
    int turn = 0;
    while (1) {
      if (p >= 1) {
        ++people[turn];
        --p;
        if (p == 0) {
          if (search(people, n) == true) {
            break;
          }
        }
      } else { // p<=0]
        p = people[turn];
        people[turn] = 0;
      }
      if (turn == (n - 1)) {
        turn = 0;
      } else {
        ++turn;
      }
    }
    cout << turn << endl;
    delete[] people;
  }
  return 0;
}
Problem p00740 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
#define ll long long
#define all(a) a.begin(), a.end()
#define ull unsigned long long
#define pb(x) push_back(x)
#define dbg(x) cout << #x << " : " << (x) << endl;
using namespace std;
const int INF = 1500000000;
const double PI = 3.14159265359;
const int mod = 1000000000 + 7;

const int MAX_N = 50, MAX_P = 50;
int n, p, res, v[MAX_N];

void solve() {
  memset(v, 0, n);
  int c = 0;
  while (true) {
    if (p == 0) {
      p = v[c];
      v[c] = 0;
    } else {
      p--;
      v[c]++;
      if (p == 0) {
        bool flg = true;
        rep(i, n) if (i != c && v[i] > 0) flg = false;
        if (flg)
          break;
      }
    }
    c = (c == n - 1) ? 0 : c + 1;
    res = c;
  }
}

int main() {
  while (true) {
    cin >> n >> p;
    if (!n && !p)
      break;
    solve();
    cout << res << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
#define ll long long
#define all(a) a.begin(), a.end()
#define ull unsigned long long
#define pb(x) push_back(x)
#define dbg(x) cout << #x << " : " << (x) << endl;
using namespace std;
const int INF = 1500000000;
const double PI = 3.14159265359;
const int mod = 1000000000 + 7;

const int MAX_N = 50, MAX_P = 50;
int n, p, res, v[MAX_N];

void solve() {
  rep(i, n) v[i] = 0;
  int c = 0;
  while (true) {
    if (p == 0) {
      p = v[c];
      v[c] = 0;
    } else {
      p--;
      v[c]++;
      if (p == 0) {
        bool flg = true;
        rep(i, n) if (i != c && v[i] > 0) flg = false;
        if (flg)
          break;
      }
    }
    c = (c == n - 1) ? 0 : c + 1;
    res = c;
  }
}

int main() {
  while (true) {
    cin >> n >> p;
    if (!n && !p)
      break;
    solve();
    cout << res << endl;
  }
  return 0;
}
Problem p00740 (C++)

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

using namespace std;
static const int MAXV = 1000;

int main() {
  while (1) {
    int n, m, p, M[MAXV] = {}, i = 0;
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    p = m;
    while (1) {
      printf("%d   %d   %d\n", M[0], M[1], M[2]);
      if (p == 0) {
        p = M[i];
        M[i] = 0;
      } else {
        M[i]++;
        p--;
      }
      if (M[i] == m)
        break;
      i++;
      if (i == n)
        i = 0;
    }
    printf("%d\n", i);
  }
}

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

using namespace std;
static const int MAXV = 1000;

int main() {
  while (1) {
    int n, m, p, M[MAXV] = {}, i = 0;
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    p = m;
    while (1) {
      // printf("%d   %d   %d\n", M[0], M[1], M[2]);
      if (p == 0) {
        p = M[i];
        M[i] = 0;
      } else {
        M[i]++;
        p--;
      }
      if (M[i] == m)
        break;
      i++;
      if (i == n)
        i = 0;
    }
    printf("%d\n", i);
  }
}
Problem p00740 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#define INF 100000000

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {-1, 0, 1, 0};
const int ty[] = {0, 1, 0, -1};

bool check(map<int, int> hand, int step, int n) {
  for (map<int, int>::iterator it = hand.begin(); it != hand.end(); it++) {
    if (it->first == step % n)
      continue;
    if (it->second != 0)
      return false;
  }
  return true;
}
int main() {
  int n, p;
  while (~scanf("%d %d", &n, &p)) {
    if (n == p && p == 0)
      break;
    map<int, int> hand;
    int bowl = p;
    int step = 0;
    int res = 0;
    while (1) {
      if (bowl > 0) {
        hand[step]++;
        bowl--;
        if (bowl == 0 && check(hand, step, n)) {
          res = step;
          break;
        }
      } else if (bowl == 0) {
        bowl += hand[step];
        hand[step] = 0;
      }
      step = (step + 1) % n;
    }

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

Fixed submission:
#define _USE_MATH_DEFINES
#define INF 100000000

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {-1, 0, 1, 0};
const int ty[] = {0, 1, 0, -1};

bool check(map<int, int> &hand, int step, int n) {
  for (map<int, int>::iterator it = hand.begin(); it != hand.end(); it++) {
    if (it->first == step % n)
      continue;
    if (it->second != 0)
      return false;
  }
  return true;
}
int main() {
  int n, p;
  while (~scanf("%d %d", &n, &p)) {
    if (n == p && p == 0)
      break;
    map<int, int> hand;
    int bowl = p;
    int step = 0;
    int res = 0;
    while (1) {
      if (bowl > 0) {
        hand[step]++;
        bowl--;
        if (bowl == 0 && check(hand, step, n)) {
          res = step;
          break;
        }
      } else if (bowl == 0) {
        bowl += hand[step];
        hand[step] = 0;
      }
      step = (step + 1) % n;
    }

    printf("%d\n", res);
  }
}
Problem p00740 (C++)

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

using namespace std;

int main() {
  int n, r;
  while (cin >> n >> r, n) {
    int p[n];
    fill(p, p + n, 0);
    int s = n;
    for (int i = 0;; i++, i %= n) {
      if (p[i] == r) {
        cout << i << endl;
        break;
      } else {
        if (s == 0) {
          s += p[i];
          p[i] = 0;
        } else {
          p[i]++;
          s--;
        }
      }
    }
  }
}

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

using namespace std;

int main() {
  int n, r;
  while (cin >> n >> r, n) {
    int p[n];
    fill(p, p + n, 0);
    int s = r;
    for (int i = 0;; i++, i %= n) {
      if (p[i] == r) {
        cout << i << endl;
        break;
      } else {
        if (s == 0) {
          s += p[i];
          p[i] = 0;
        } else {
          p[i]++;
          s--;
        }
      }
    }
  }
}
Problem p00740 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main() {
  int t[50], n, s, storn, i, j;
  while (1) {
    scanf("%d %d", &n, &storn);
    // printf("%d %d",n,storn);///
    if (n == 0 && storn == 0)
      break;
    for (i = 0; i < n; i++)
      t[i] = 0;
    j = 0;
    s = storn;
    while (1) {
      printf("%d %d %d %d\n", j, s, t[j], storn); ///
      if (s != 0) {
        t[j]++;
        s--;
      } else if (s == 0) {
        s = t[j];
        t[j] = 0;
      }
      if (t[j] == storn)
        break;
      j++;
      if (j == n)
        j = 0;
    }
    printf("%d\n", j);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  int t[50], n, s, storn, i, j;
  while (1) {
    scanf("%d %d", &n, &storn);
    // printf("%d %d",n,storn);///
    if (n == 0 && storn == 0)
      break;
    for (i = 0; i < n; i++)
      t[i] = 0;
    j = 0;
    s = storn;
    while (1) {
      // printf("%d %d %d %d\n",j,s,t[j],storn);///
      if (s != 0) {
        t[j]++;
        s--;
      } else if (s == 0) {
        s = t[j];
        t[j] = 0;
      }
      if (t[j] == storn)
        break;
      j++;
      if (j == n)
        j = 0;
    }
    printf("%d\n", j);
  }
  return 0;
}
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int needx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
int needy[] = {0, 1, 0, -1, 1, -1, 1, -1};
//??¶?????¨???false????????????true???????????¢??°//
bool land(int x, int y, vector<vector<bool>> &cango) {
  if (cango[x][y]) {
    cango[x][y] = false;
    for (int i = 0; i < 8; i++) {
      int nextx = x + needx[i];
      int nexty = y + needy[i];
      if (cango[nextx][nexty])
        bool flag = land(nextx, nexty, cango);
    }
    return true;
  } else {
    return false;
  }
}
int main(void) {
  int input;
  int w, h;
  while (1) {
    cin >> w >> h;
    if (h == 0)
      break;
    vector<vector<bool>> cango(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> input;
        if (input == 1) {
          cango[i][j] = true;
        }
      }
    }

    int t = 0;
    for (int i = 1; i <= w; i++) {
      for (int j = 1; j <= h; j++) {
        if (land(i, j, cango)) {
          t++;
        }
      }
    }
    cout << t << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int needx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
int needy[] = {0, 1, 0, -1, 1, -1, 1, -1};
//??¶?????¨???false????????????true???????????¢??°//
bool land(int x, int y, vector<vector<bool>> &cango) {
  if (cango[x][y]) {
    cango[x][y] = false;
    for (int i = 0; i < 8; i++) {
      int nextx = x + needx[i];
      int nexty = y + needy[i];
      if (cango[nextx][nexty])
        bool flag = land(nextx, nexty, cango);
    }
    return true;
  } else {
    return false;
  }
}
int main(void) {
  int input;
  int w, h;
  while (1) {
    cin >> w >> h;
    if (h == 0)
      break;
    vector<vector<bool>> cango(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> input;
        if (input == 1) {
          cango[i][j] = true;
        }
      }
    }

    int t = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (land(i, j, cango)) {
          t++;
        }
      }
    }
    cout << t << endl;
  }
  return 0;
}
Problem p00741 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

bool dfs(int sx, int sy, vector<vector<bool>> &c) {
  if (c[sx][sy] == false)
    return false;
  c[sx][sy] = false;
  for (int i = 0; i < 8; i++) {
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    bool flag = dfs(nx, ny, c);
  }
  return true;
}

int main(void) {
  int h, w, temp;
  while (1) {
    cin >> w >> h;
    if (w == 0)
      break;
    vector<vector<bool>> c(w + 2, vector<bool>(h + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> temp;
        if (temp == 1)
          c[i][j] = true;
      }
    }
    int t = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        bool flag = dfs(i, j, c);
        if (flag)
          t++;
      }
    }
    cout << t << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

bool dfs(int sx, int sy, vector<vector<bool>> &c) {
  if (c[sx][sy] == false)
    return false;
  c[sx][sy] = false;
  for (int i = 0; i < 8; i++) {
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    bool flag = dfs(nx, ny, c);
  }
  return true;
}

int main(void) {
  int h, w, temp;
  while (1) {
    cin >> w >> h;
    if (w == 0)
      break;
    vector<vector<bool>> c(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> temp;
        if (temp == 1)
          c[i][j] = true;
      }
    }
    int t = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        bool flag = dfs(i, j, c);
        if (flag)
          t++;
      }
    }
    cout << t << endl;
  }
  return 0;
}