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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MAX_N 100010
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 100000005
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef pair<int, int> PI;

int gcm(int a, int b) {
  if (a < b) {
    swap(a, b);
  }
  while (b > 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int lcm(int a, int b) { return a / gcm(a, b) * b; }

int main() {
  int n, a, b;
  int LCM, GCM;
  bool flag[MAX_N];
  int count[MAX_N];
  while (1) {
    cin >> n >> a >> b;
    if (n == 0)
      break;
    rep(i, MAX_N) { flag[i] = 0; }
    LCM = lcm(a, b);
    GCM = gcm(a, b);
    int ans;
    int irange = min(MAX_N - 1, LCM) / a + 1;
    rep(i, irange) {
      int jrange = (min(MAX_N - 1, LCM) - a * i) / b + 1;
      rep(j, jrange) { flag[a * i + b * j] = true; }
    }
    count[0] = 0;
    rep(i, MAX_N - 1) { count[i + 1] = count[i] + flag[i + 1]; }
    if (n < LCM) {
      ans = count[n];
    } else {
      ans = count[LCM] + (n - LCM) / GCM;
    }
    cout << n - ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MAX_N 1000010
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 100000005
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef pair<int, int> PI;

int gcm(int a, int b) {
  if (a < b) {
    swap(a, b);
  }
  while (b > 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int lcm(int a, int b) { return a / gcm(a, b) * b; }

int main() {
  int n, a, b;
  int LCM, GCM;
  bool flag[MAX_N];
  int count[MAX_N];
  while (1) {
    cin >> n >> a >> b;
    if (n == 0)
      break;
    rep(i, MAX_N) { flag[i] = 0; }
    LCM = lcm(a, b);
    GCM = gcm(a, b);
    int ans;
    int irange = min(MAX_N - 1, LCM) / a + 1;
    rep(i, irange) {
      int jrange = (min(MAX_N - 1, LCM) - a * i) / b + 1;
      rep(j, jrange) { flag[a * i + b * j] = true; }
    }
    count[0] = 0;
    rep(i, MAX_N - 1) { count[i + 1] = count[i] + flag[i + 1]; }
    if (n < LCM) {
      ans = count[n];
    } else {
      ans = count[LCM] + (n - LCM) / GCM;
    }
    cout << n - ans << endl;
  }
}
Problem p00687 (C++)

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

using namespace std;

bool ok(int n, int a, int b) {
  for (int i = 0; i <= n; i += a) {
    if ((n - i) % b == 0)
      return true;
  }
  return false;
}

int main() {
  int n, a, b;

  while (cin >> n >> a >> b, n | a | b) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (ok(i, a, b))
        continue;
      ans++;
    }
    cout << ans << endl;
  }
}

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

using namespace std;

bool ok(int n, int a, int b) {
  if (a < b)
    swap(a, b);
  for (int i = 0; i <= n; i += a) {
    if ((n - i) % b == 0)
      return true;
  }
  return false;
}

int main() {
  int n, a, b;

  while (cin >> n >> a >> b, n | a | b) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (ok(i, a, b))
        continue;
      ans++;
    }
    cout << ans << endl;
  }
}
Problem p00687 (C++)

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

using namespace std;

int main() {
  for (int n, a, b; cin >> n >> a >> b, n | a | b;) {
    static bool ap[1000001];
    fill(ap, ap + 1000001, false);
    for (int i = 0; i * a <= n; i++) {
      for (int j = 0; j * b + i * a <= n; j++) {
        ap[i * a + j * b] = true;
      }
    }
    cout << count(ap + 1, ap + 1 + n, false) << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  for (int n, a, b; cin >> n >> a >> b, n | a | b;) {
    static bool ap[1000001];
    fill(ap, ap + 1000001, false);
    ap[0] = true;
    for (int i = a; i <= n; i++) {
      ap[i] = ap[i - a];
    }
    for (int i = b; i <= n; i++) {
      ap[i] |= ap[i - b];
    }
    cout << count(ap + 1, ap + 1 + n, false) << endl;
  }
  return 0;
}
Problem p00687 (C++)

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define between(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

int main() {
  int n, a, b;
  while (scanf("%d%d%d", &n, &a, &b) && n + a + b) {
    bool f[1000001] = {};
    f[0] = 1;
    rep(i, n + 1) {
      if (!f[i])
        continue;
      f[i + a] = 1;
      f[i + b] = 1;
    }

    //    puts("error\n");
    int res = 0;
    rep(i, n + 1) if (!f[i]) res++;

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

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define between(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

int main() {
  int n, a, b;
  while (scanf("%d%d%d", &n, &a, &b) && n + a + b) {
    bool f[2000000] = {};
    f[0] = 1;
    rep(i, n + 1) {
      if (!f[i])
        continue;
      f[i + a] = 1;
      f[i + b] = 1;
    }

    //    puts("error\n");
    int res = 0;
    rep(i, n + 1) if (!f[i]) res++;

    printf("%d\n", res);
  }
  return 0;
}
Problem p00688 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int a, b, c;

void solve() {
  FOR(r, 1, 101) {
    FOR(p, r, 101) {
      FOR(q, -100, 101) {
        FOR(s, -100, 101) {
          if (r == p && q < s)
            break;
          if (p * r == a && p * s + r * q == b && q * s == c) {
            cout << p << " " << q << " " << r << " " << s << endl;
            return;
          }
        }
      }
    }
  }
  cout << "Impossible" << endl;
}

int main() {
  while (cin >> a >> b >> c && a)
    solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int a, b, c;

void solve() {
  FOR(p, 1, 10001) {
    if (a % p)
      continue;
    int r = a / p;
    FOR(q, -10001, 10001) {
      int s;
      if (q == 0)
        s = b / p;
      else
        s = c / q;
      if (p < r || (r == p && q < s))
        continue;
      if (p * r == a && p * s + r * q == b && q * s == c) {
        cout << p << " " << q << " " << r << " " << s << endl;
        return;
      }
    }
  }
  cout << "Impossible" << endl;
}

int main() {
  while (cin >> a >> b >> c && a)
    solve();
  return 0;
}
Problem p00691 (C++)

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

using namespace std;

int main() {
  int z;

  while (cin >> z, z) {
    int max_xy = 0;
    int z3 = z * z * z;

    for (int x = 1; x <= z3; ++x) {
      for (int y = x; y <= z3; ++y) {
        if (x * x * x + y * y * y <= z3)
          max_xy = max(max_xy, x * x * x + y * y * y);
        else
          break;
      }
    }

    cout << z3 - max_xy << endl;
  }

  return 0;
}

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

using namespace std;

int main() {
  int z;

  while (cin >> z, z) {
    int max_xy = 0;
    int z3 = z * z * z;

    for (int x = 1; x * x * x <= z3; ++x) {
      int x3 = x * x * x;
      for (int y = 1; y * y * y <= z3 - x3; ++y)
        max_xy = max(max_xy, x3 + y * y * y);
    }

    cout << z3 - max_xy << endl;
  }

  return 0;
}
Problem p00691 (C++)

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

int main() {
  long long int n;
  while (cin >> n, n) {
    long long int ans = 100000;
    for (long long int i = 1; pow(i, 3) <= pow(n, 3); i++) {
      for (long long int j = 1; pow(i, 3) + pow(j, 3) <= pow(n, 3); j++) {
        long long int m = pow(n, 3) - pow(i, 3) - pow(j, 3);
        ans = min(ans, m);
      }
    }
    cout << ans << endl;
  }
}

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

int main() {
  long long int n;
  while (cin >> n, n) {
    long long int ans = 100000;
    for (long long int i = 1; i * i * i <= n * n * n; i++) {
      for (long long int j = 1; i * i * i + j * j * j <= n * n * n; j++) {
        ans = min(ans, n * n * n - i * i * i - j * j * j);
      }
    }
    cout << ans << endl;
  }
}
Problem p00693 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool match(string s, string t) {
  for (int i = 0; i < 8; i++) {
    if (s[i] != '?' && s[i] != t[i])
      return false;
  }
  return true;
}
int n, m;
bool x[1111];
string r1[1111], r2[1111], a, b, c;
int main() {
  while (cin >> n >> m, n | m) {
    for (int i = 0; i < n; i++) {
      cin >> a >> r1[i] >> r2[i];
      if (a == "permit")
        x[i] = true;
      else
        x[i] = false;
    }
    int ret = 0;
    vector<string> v1, v2, v3;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      bool p1 = false, p2 = false;
      for (int j = n - 1; j >= 0; j++) {
        if (match(r1[j], a) && match(r2[j], b)) {
          if (x[j])
            p1 = true;
          else
            p2 = true;
          break;
        }
      }
      if (p1) {
        v1.push_back(a);
        v2.push_back(b);
        v3.push_back(c);
        ret++;
      }
    }
    cout << ret << endl;
    for (int i = 0; i < ret; i++) {
      cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << endl;
    }
  }
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool match(string s, string t) {
  for (int i = 0; i < 8; i++) {
    if (s[i] != '?' && s[i] != t[i])
      return false;
  }
  return true;
}
int n, m;
bool x[1111];
string r1[1111], r2[1111], a, b, c;
int main() {
  while (cin >> n >> m, n | m) {
    for (int i = 0; i < n; i++) {
      cin >> a >> r1[i] >> r2[i];
      if (a == "permit")
        x[i] = true;
      else
        x[i] = false;
    }
    int ret = 0;
    vector<string> v1, v2, v3;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      bool p1 = false, p2 = false;
      for (int j = n - 1; j >= 0; j--) {
        if (match(r1[j], a) && match(r2[j], b)) {
          if (x[j])
            p1 = true;
          else
            p2 = true;
          break;
        }
      }
      if (p1) {
        v1.push_back(a);
        v2.push_back(b);
        v3.push_back(c);
        ret++;
      }
    }
    cout << ret << endl;
    for (int i = 0; i < ret; i++) {
      cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << endl;
    }
  }
}
Problem p00693 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, m;
string S0[1000], S1[1000], S2[1000], T0[1000], T1[1000], T2[1000];
bool solve(string p1, string p2) {
  for (int i = 0; i < p1.size(); i++) {
    if (p1[i] != '?' && p1[i] != p2[i])
      return false;
  }
  return true;
}
int main() {
  while (true) {
    cin >> n >> m;
    vector<int> vec;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> S0[i] >> S1[i] >> S2[i];
    for (int i = 0; i < m; i++) {
      cin >> T1[i] >> T2[i] >> T0[i];
      for (int j = n - 1; j >= 0; j--) {
        bool P = solve(S1[j], T1[i]);
        if (P == true && solve(S2[j], T2[i]) == false)
          P = false;
        if (P == true && S0[j] == "deny")
          break;
        if (P == true && S0[j] == "permit") {
          vec.push_back(i);
          break;
        }
      }
    }
    cout << vec.size() << endl;
    for (int i = 0; i < vec.size(); i++)
      cout << T1[vec[i]] << ' ' << T2[vec[i]] << ' ' << T0[vec[i]] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, m;
string S0[5000], S1[5000], S2[5000], T0[5000], T1[5000], T2[5000];
bool solve(string p1, string p2) {
  for (int i = 0; i < p1.size(); i++) {
    if (p1[i] != '?' && p1[i] != p2[i])
      return false;
  }
  return true;
}
int main() {
  while (true) {
    cin >> n >> m;
    vector<int> vec;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> S0[i] >> S1[i] >> S2[i];
    for (int i = 0; i < m; i++) {
      cin >> T1[i] >> T2[i] >> T0[i];
      for (int j = n - 1; j >= 0; j--) {
        bool P = solve(S1[j], T1[i]);
        if (P == true && solve(S2[j], T2[i]) == false)
          P = false;
        if (P == true && S0[j] == "deny")
          break;
        if (P == true && S0[j] == "permit") {
          vec.push_back(i);
          break;
        }
      }
    }
    cout << vec.size() << endl;
    for (int i = 0; i < vec.size(); i++)
      cout << T1[vec[i]] << ' ' << T2[vec[i]] << ' ' << T0[vec[i]] << endl;
  }
  return 0;
}
Problem p00697 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

char s[9][5], t[3][3][5];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
  int res = 0;
  rep(i, 3) rep(j, 3) {
    if (t[i][j][0])
      continue;
    rep(k, 4) {
      rep(l, 4) {
        int nx = i + dx[l], ny = j + dy[l];
        if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || !t[nx][ny][0])
          continue;
        if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
          goto g;
      }
      if (p == 8)
        res++;
      else {
        rep(l, 4) t[i][j][l] = s[p][(l + k) % 4];
        res += dfs(p + 1);
        t[i][j][0] = 0;
      }
    g:;
    }
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    rep(j, 9) scanf("%s", s[i]);
    printf("%d\n", dfs(0));
  }
}

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

char s[9][5], t[3][3][5];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
  int res = 0;
  rep(i, 3) rep(j, 3) {
    if (t[i][j][0])
      continue;
    rep(k, 4) {
      rep(l, 4) {
        int nx = i + dx[l], ny = j + dy[l];
        if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || !t[nx][ny][0])
          continue;
        if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
          goto g;
      }
      if (p == 8)
        res++;
      else {
        rep(l, 4) t[i][j][l] = s[p][(l + k) % 4];
        res += dfs(p + 1);
        t[i][j][0] = 0;
      }
    g:;
    }
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    rep(j, 9) scanf("%s", s[j]);
    printf("%d\n", dfs(0));
  }
}
Problem p00697 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

string s[9], t[3][3];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
  int res = 0;
  rep(i, 3) rep(j, 3) {
    if (!t[i][j].empty())
      continue;
    rep(k, 4) {
      rep(l, 4) {
        int nx = i + dx[l], ny = j + dy[l];
        if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || t[nx][ny].empty())
          continue;
        char c = t[nx][ny][(l + 2) % 4];
        if (isupper(s[p][(l + k) % 4])) {
          if (isupper(c) || toupper(c) != s[p][(l + k) % 4])
            goto g;
        } else if (islower(c) || tolower(c) != s[p][(l + k) % 4])
          goto g;
      }
      if (p == 8)
        res++;
      else {
        rep(l, 4) t[i][j] += s[p][(l + k) % 4];
        res += dfs(p + 1);
        t[i][j].clear();
      }
    g:;
    }
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    rep(j, 9) cin >> s[j];
    printf("%d\n", dfs(0));
  }
}

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

string s[9], t[3][3];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
  int res = 0;
  rep(i, 3) rep(j, 3) {
    if (!t[i][j].empty())
      continue;
    rep(k, 4) {
      rep(l, 4) {
        int nx = i + dx[l], ny = j + dy[l];
        if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || t[nx][ny].empty())
          continue;
        if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
          goto g;
      }
      if (p == 8)
        res++;
      else {
        rep(l, 4) t[i][j] += s[p][(l + k) % 4];
        res += dfs(p + 1);
        t[i][j].clear();
      }
    g:;
    }
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    rep(j, 9) cin >> s[j];
    printf("%d\n", dfs(0));
  }
}
Problem p00701 (C++)

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

const int MAXN = 105;

int N;
int top[MAXN], bottom[MAXN];

int findTop(int x, vector<int> *v = NULL) {
  if (v != NULL)
    v->push_back(x);
  return top[x] == x ? x : findTop(top[x], v);
}

int getH(int x) { return bottom[x] == 0 ? 1 : getH(bottom[x]) + 1; }

void dispose(int x) {
  vector<int> v;
  findTop(x, &v);
  int y = bottom[x];
  if (y != 0)
    top[y] = y;
  for (int i = 0; i < v.size(); ++i) {
    top[v[i]] = v[i];
    bottom[v[i]] = 0;
  }
}

void move(int i, int j) {
  if (j == 0) {
    if (bottom[i] == 0)
      return;
    dispose(i);
  } else {
    if (findTop(i) == findTop(j) && getH(i) >= getH(j))
      return;
    if (top[i] != i)
      dispose(top[i]);
    j = findTop(j);
    bottom[i] = j;
    top[j] = i;
  }
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i <= N; ++i) {
      top[i] = i;
      bottom[i] = 0;
    }
    {
      int I, J;
      while (cin >> I >> J && (I | J)) {
        move(I, J);
      }
    }
    vector<int> h;
    int sum = 0;
    for (int i = 1; i <= N; ++i) {
      if (top[i] == i) {
        h.push_back(getH(i));
        sum += h.back();
      }
    }
    assert(sum == N);
    sort(h.begin(), h.end());
    for (int i = 0; i < h.size(); ++i) {
      cout << h[i] << endl;
    }
    cout << "end" << endl;
  }
  return 0;
}

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

const int MAXN = 105;

int N;
int top[MAXN], bottom[MAXN];

int findTop(int x, vector<int> *v = NULL) {
  if (v != NULL)
    v->push_back(x);
  return top[x] == x ? x : findTop(top[x], v);
}

int getH(int x) { return bottom[x] == 0 ? 1 : getH(bottom[x]) + 1; }

void dispose(int x) {
  vector<int> v;
  findTop(x, &v);
  int y = bottom[x];
  if (y != 0)
    top[y] = y;
  for (int i = 0; i < v.size(); ++i) {
    top[v[i]] = v[i];
    bottom[v[i]] = 0;
  }
}

void move(int i, int j) {
  if (j == 0) {
    if (bottom[i] == 0)
      return;
    dispose(i);
  } else {
    if (findTop(i) == findTop(j) && getH(i) >= getH(j))
      return;
    dispose(i);
    j = findTop(j);
    bottom[i] = j;
    top[j] = i;
  }
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i <= N; ++i) {
      top[i] = i;
      bottom[i] = 0;
    }
    {
      int I, J;
      while (cin >> I >> J && (I | J)) {
        move(I, J);
      }
    }
    vector<int> h;
    int sum = 0;
    for (int i = 1; i <= N; ++i) {
      if (top[i] == i) {
        h.push_back(getH(i));
        sum += h.back();
      }
    }
    assert(sum == N);
    sort(h.begin(), h.end());
    for (int i = 0; i < h.size(); ++i) {
      cout << h[i] << endl;
    }
    cout << "end" << endl;
  }
  return 0;
}
Problem p00702 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  cin.ignore();

  string kan[] = {"ld", "mb", "mp", "nc", "nd", "ng",
                  "nt", "nw", "ps", "qu", "cw", "ts"};
  map<string, int> index;
  for (int i = 0; i < 12; ++i)
    index.insert(make_pair(kan[i], 26 + i));

  vector<vector<int>> num(38, vector<int>(38, 0));
  for (;;) {
    string s;
    if (!(cin >> s))
      break;
    int n = s.size();

    vector<int> j;
    for (int i = 0; i < n; ++i) {
      if (i < n - 1 && index.find(s.substr(i, 2)) != index.end()) {
        j.push_back(index[s.substr(i, 2)]);
        ++i;
      } else {
        j.push_back(s[i] - 'a');
      }
    }

    for (unsigned i = 0; i < j.size() - 1; ++i)
      ++num[j[i]][j[i + 1]];
  }

  for (int i = 0; i < 38; ++i) {
    int k = 0;
    for (int j = 1; j < 38; ++j) {
      if (num[i][j] > num[i][k])
        k = j;
    }
    if (i < 26)
      cout << (char)('a' + i) << ' ';
    else
      cout << kan[i - 26] << ' ';
    if (k < 26)
      cout << (char)('a' + k) << ' ';
    else
      cout << kan[k - 26] << ' ';
    cout << num[i][k] << endl;
  }

  return 0;
}

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

int main() {
  int tmp;
  cin >> tmp;

  string kan[] = {"ld", "mb", "mp", "nc", "nd", "ng",
                  "nt", "nw", "ps", "qu", "cw", "ts"};
  map<string, int> index;
  for (int i = 0; i < 12; ++i)
    index.insert(make_pair(kan[i], 26 + i));

  vector<vector<int>> num(38, vector<int>(38, 0));
  for (;;) {
    string s;
    if (!(cin >> s))
      break;
    int n = s.size();

    vector<int> j;
    for (int i = 0; i < n; ++i) {
      if (i < n - 1 && index.find(s.substr(i, 2)) != index.end()) {
        j.push_back(index[s.substr(i, 2)]);
        ++i;
      } else {
        j.push_back(s[i] - 'a');
      }
    }

    for (unsigned i = 0; i < j.size() - 1; ++i)
      ++num[j[i]][j[i + 1]];
  }

  for (int i = 0; i < 38; ++i) {
    int k = 0;
    for (int j = 1; j < 38; ++j) {
      if (num[i][j] > num[i][k])
        k = j;
    }
    if (i < 26)
      cout << (char)('a' + i) << ' ';
    else
      cout << kan[i - 26] << ' ';
    if (k < 26)
      cout << (char)('a' + k) << ' ';
    else
      cout << kan[k - 26] << ' ';
    cout << num[i][k] << endl;
  }

  return 0;
}
Problem p00705 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
// #define N 101
#define N 11
using namespace std;

int main() {
  int n, q, m, d;
  int data[N];

  while (cin >> n >> q && (n || q)) {

    //    cout << "n="<<n<<" q="<<q << endl;

    for (int i = 0; i < N; i++)
      data[i] = 0;

    for (int i = 0; i < n; i++) {
      cin >> m;
      for (int i = 0; i < m; i++) {
        cin >> d;
        data[d]++;
      }
    }

    /*
    for( int i=0;i<N;i++ )
      cout << "  day"<<i <<"="<<data[i]<<" person" << endl;
    */

    int max = -1, mm = -1;
    for (d = 0; d < N; d++)
      if (max < data[d]) {
        max = data[d];
        mm = d;
      }

    if (max < q)
      cout << "0" << endl;
    else
      cout << mm << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
// #define N 101
#define N 101
using namespace std;

int main() {
  int n, q, m, d;
  int data[N];

  while (cin >> n >> q && (n || q)) {

    //    cout << "n="<<n<<" q="<<q << endl;

    for (int i = 0; i < N; i++)
      data[i] = 0;

    for (int i = 0; i < n; i++) {
      cin >> m;
      for (int i = 0; i < m; i++) {
        cin >> d;
        data[d]++;
      }
    }

    /*
    for( int i=0;i<N;i++ )
      cout << "  day"<<i <<"="<<data[i]<<" person" << endl;
    */

    int max = -1, mm = -1;
    for (d = 0; d < N; d++)
      if (max < data[d]) {
        max = data[d];
        mm = d;
      }

    if (max < q)
      cout << "0" << endl;
    else
      cout << mm << endl;
  }

  return 0;
}
Problem p00705 (Python)

Buggy submission (status: Runtime Error):
import collections

while True:
    N, Q = map(int, input().split())
    if N == 0 and Q == 0:
        break
    D = []
    for _ in range(N):
        i = list(map(int, input().split()))
        del i[0]
        [D.append(ii) for ii in i]
    c = collections.Counter(D)
    cc = c.most_common()
    if cc[0][1] >= Q:
        print(cc[0][0])
    else:
        print(0)


Fixed submission:
import collections

while True:
    N, Q = map(int, input().split())
    if N == 0 and Q == 0:
        break
    D = []
    for _ in range(N):
        i = list(map(int, input().split()))
        del i[0]
        [D.append(ii) for ii in i]
    if not len(D) == 0:
        c = collections.Counter(D)
        cc = c.most_common()
        # print(cc)
        if cc[0][1] >= Q:
            ans = 1000000
            tmp = cc[0][1]
            for i in range(len(cc)):
                if cc[i][1] == tmp:
                    ans = min(cc[i][0], ans)
            print(ans)
        else:
            print(0)
    else:
        print(0)
Problem p00705 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#pragma warning(disable : 4996)
int N, Q, M, D, R[100];
int main() {
  while (true) {
    scanf("%d%d", &N, &Q);
    if (N == 0 && Q == 0)
      break;
    for (int i = 0; i < 100; i++)
      R[i] = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d", &M);
      for (int j = 0; j < M; j++) {
        scanf("%d", &D);
        R[--D]++;
      }
    }
    int ptr = 0;
    for (int i = 0; i < 100;) {
      if (R[i] >= Q) {
        if (ptr == 0) {
          ptr = i + 1;
        } else if (R[i] > R[ptr - 1]) {
          ptr = i + 1;
        }
      }
    }
    printf("%d\n", ptr);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#pragma warning(disable : 4996)
int N, Q, M, D, R[100];
int main() {
  while (true) {
    scanf("%d%d", &N, &Q);
    if (N == 0 && Q == 0)
      break;
    for (int i = 0; i < 100; i++)
      R[i] = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d", &M);
      for (int j = 0; j < M; j++) {
        scanf("%d", &D);
        R[--D]++;
      }
    }
    int ptr = 0;
    for (int i = 0; i < 100; i++) {
      if (R[i] >= Q) {
        if (ptr == 0) {
          ptr = i + 1;
        } else if (R[i] > R[ptr - 1]) {
          ptr = i + 1;
        }
      }
    }
    printf("%d\n", ptr);
  }
  return 0;
}
Problem p00705 (C++)

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

#define MAX 50

using namespace std;
int main() {
  int n, q, m;
  int d[MAX], date;
  while (cin >> n >> q) {
    if (n == 0 && q == 0)
      break;
    memset(d, 0, sizeof(d));

    while (n--) {
      cin >> m;
      for (int i = 0; i < m; i++) {
        cin >> date;
        d[date]++;
      }
    }
    bool flag = 0;
    int ans = 0, tmp = 0;

    for (int i = 1; i < MAX; i++) {
      if (d[i] >= q) {
        if (i >= ans && d[i] > tmp) {
          ans = i;
          tmp = d[i];
          flag = 1;
        }
      }
    }

    if (flag == 1)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

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

#define MAX 100

using namespace std;
int main() {
  int n, q, m;
  int d[MAX], date;
  while (cin >> n >> q) {
    if (n == 0 && q == 0)
      break;
    memset(d, 0, sizeof(d));

    while (n--) {
      cin >> m;
      for (int i = 0; i < m; i++) {
        cin >> date;
        d[date]++;
      }
    }
    bool flag = 0;
    int ans = 0, tmp = 0;

    for (int i = 1; i < MAX; i++) {
      if (d[i] >= q) {
        if (i >= ans && d[i] > tmp) {
          ans = i;
          tmp = d[i];
          flag = 1;
        }
      }
    }

    if (flag == 1)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}
Problem p00706 (C++)

Buggy submission (status: Runtime Error):
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1125

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;

int N, W, H, S, T;
bool field[110][110];

void init() { fill(&field[0][0], &field[110][110], false); }

int trees(int o_x, int o_y) {
  int count = 0;
  for (int i = o_x; i < o_x + S; i++) {
    for (int j = o_y; j < o_y + T; j++) {
      if (field[i][j]) {
        count++;
      }
    }
  }
  return count++;
}

int main() {
  while (scanf("%d", &N) != EOF && N) {
    init();
    scanf("%d%d", &W, &H);
    int x, y;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &x, &y);
      x--;
      y--; // 0からにする。
      field[x][y] = true;
    }
    scanf("%d%d", &S, &T);
    int max = 0;
    for (int i = 0; i + S <= W; i++) {
      for (int j = 0; j + T <= H; j++) {
        int temp = trees(i, j);
        if (max < temp) {
          max = temp;
        }
      }
    }
    printf("%d\n", max);
  }
}

Fixed submission:
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1125

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;

int N, W, H, S, T;
bool field[110][110];

void init() { fill(&field[0][0], &field[0][0] + 110 * 110, false); }

int trees(int o_x, int o_y) {
  int count = 0;
  for (int i = o_x; i < o_x + S; i++) {
    for (int j = o_y; j < o_y + T; j++) {
      if (field[i][j]) {
        count++;
      }
    }
  }
  return count++;
}

int main() {
  while (scanf("%d", &N) != EOF && N) {
    init();
    scanf("%d%d", &W, &H);
    int x, y;
    for (int i = 0; i < N; i++) {
      scanf("%d%d", &x, &y);
      x--;
      y--; // 0からにする。
      field[x][y] = true;
    }
    scanf("%d%d", &S, &T);
    int max = 0;
    for (int i = 0; i + S <= W; i++) {
      for (int j = 0; j + T <= H; j++) {
        int temp = trees(i, j);
        if (max < temp) {
          max = temp;
        }
      }
    }
    printf("%d\n", max);
  }
}
Problem p00706 (C++)

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

int main() {
  int i, j, n, w, h, s, t, wx, hy, map[101][101];
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int datax[101];
    int datay[101];
    cin >> w >> h;
    for (i = 1; i <= n; i++) {
      cin >> wx >> hy;
      map[hy][wx] = 1;
      datax[i] = wx;
      datay[i] = hy;
    }
    cin >> s >> t;
    int max = -1;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        int count = 0;
        for (int k = 1; k <= n; k++) {
          if (datay[k] >= i && datay[k] < i + t) {
            if (datax[k] >= j && datax[k] < j + s) {
              count++;
              if (max < count)
                max = count;
            }
          }
        }
      }
    }
    cout << max << endl;
  }
  return 0;
}

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

int main() {
  int i, j, n, w, h, s, t, wx, hy, map[101][101];
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int datax[501];
    int datay[501];
    cin >> w >> h;
    for (i = 1; i <= n; i++) {
      cin >> wx >> hy;
      map[hy][wx] = 1;
      datax[i] = wx;
      datay[i] = hy;
    }
    cin >> s >> t;
    int max = -1;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        int count = 0;
        for (int k = 1; k <= n; k++) {
          if (datay[k] >= i && datay[k] < i + t) {
            if (datax[k] >= j && datax[k] < j + s) {
              count++;
              if (max < count)
                max = count;
            }
          }
        }
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00706 (C++)

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  while (1) {
    int n, w, h;
    cin >> n >> w >> h;
    vector<vector<int>> ruiseki(h + 1, vector<int>(w + 1, 0));
    int x, y;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      ruiseki[y][x]++;
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        ruiseki[i][j] +=
            +ruiseki[i - 1][j] + ruiseki[i][j - 1] - ruiseki[i - 1][j - 1];
      }
    }
    int s, t;
    cin >> t >> s;
    int ans = 0;
    for (int i = s; i <= h; i++) {
      for (int j = t; j <= w; j++) {
        ans = max(ans, ruiseki[i][j] - ruiseki[i - s][j] - ruiseki[i][j - t] +
                           ruiseki[i - s][j - t]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main() {
  while (1) {
    int n, w, h;
    cin >> n >> w >> h;
    if (n == 0)
      return 0;
    vector<vector<int>> ruiseki(h + 1, vector<int>(w + 1, 0));
    int x, y;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      ruiseki[y][x]++;
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        ruiseki[i][j] +=
            +ruiseki[i - 1][j] + ruiseki[i][j - 1] - ruiseki[i - 1][j - 1];
      }
    }
    int s, t;
    cin >> t >> s;
    int ans = 0;
    for (int i = s; i <= h; i++) {
      for (int j = t; j <= w; j++) {
        ans = max(ans, ruiseki[i][j] - ruiseki[i - s][j] - ruiseki[i][j - t] +
                           ruiseki[i - s][j - t]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00706 (C++)

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define SORT(v) sort(ALL(v))
// #define scanf scanf_s
typedef pair<int, int> P;

int n;
int w, h;
int tizu[105][105];
int main() {
  while (cin >> n, n) {
    memset(tizu, 0, sizeof(tizu));
    cin >> w >> h;
    REP(i, n) {
      int x, y;
      cin >> x >> y;
      tizu[y - 1][x - 1] = 1;
    }
    int s, t;
    cin >> s >> t;
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int cnt = 0;
        REP(k, t) REP(l, s) { cnt += tizu[k + i][l + j]; }
        ans = max(cnt, ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define SORT(v) sort(ALL(v))
// #define scanf scanf_s
typedef pair<int, int> P;

int n;
int w, h;
int tizu[300][300];
int main() {
  while (cin >> n, n) {
    memset(tizu, 0, sizeof(tizu));
    cin >> w >> h;
    REP(i, n) {
      int x, y;
      cin >> x >> y;
      tizu[y - 1][x - 1] = 1;
    }
    int s, t;
    cin >> s >> t;
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int cnt = 0;
        REP(k, t) REP(l, s) { cnt += tizu[k + i][l + j]; }
        ans = max(cnt, ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00706 (C++)

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

int main(void) {

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

    cerr << endl;
    int w, h;
    cin >> w >> h;
    vector<vector<bool>> a(h, vector<bool>(w, false));
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      a[y - 1][x - 1] = true;
    }
    int s, t;
    cin >> s >> t;

    int mx = 0;
    for (int i = 0; i <= h - t; i++) {
      for (int j = 0; j <= w - s; j++) {
        int cnt = 0;
        for (int k = 0; k < t; k++) {
          for (int l = 0; l < s; l++) {
            if (a[i + k][j + l])
              cnt++;
          }
        }
        mx = max(mx, cnt);
      }
    }
    cout << mx << endl;
  }

  return 0;
}

//

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

int main(void) {

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

    int w, h;
    cin >> w >> h;
    vector<vector<bool>> a(h, vector<bool>(w, false));
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      a[y - 1][x - 1] = true;
    }
    int s, t;
    cin >> s >> t;

    int mx = 0;
    for (int i = 0; i <= h - t; i++) {
      for (int j = 0; j <= w - s; j++) {
        int cnt = 0;
        for (int k = 0; k < t; k++) {
          for (int l = 0; l < s; l++) {
            if (a[i + k][j + l])
              cnt++;
          }
        }
        mx = max(mx, cnt);
      }
    }
    cout << mx << endl;
  }

  return 0;
}

//
Problem p00706 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  int n;
  while (cin >> n && n) {
    int w, h;
    cin >> w >> h;
    bool field[w][h];
    fill(field[0], field[w], 0);
    for (int i = 0; i < n; ++i) {
      int x, y;
      cin >> x >> y;
      field[x][y] = 1;
    }
    int s, t;
    cin >> s >> t;
    int res = 0;
    for (int sy = 0; sy <= h - t; ++sy) {
      for (int sx = 0; sx <= w - s; ++sx) {
        int count = 0;
        for (int dy = 0; dy < t; ++dy) {
          for (int dx = 0; dx < s; ++dx) {
            count += field[sx + dx][sy + dy];
          }
        }
        res = max(res, count);
      }
    }
    cout << res << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  int n;
  while (cin >> n && n) {
    int w, h;
    cin >> w >> h;
    bool field[w][h];
    fill(field[0], field[w], 0);
    for (int i = 0; i < n; ++i) {
      int x, y;
      cin >> x >> y;
      field[x - 1][y - 1] = 1;
    }
    int s, t;
    cin >> s >> t;
    int res = 0;
    for (int sy = 0; sy <= h - t; ++sy) {
      for (int sx = 0; sx <= w - s; ++sx) {
        int count = 0;
        for (int dy = 0; dy < t; ++dy) {
          for (int dx = 0; dx < s; ++dx) {
            count += field[sx + dx][sy + dy];
          }
        }
        res = max(res, count);
      }
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00706 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
  while (true) {
    int N, W, H;
    cin >> N;
    if (N == 0)
      break;
    cin >> W >> H;
    vector<vector<bool>> m(H, vector<bool>(W, false));
    vector<vector<int>> wa(H + 1, vector<int>(W, 0));
    REP(i, N) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      m[y][x] = true;
    }
    REP(i, H) {
      REP(j, W) { wa[i + 1][j + 1] += wa[i + 1][j] + (int)m[i][j]; }
    }
    REP(i, H) {
      REP(j, W) { wa[i + 1][j + 1] += wa[i][j + 1]; }
    }
    int S, T;
    cin >> S >> T;
    int ans = 0;
    REP(i, H - T + 1) {
      REP(j, W - S + 1) {
        ans =
            max(ans, wa[T + i][S + j] - wa[T + i][j] - wa[i][S + j] + wa[i][j]);
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
  while (true) {
    int N, W, H;
    cin >> N;
    if (N == 0)
      break;
    cin >> W >> H;
    vector<vector<bool>> m(H, vector<bool>(W, false));
    vector<vector<int>> wa(H + 1, vector<int>(W + 1, 0));
    REP(i, N) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      m[y][x] = true;
    }
    REP(i, H) {
      REP(j, W) { wa[i + 1][j + 1] += wa[i + 1][j] + (int)m[i][j]; }
    }
    REP(i, H) {
      REP(j, W) { wa[i + 1][j + 1] += wa[i][j + 1]; }
    }
    int S, T;
    cin >> S >> T;
    int ans = 0;
    REP(i, H - T + 1) {
      REP(j, W - S + 1) {
        ans =
            max(ans, wa[T + i][S + j] - wa[T + i][j] - wa[i][S + j] + wa[i][j]);
      }
    }
    cout << ans << endl;
  }
}
Problem p00706 (C++)

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

using namespace std;

#define int long long

int H, W, S, T;
bool fld[100][100];

int Count(int x, int y) {
  int res = 0;
  for (int i = y; i < y + T; i++) {
    for (int j = x; j < x + S; j++) {
      res += fld[i][j];
    }
  }
  return res;
}

signed main() {
  int N;
  while (cin >> N, N) {
    fill_n((bool *)fld, 100 * 1000, false);
    cin >> W >> H;
    for (int i = 0; i < N; i++) {
      int X, Y;
      cin >> X >> Y;
      X--;
      Y--;
      fld[Y][X] = true;
    }
    cin >> S >> T;

    int ma = 0;
    for (int i = 0; i <= H - T; i++) {
      for (int j = 0; j <= W - S; j++) {
        ma = max(ma, Count(j, i));
      }
    }
    printf("%lld\n", ma);
  }
}

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

using namespace std;

#define int long long

int H, W, S, T;
bool fld[100][100];

int Count(int x, int y) {
  int res = 0;
  for (int i = y; i < y + T; i++) {
    for (int j = x; j < x + S; j++) {
      res += fld[i][j];
    }
  }
  return res;
}

signed main() {
  int N;
  while (cin >> N, N) {
    fill_n((bool *)fld, 100 * 100, false);
    cin >> W >> H;
    for (int i = 0; i < N; i++) {
      int X, Y;
      cin >> X >> Y;
      X--;
      Y--;
      fld[Y][X] = true;
    }
    cin >> S >> T;

    int ma = 0;
    for (int i = 0; i <= H - T; i++) {
      for (int j = 0; j <= W - S; j++) {
        ma = max(ma, Count(j, i));
      }
    }
    printf("%lld\n", ma);
  }
}
Problem p00707 (C++)

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

const int WH = 72;

char board[WH];
string dp[WH];

bool comp(const string &a, const string &b) {
  if (a.length() == b.length()) {
    return a < b;
  }
  return a.length() < b.length();
}

int main() {
  int w, h, m;
  string r;
  while (cin >> w >> h, w) {
    r = "";
    for (int i = 0; i < w * h; ++i) {
      cin >> board[i];
    }
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        dp[i * w + j] = "";
        if (i - 1 >= 0 && dp[(i - 1) * w + j] != "") {
          dp[i * w + j] = dp[(i - 1) * w + j];
        }
        if (j - 1 >= 0 && dp[i * w + (j - 1)] != "") {
          dp[i * w + j] = max(dp[i * w + j], dp[i * w + (j - 1)], comp);
        }

        if ('0' <= board[i * w + j] && board[i * w + j] <= '9') {
          if (dp[i * w + j] != "" || board[i * w + j] != '0') {
            dp[i * w + j] += board[i * w + j];
          }
        } else
          dp[i * w + j] = "";

        r = max(r, dp[i * w + j], comp);
      }
    }
    cout << r << endl;
  }
  return 0;
}

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

const int WH = 4900;

char board[WH];
string dp[WH];

bool comp(const string &a, const string &b) {
  if (a.length() == b.length()) {
    return a < b;
  }
  return a.length() < b.length();
}

int main() {
  int w, h, m;
  string r;
  while (cin >> w >> h, w) {
    r = "";
    for (int i = 0; i < w * h; ++i) {
      cin >> board[i];
    }
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        dp[i * w + j] = "";
        if (i - 1 >= 0 && dp[(i - 1) * w + j] != "") {
          dp[i * w + j] = dp[(i - 1) * w + j];
        }
        if (j - 1 >= 0 && dp[i * w + (j - 1)] != "") {
          dp[i * w + j] = max(dp[i * w + j], dp[i * w + (j - 1)], comp);
        }

        if ('0' <= board[i * w + j] && board[i * w + j] <= '9') {
          if (dp[i * w + j] != "" || board[i * w + j] != '0') {
            dp[i * w + j] += board[i * w + j];
          }
        } else
          dp[i * w + j] = "";

        r = max(r, dp[i * w + j], comp);
      }
    }
    cout << r << endl;
  }
  return 0;
}