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

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

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

const int INF = 100000000;

string m = "012345678";
P getP(char c) {
  int p = m[c - 'A'] - '0';
  return MP(p % 3, p / 3);
}

int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};

int main() {
  while (1) {
    int n;
    char s, t, b;
    cin >> n;
    if (!n)
      break;
    cin >> s >> t >> b;

    P sp = getP(s);
    P tp = getP(t);
    P bp = getP(b);

    double p[15][3][3] = {};

    p[0][sp.Y][sp.X] = 1.0;

    for (int i = 1; i <= n; i++) {
      rep(j, 3) {
        rep(k, 3) {
          if (MP(k, j) == bp)
            continue;
          int wallcnt = 0;
          rep(l, 4) {
            int x = k + dx[l];
            int y = j + dy[l];
            if (x < 0 || x > 2 || y < 0 || y > 2 || MP(x, y) == bp) {
              wallcnt++;
              continue;
            }
            p[i][j][k] += p[i - 1][y][x] / 4;
          }
          p[i][j][k] += p[i - 1][j][k] * wallcnt / 4;
        }
      }
    }

    printf("%.8lf\n", p[n][tp.Y][tp.X]);
  }

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

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

const int INF = 100000000;

string m = "012345678";
P getP(char c) {
  int p = m[c - 'A'] - '0';
  return MP(p % 3, p / 3);
}

int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};

int main() {
  while (1) {
    int n;
    char s, t, b;
    cin >> n;
    if (!n)
      break;
    cin >> s >> t >> b;

    P sp = getP(s);
    P tp = getP(t);
    P bp = getP(b);

    double p[16][3][3] = {};

    p[0][sp.Y][sp.X] = 1.0;

    for (int i = 1; i <= n; i++) {
      rep(j, 3) {
        rep(k, 3) {
          if (MP(k, j) == bp)
            continue;
          int wallcnt = 0;
          rep(l, 4) {
            int x = k + dx[l];
            int y = j + dy[l];
            if (x < 0 || x > 2 || y < 0 || y > 2 || MP(x, y) == bp) {
              wallcnt++;
              continue;
            }
            p[i][j][k] += p[i - 1][y][x] / 4;
          }
          p[i][j][k] += p[i - 1][j][k] * wallcnt / 4;
        }
      }
    }

    printf("%.8lf\n", p[n][tp.Y][tp.X]);
  }

  return 0;
}
Problem p00606 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

double dp[3][3][16];
int n, s, t, b;

int toIndex(char l) { return l - 'A'; }

double rec(int x, int y, int battery) {
  if (dp[y][x][battery] > -1e-6) {
    return dp[y][x][battery];
  }
  if (x == t % 3 && y == t / 3 && battery == n) {
    return 1.0;
  }
  if (battery >= n) {
    return 0.0;
  }

  double res = 0.0;
  REP(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 &&
        !(nx == b % 3 && ny == b / 3)) {
      res += 0.25 * rec(nx, ny, battery + 1);
    } else {
      res += 0.25 * rec(x, y, battery + 1);
    }
  }

  return res;
}

int main() {
  while (std::cin >> n, n) {
    std::fill(dp[0][0], dp[0][0] + 3 * 3 * 16, -1.0);

    char sl, tl, bl;
    std::cin >> sl >> tl >> bl;

    s = toIndex(sl), t = toIndex(tl), b = toIndex(bl);

    printf("%.7f\n", rec(s % 3, s / 3, 0));
  }
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

double dp[3][3][16];
int n, s, t, b;

int toIndex(char l) { return l - 'A'; }

double rec(int x, int y, int battery) {
  if (dp[y][x][battery] > -1e-6) {
    return dp[y][x][battery];
  }
  if (x == t % 3 && y == t / 3 && battery == n) {
    return 1.0;
  }
  if (battery >= n) {
    return 0.0;
  }

  double res = 0.0;
  REP(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 &&
        !(nx == b % 3 && ny == b / 3)) {
      res += 0.25 * rec(nx, ny, battery + 1);
    } else {
      res += 0.25 * rec(x, y, battery + 1);
    }
  }

  return dp[y][x][battery] = res;
}

int main() {
  while (std::cin >> n, n) {
    std::fill(dp[0][0], dp[0][0] + 3 * 3 * 16, -1.0);

    char sl, tl, bl;
    std::cin >> sl >> tl >> bl;

    s = toIndex(sl), t = toIndex(tl), b = toIndex(bl);

    printf("%.7f\n", rec(s % 3, s / 3, 0));
  }
}
Problem p00609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  int a, b, R;
  while (cin >> a >> b >> R, a || b) {
    vector<long long int> A_x(a), A_y(a);
    for (int i = 0; i < a; ++i)
      cin >> A_x[i] >> A_y[i];
    vector<vector<long long int>> M(100010);
    for (int i = 0; i < b; ++i) {
      long long int x, y;
      cin >> x >> y;
      M[x].push_back(y);
    }
    for (auto &x : M)
      sort(x.begin(), x.end());
    long long int ans = 0;
    for (int i = 0; i < a; ++i) {
      long long int x = A_x[i], y = A_y[i];
      for (long long int j = x - 4 * R; j <= x + 4 * R; ++j) {
        long long int d = floor(sqrt(16 * R * R - (x - j) * (x - j)) + 1e-8);
        ans += upper_bound(M[j].begin(), M[j].end(), y + d) -
               lower_bound(M[j].begin(), M[j].end(), y - d);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  int a, b, R;
  while (cin >> a >> b >> R, a || b) {
    vector<long long int> A_x(a), A_y(a);
    for (int i = 0; i < a; ++i)
      cin >> A_x[i] >> A_y[i];
    vector<vector<long long int>> M(100010);
    for (int i = 0; i < b; ++i) {
      long long int x, y;
      cin >> x >> y;
      M[x].push_back(y);
    }
    for (auto &x : M)
      sort(x.begin(), x.end());
    long long int ans = 0;
    for (int i = 0; i < a; ++i) {
      long long int x = A_x[i], y = A_y[i];
      for (long long int j = max(0LL, x - 4 * R); j <= min(100000LL, x + 4 * R);
           ++j) {
        long long int d = floor(sqrt(16 * R * R - (x - j) * (x - j)) + 1e-8);
        ans += upper_bound(M[j].begin(), M[j].end(), y + d) -
               lower_bound(M[j].begin(), M[j].end(), y - d);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX 10005
set<int> G[MAX];
int n, m, x, y, R;
int main() {
  while (1) {
    scanf("%d %d %d", &n, &m, &R);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < MAX; i++)
      G[i].clear();

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &x, &y);
      G[x].insert(y);
    }

    int ans = 0;
    while (m--) {
      scanf("%d %d", &x, &y);
      set<int>::iterator it, it2;
      for (int px = x - R * 4; px <= x + R * 4; px++) {
        int py = y - R * 4;
        it = G[px].lower_bound(py);
        it2 = G[px].end();
        while (it != it2 && (*it) <= y + R * 4) {
          int X = px - x;
          int Y = (*it) - y;
          if (16 * R * R >= X * X + Y * Y) {
            ans++;
          }
          it++;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX 10005
set<int> G[MAX];
int n, m, x, y, R;
int main() {
  while (1) {
    scanf("%d %d %d", &n, &m, &R);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < MAX; i++)
      G[i].clear();

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &x, &y);
      G[x].insert(y);
    }

    int ans = 0;
    while (m--) {
      scanf("%d %d", &x, &y);
      set<int>::iterator it, it2;
      for (int px = x - R * 4; px <= x + R * 4; px++) {
        if (px < 0 || MAX <= px)
          continue;

        int py = y - R * 4;
        it = G[px].lower_bound(py);
        it2 = G[px].end();
        while (it != it2 && (*it) <= y + R * 4) {
          int X = px - x;
          int Y = (*it) - y;
          if (16 * R * R >= X * X + Y * Y) {
            ans++;
          }
          it++;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00609 (C++)

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

int A, B, R;

vector<int> P[11000];

bool check(int x, int y, int x1, int y1) {
  if ((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y) > 16 * R * R)
    return false;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> A >> B >> R && (A || B || R)) {
    for (int i = 0; i <= 10000; i++)
      P[i].clear();
    for (int i = 0; i < A; i++) {
      int x, y;
      cin >> x >> y;
      P[x].push_back(y);
    }

    for (int i = 0; i <= 10000; i++)
      sort(P[i].begin(), P[i].end());

    int res = 0;
    for (int i = 0; i < B; i++) {
      int x, y;
      cin >> x >> y;
      // cout << x << " " << y << " : " << endl;
      for (int nx = x - 4 * R; nx <= x + 4 * R; nx++) {
        int id =
            lower_bound(P[nx].begin(), P[nx].end(), y - 4 * R) - P[nx].begin();
        for (int j = id; j < P[nx].size(); j++) {
          // cout << nx << " "<< P[nx][j] << endl;
          if (P[nx][j] > y + 4 * R)
            break;
          if (check(nx, P[nx][j], x, y))
            res++;
        }
      }
    }
    cout << res << endl;
  }
}

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

int A, B, R;

vector<int> P[11000];

bool check(int x, int y, int x1, int y1) {
  if ((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y) > 16 * R * R)
    return false;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> A >> B >> R && (A || B || R)) {
    for (int i = 0; i <= 10000; i++)
      P[i].clear();
    for (int i = 0; i < A; i++) {
      int x, y;
      cin >> x >> y;
      P[x].push_back(y);
    }

    for (int i = 0; i <= 10000; i++)
      sort(P[i].begin(), P[i].end());

    int res = 0;
    for (int i = 0; i < B; i++) {
      int x, y;
      cin >> x >> y;
      // cout << x << " " << y << " : " << endl;
      for (int nx = max(0, x - 4 * R); nx <= x + 4 * R; nx++) {
        int id =
            lower_bound(P[nx].begin(), P[nx].end(), y - 4 * R) - P[nx].begin();
        for (int j = id; j < P[nx].size(); j++) {
          // cout << nx << " "<< P[nx][j] << endl;
          if (P[nx][j] > y + 4 * R)
            break;
          if (check(nx, P[nx][j], x, y))
            res++;
        }
      }
    }
    cout << res << endl;
  }
}
Problem p00612 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef long long ll;

int main() {
  ll n;
  while (cin >> n, n) {
    if (n == 3) {
      cout << 42 << endl;
      continue;
    }
    ll hoge = (int)(sqrt((double)n / 2) - 1);
    ll num = 2 * n - 4 * hoge;
    cout << num << endl;
    for (ll i = 1; 2 * i * i <= n; ++i) {
      num += 4 * ceil((double)n / i / 2) - 2 * hoge;
      cout << i << " " << num << endl;
    }
    cout << num * 2 + 8 * n << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef long long ll;

int main() {
  ll n;
  while (scanf("%lld", &n), n) {
    ll corner = 1;
    while (corner * corner < n / 2)
      ++corner;
    ll side = n / 2;
    for (ll i = 1; i * i < n / 2; ++i)
      side += (n / 2 + i - 1) / i;
    side -= corner * corner;
    // cout << corner << " " << side << endl;
    ll ans = side * 8 + corner * corner * 4;
    ans *= 2;
    ans += n * 2 * 4;
    printf("%lld\n", ans);
  }
}
Problem p00613 (C++)

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

int main() {
  while (1) {
    int K, c[10], sum = 0;
    cin >> K;
    if (0 == K)
      break;
    for (int i = 0; i < K * (K - 1) / 2; i++) {
      cin >> c[i];
      sum += c[i];
    }
    sum = sum / (K - 1);
    cout << sum << endl;
  }
  return 0;
}


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

int main() {
  while (1) {
    int K, c[10000], sum = 0;
    cin >> K;
    if (0 == K)
      break;
    for (int i = 0; i < K * (K - 1) / 2; i++) {
      cin >> c[i];
      sum += c[i];
    }
    sum = sum / (K - 1);
    cout << sum << endl;
  }
  return 0;
}
Problem p00613 (C++)

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

int main() {
  int k, l;
  int p = 0;
  int i = 0;
  int a[101];
  while (1) {
    cin >> k;
    l = k * (k - 1) / 2;
    if (k == 0) {
      break;
    }
    for (i = 0; i < l; i++) {
      cin >> a[i];
      p += a[i];
    }
    cout << p / (k - 1) << endl;
    p = 0;
  }
  return 0;
}


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

int main() {
  int k, l;
  int p = 0;
  int i = 0;
  int a[10000];
  while (1) {
    cin >> k;
    l = k * (k - 1) / 2;
    if (k == 0) {
      break;
    }
    for (i = 0; i < l; i++) {
      cin >> a[i];
      p += a[i];
    }
    cout << p / (k - 1) << endl;
    p = 0;
  }
  return 0;
}
Problem p00614 (C++)

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

using namespace std;

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

int main() {

  //  cout << 5 * 100000 + 100000 + 5 * 10000 + 10000 + 5 * 1000 + 1000 << endl;

  while (1) {
    int P, N[7] = {};
    scanf("%d", &P);
    if (P == 0)
      break;
    REP(i, 1, 7) scanf("%d", &N[i]);

    int ans = 1000000000;

    REP(d, P, 666001) {
      int D = d;

      int U = 0;
      int k, m;

      m = D / 500;
      k = min(m, N[6]);
      D %= 500;
      D += (m - k) * 500;
      U += k;

      m = D / 100;
      k = min(m, N[5]);
      D %= 100;
      D += (m - k) * 100;
      U += k;

      m = D / 50;
      k = min(m, N[4]);
      D %= 50;
      D += (m - k) * 50;
      U += k;

      m = D / 10;
      k = min(m, N[3]);
      D %= 10;
      D += (m - k) * 10;
      U += k;

      m = D / 5;
      k = min(m, N[2]);
      D %= 5;
      D += (m - k) * 5;
      U += k;

      m = D;
      k = min(D, N[1]);
      D += (m - k);
      U += k;

      int T = d - P;
      int S = 0;
      S += T / 500;
      T %= 500;
      S += T / 100;
      T %= 100;
      S += T / 50;
      T %= 50;
      S += T / 10;
      T %= 10;
      S += T / 5;
      T %= 5;
      S += T;

      ans = min(ans, U + S);
    }
    printf("%d\n", ans);
  }

  return 0;
}

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

using namespace std;

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

int main() {

  //  cout << 5 * 100000 + 100000 + 5 * 10000 + 10000 + 5 * 1000 + 1000 << endl;

  while (1) {
    int P, N[7] = {};
    scanf("%d", &P);
    if (P == 0)
      break;
    REP(i, 1, 7) scanf("%d", &N[i]);

    int ans = 1000000000;

    REP(d, P, P + 100000) {
      int D = d;

      int U = 0;
      int k, m;

      m = D / 500;
      k = min(m, N[6]);
      D %= 500;
      D += (m - k) * 500;
      U += k;

      m = D / 100;
      k = min(m, N[5]);
      D %= 100;
      D += (m - k) * 100;
      U += k;

      m = D / 50;
      k = min(m, N[4]);
      D %= 50;
      D += (m - k) * 50;
      U += k;

      m = D / 10;
      k = min(m, N[3]);
      D %= 10;
      D += (m - k) * 10;
      U += k;

      m = D / 5;
      k = min(m, N[2]);
      D %= 5;
      D += (m - k) * 5;
      U += k;

      m = D;
      k = min(D, N[1]);
      D += (m - k);
      U += k;

      int T = d - P;
      int S = 0;
      S += T / 500;
      T %= 500;
      S += T / 100;
      T %= 100;
      S += T / 50;
      T %= 50;
      S += T / 10;
      T %= 10;
      S += T / 5;
      T %= 5;
      S += T;

      ans = min(ans, U + S);
    }
    printf("%d\n", ans);
  }

  return 0;
}
Problem p00614 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define FOREACH(i, c)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

const int INF = 1000000000;
int value[] = {500, 100, 50, 10, 5, 1};

int opt(int p, int num[]) {
  int ret = 0;
  REP(i, 6) {
    int use = min(num[i], p / value[i]);
    p -= value[i] * use;
    ret += use;
  }
  return p ? INF : ret;
}

int main() {
  int p;
  while (scanf("%d", &p), p) {
    int num[6], sum = 0;
    REP(i, 6) {
      scanf("%d", &num[5 - i]);
      sum += num[5 - i] * value[5 - i];
    }
    int big[] = {INF, INF, INF, INF, INF, INF};
    int ans = INF;
    FOREQ(i, p, sum) { ans = min(ans, opt(i, num) + opt(i - p, big)); }
    printf("%d\n", ans);
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define FOREACH(i, c)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

const int INF = 1000000000;
int value[] = {500, 100, 50, 10, 5, 1};

int opt(int p, int num[]) {
  int ret = 0;
  REP(i, 6) {
    int use = min(num[i], p / value[i]);
    p -= value[i] * use;
    ret += use;
  }
  return p ? INF : ret;
}

int main() {
  int p;
  while (scanf("%d", &p), p) {
    int num[6], sum = 0;
    REP(i, 6) {
      scanf("%d", &num[5 - i]);
      sum += num[5 - i] * value[5 - i];
    }
    int big[] = {INF, INF, INF, INF, INF, INF};
    int ans = INF;
    FOREQ(i, p, min(sum, p + 10000)) {
      ans = min(ans, opt(i, num) + opt(i - p, big));
    }
    printf("%d\n", ans);
  }
}
Problem p00614 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#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 FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int MAXN = 666 * 1000 + 1;

int dp2[MAXN];
int p[6] = {1, 5, 10, 50, 100, 500};
int count_c(int Y, int c[]) {
  int res = 0;
  for (int i = 5; i >= 0; i--) {
    REP(j, c[i]) {
      if (Y >= p[i]) {
        Y -= p[i];
        res++;
      }
    }
  }
  if (Y == 0)
    return res;
  else
    return INF;
}
int main() {
  int Y;
  fill(dp2, dp2 + MAXN, INF);
  dp2[0] = 0;
  REP(i, 6) {
    FOR(j, p[i], MAXN) { dp2[j] = min(dp2[j], dp2[j - p[i]] + 1); }
  }
  while (cin >> Y, Y) {
    int c[6];
    REP(i, 6) cin >> c[i];
    int MY = 0;
    REP(i, 6) MY += c[i] * p[i];
    int ans = INF;
    for (int i = MY; i >= Y; i--) {
      ans = min(ans, count_c(i, c) + dp2[i - Y]);
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#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 FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int MAXN = 666 * 1000 + 1;

int dp2[MAXN];
int p[6] = {1, 5, 10, 50, 100, 500};
int count_c(int Y, int c[]) {
  int res = 0;
  for (int i = 5; i >= 0; i--) {
    int t = min(c[i], Y / p[i]);
    Y -= t * p[i];
    res += t;
  }
  if (Y == 0)
    return res;
  else
    return INF;
}
int main() {
  int Y;
  fill(dp2, dp2 + MAXN, INF);
  dp2[0] = 0;
  REP(i, 6) {
    FOR(j, p[i], MAXN) { dp2[j] = min(dp2[j], dp2[j - p[i]] + 1); }
  }
  while (cin >> Y, Y) {
    int c[6];
    REP(i, 6) cin >> c[i];
    int MY = 0;
    REP(i, 6) MY += c[i] * p[i];
    int ans = INF;
    for (int i = MY; i >= Y; i--) {
      ans = min(ans, count_c(i, c) + dp2[i - Y]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00615 (C++)

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

// #define int long long
#define DEBUG 0
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \
  if (DEBUG) {                                                                 \
    cerr << #o << " " << o << endl;                                            \
  }
#define dumpc(o)                                                               \
  if (DEBUG) {                                                                 \
    cerr << #o;                                                                \
    for (auto &e : (o))                                                        \
      cerr << " " << e;                                                        \
    cerr << endl;                                                              \
  }
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
static const int MOD = 1e9 + 7;

signed main() {
  int a[10010] = {};
  for (int n, m; cin >> n >> m && (n || m);) {
    rep(i, 0, n + m) cin >> a[i];
    a[n + m] = 0;
    sort(a, a + n + m + 1);
    int M = 0;
    rep(i, 1, n + m + 1) { M = max(M, a[i] - a[i - 1]); }
    cout << M << endl;
  }
  return 0;
}

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

// #define int long long
#define DEBUG 0
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \
  if (DEBUG) {                                                                 \
    cerr << #o << " " << o << endl;                                            \
  }
#define dumpc(o)                                                               \
  if (DEBUG) {                                                                 \
    cerr << #o;                                                                \
    for (auto &e : (o))                                                        \
      cerr << " " << e;                                                        \
    cerr << endl;                                                              \
  }
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
static const int MOD = 1e9 + 7;

signed main() {
  int a[20010] = {};
  for (int n, m; cin >> n >> m && (n || m);) {
    rep(i, 0, n + m) cin >> a[i];
    a[n + m] = 0;
    sort(a, a + n + m + 1);
    int M = 0;
    rep(i, 1, n + m + 1) { M = max(M, a[i] - a[i - 1]); }
    cout << M << endl;
  }
  return 0;
}
Problem p00615 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <iostream>
#define N 10000000
int tl[N];

using namespace std;

void sort(int, int);
int syori(int, int);

int main() {
  int n, m, i, ans;
  while (1) {
    for (i = 0; i < N; i++) {
      tl[i] = 0;
    }

    cin >> n;
    cin >> m;

    if (m == 0 && n == 0)
      break;

    for (i = 1; i <= n; i++) {
      cin >> tl[i];
    }

    for (i = n + 1; i <= (m + n); i++) {
      cin >> tl[i];
    }

    sort(m, n);

    ans = syori(n, m);

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

void sort(int m, int n) {
  int i, j, max, hi;

  for (i = 0; i <= (m + n); i++) {
    max = i;
    for (j = i; j <= (m + n); j++) {
      if (tl[j] < tl[max])
        max = j;
    }
    hi = tl[max];
    tl[max] = tl[i];
    tl[i] = hi;
  }

  //  for(i=0;i<=(m+n);i++){
  // cout << tl[i] << endl;;

  //}
}

int syori(int n, int m) {
  int i, max = 0;
  for (i = 0; i < (m + n); i++) {
    if (tl[i + 1] - tl[i] >= max)
      max = tl[i + 1] - tl[i];
  }

  return max;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define N 1000000
int tl[N];

using namespace std;

void sort(int, int);
int syori(int, int);

int main() {
  int n, m, i, ans;
  while (1) {
    for (i = 0; i < N; i++) {
      tl[i] = 0;
    }

    cin >> n;
    cin >> m;

    if (m == 0 && n == 0)
      break;

    for (i = 1; i <= n; i++) {
      cin >> tl[i];
    }

    for (i = n + 1; i <= (m + n); i++) {
      cin >> tl[i];
    }

    sort(m, n);

    ans = syori(n, m);

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

void sort(int m, int n) {
  int i, j, max, hi;

  for (i = 0; i <= (m + n); i++) {
    max = i;
    for (j = i; j <= (m + n); j++) {
      if (tl[j] < tl[max])
        max = j;
    }
    hi = tl[max];
    tl[max] = tl[i];
    tl[i] = hi;
  }

  //  for(i=0;i<=(m+n);i++){
  // cout << tl[i] << endl;;

  //}
}

int syori(int n, int m) {
  int i, max = 0;
  for (i = 0; i < (m + n); i++) {
    if (tl[i + 1] - tl[i] >= max)
      max = tl[i + 1] - tl[i];
  }

  return max;
}
Problem p00615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
#define FOR(i, b, n) for (int i = (b); i < (n); ++i)
#define rep(i, n) FOR(i, 0, n)
int main() {
  int a, b, data[10000];
  while (cin >> a >> b, a || b) {
    rep(i, a + b) cin >> data[i];
    sort(data, data + a + b);
    int ans = data[0];
    FOR(i, 1, a + b) ans = max(ans, data[i] - data[i - 1]);
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
#define FOR(i, b, n) for (int i = (b); i < (n); ++i)
#define rep(i, n) FOR(i, 0, n)
int main() {
  int a, b, data[20000];
  while (cin >> a >> b, a || b) {
    rep(i, a + b) cin >> data[i];
    sort(data, data + a + b);
    int ans = data[0];
    FOR(i, 1, a + b) ans = max(ans, data[i] - data[i - 1]);
    cout << ans << endl;
  }
}
Problem p00615 (C++)

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

int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}

void bsort(int a[], int lim) {
  int tmp;

  for (int i = 0; i <= lim - 1; i++)
    for (int j = lim; i < j; j--) {
      if (a[j - 1] > a[j]) {
        tmp = a[j];
        a[j] = a[j - 1];
        a[j - 1] = tmp;
      }
    }
}

int main() {
  int n, m, i;
  int out;
  int t[1000];

  while (std::cin >> n >> m, (n || m)) {
    std::memset(t, 0, sizeof(t));
    out = 0;

    for (i = 1; i <= n + m; i++) {
      std::cin >> t[i];
    }

    bsort(t, n + m);

    for (i = 0; i <= m + n; i++) {
      t[i] = t[i + 1] - t[i];
    }
    for (i = 0; i <= m + n - 1; i++) {
      out = max(out, t[i]);
    }

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

  return 0;
}

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

int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}

void bsort(int a[], int lim) {
  int tmp;

  for (int i = 0; i <= lim - 1; i++)
    for (int j = lim; i < j; j--) {
      if (a[j - 1] > a[j]) {
        tmp = a[j];
        a[j] = a[j - 1];
        a[j - 1] = tmp;
      }
    }
}

int main() {
  int n, m, i;
  int out;
  int t[20002];

  while (std::cin >> n >> m, (n || m)) {
    std::memset(t, 0, sizeof(t));
    out = 0;

    for (i = 1; i <= n + m; i++) {
      std::cin >> t[i];
    }

    bsort(t, n + m);

    for (i = 0; i <= m + n; i++) {
      t[i] = t[i + 1] - t[i];
    }
    for (i = 0; i <= m + n - 1; i++) {
      out = max(out, t[i]);
    }

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

  return 0;
}
Problem p00616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
bool t[3][200][200];
int n, h, x, y;
string s;
int main() {
  while (cin >> n >> h && n + h) {
    int cnt, ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++) {
          t[i][j][k] = 0;
        }
    }
    for (int i = 0; i < h; i++) {
      cin >> s >> x >> y;
      x--, y--;
      if (s == "xy") {
        t[0][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[1][x][i])
            cnt++;
          if (t[2][y][i])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
      if (s == "xz") {
        t[1][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[0][x][i])
            cnt++;
          if (t[2][i][y])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
      if (s == "yz") {
        t[2][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[0][i][x])
            cnt++;
          if (t[1][i][y])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
    }
    cout << n * (n * n - h) + ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
bool t[3][500][500];
int n, h, x, y;
string s;
int main() {
  while (cin >> n >> h && n + h) {
    int cnt, ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++) {
          t[i][j][k] = 0;
        }
    }
    for (int i = 0; i < h; i++) {
      cin >> s >> x >> y;
      x--, y--;
      if (s == "xy") {
        t[0][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[1][x][i])
            cnt++;
          if (t[2][y][i])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
      if (s == "xz") {
        t[1][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[0][x][i])
            cnt++;
          if (t[2][i][y])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
      if (s == "yz") {
        t[2][x][y] = 1;
        for (int i = 0; i < n; i++) {
          cnt = 0;
          if (t[0][i][x])
            cnt++;
          if (t[1][i][y])
            cnt++;
          if (cnt == 2)
            cnt--;
          ans += cnt;
        }
      }
    }
    cout << n * (n * n - h) + ans << endl;
  }
  return 0;
}
Problem p00618 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct NODE {
  int a, b, c;
  NODE(int x, int y, int z) { a = x, b = y, c = z; }
};

#define INF (1 << 21)
int n, U, unit[20];
char done[1 << 20];
int main() {
  while (cin >> n >> U, n) {

    int graph[21] = {0};
    memset(done, 0, sizeof(done));
    for (int i = 0; i < n; i++) {
      int k;
      cin >> unit[i] >> k;
      for (int j = 0; j < k; j++) {
        int a;
        cin >> a;
        graph[i] |= (1 << a);
      }
    }
    queue<NODE> Q;
    Q.push(NODE(0, 0, 0));
    done[0] = 1;
    while (Q.size()) {
      NODE q = Q.front();
      Q.pop();
      if (q.b >= U) {
        cout << q.c << endl;
        break;
      }
      for (int i = 0; i < n; i++) {
        if (!(q.a >> i & 1) && (q.a & graph[i]) == graph[i])
          if (!done[q.a | (1 << i)])
            Q.push(NODE(q.a | (1 << i), q.b + unit[i], q.c + 1));
      }
    }
  }
}

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

struct NODE {
  int a, b, c;
  NODE(int x, int y, int z) { a = x, b = y, c = z; }
};

#define INF (1 << 21)
int n, U, unit[20];
char done[1 << 20];
int main() {
  while (cin >> n >> U, n) {

    int graph[21] = {0};
    memset(done, 0, sizeof(done));
    for (int i = 0; i < n; i++) {
      int k;
      cin >> unit[i] >> k;
      for (int j = 0; j < k; j++) {
        int a;
        cin >> a;
        graph[i] |= (1 << a);
      }
    }
    queue<NODE> Q;
    Q.push(NODE(0, 0, 0));
    done[0] = 1;
    while (Q.size()) {
      NODE q = Q.front();
      Q.pop();
      if (q.b >= U) {
        cout << q.c << endl;
        break;
      }
      for (int i = 0; i < n; i++) {
        if (!(q.a >> i & 1) && (q.a & graph[i]) == graph[i])
          if (!done[q.a | (1 << i)])
            Q.push(NODE(q.a | (1 << i), q.b + unit[i], q.c + 1)),
                done[q.a | (1 << i)] = true;
      }
    }
  }
}
Problem p00618 (C++)

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

struct Data {
  int used;
  int credit;
};

int n, U, c, k, r;
int credit[20], required[20];

int bitcount(int n) { return n ? (n & 1) + bitcount(n >> 1) : 0; }
int BFS() {
  bool visited[1 << 20] = {0};
  queue<Data> q;
  Data now = {0, 0};
  q.push(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    if (visited[now.used])
      continue;
    visited[now.used] = true;
    if (now.credit >= U)
      return bitcount(now.used);
    for (int i = 0; i < n; i++) {
      if (now.used & (1 << i) || (now.used & required[i]) != required[i])
        continue;
      Data next = now;
      next.used ^= (1 << i);
      next.credit += credit[i];
      q.push(next);
    }
  }
  return -1;
}

int main() {
  while (cin >> n >> U, n | U) {
    for (int i = 0; i < n; i++) {
      cin >> c >> k;
      credit[i] = c;
      required[i] = 0;
      for (int j = 0; j < k; j++) {
        cin >> r;
        required[i] ^= (1 << r);
      }
    }
    cout << BFS() << endl;
  }
}

Fixed submission:

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

struct Data {
  int used;
  int credit;
};

int n, U, c, k, r;
int credit[20], required[20];

int bitcount(int n) { return n ? (n & 1) + bitcount(n >> 1) : 0; }
int BFS() {
  bool visited[1 << 20] = {0};
  queue<Data> q;
  Data now = {0, 0};
  q.push(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    if (visited[now.used])
      continue;
    visited[now.used] = true;
    if (now.credit >= U)
      return bitcount(now.used);
    for (int i = 0; i < n; i++) {
      if (now.used & (1 << i) || (now.used & required[i]) != required[i])
        continue;
      Data next = now;
      next.used ^= (1 << i);
      next.credit += credit[i];
      q.push(next);
    }
  }
  return -1;
}

int main() {
  while (cin >> n >> U, n | U) {
    for (int i = 0; i < n; i++) {
      cin >> c >> k;
      credit[i] = c;
      required[i] = 0;
      for (int j = 0; j < k; j++) {
        cin >> r;
        required[i] ^= (1 << r);
      }
    }
    cout << BFS() << endl;
  }
}
Problem p00620 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define pb push_back
using namespace std;
typedef pair<int, int> P;
int n;
vector<P> rt;
// map<vector<vector<int> >,bool> mp;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool inf(int y, int x) {
  if (y >= 0 && y < n && x >= 0 && x < n)
    return true;
  return false;
}
bool cal(vector<vector<int>> &, int);
bool f(vector<vector<int>> v, int y, int x, int left, int now) {
  if (!left) {
    return cal(v, now + 1);
  }
  for (int i = 0; i < 4; i++) {
    int ny = dy[i] + y, nx = dx[i] + x;
    if (inf(ny, nx) && v[ny][nx] > 0 && left - v[ny][nx] >= 0) {
      int temp = v[ny][nx];
      v[ny][nx] = 0;
      if (f(v, ny, nx, left - temp, now)) {
        return true;
      }
      v[ny][nx] = temp;
    }
  }
  return false;
}

bool cal(vector<vector<int>> &v, int now) {
  if (now == rt.size()) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (v[i][j] != 0)
          return false;
    return true;
  }
  int y = rt[now].first;
  int x = rt[now].second;
  int temp = v[y][x];
  v[y][x] = 0;
  if (f(v, y, x, -temp, now))
    return true;
  v[y][x] = temp;
  return false;
}
int main() {
  while (cin >> n, n) {
    vector<vector<int>> v(n, vector<int>(n));
    rt.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> v[i][j];
        if (v[i][j] < 0)
          rt.pb(P(i, j));
      }
    if (cal(v, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define pb push_back
using namespace std;
typedef pair<int, int> P;
int n;
vector<P> rt;
// map<vector<vector<int> >,bool> mp;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool inf(int y, int x) {
  if (y >= 0 && y < n && x >= 0 && x < n)
    return true;
  return false;
}
bool cal(vector<vector<int>> &, int);
bool f(vector<vector<int>> &v, int y, int x, int left, int now) {
  if (!left) {
    return cal(v, now + 1);
  }
  for (int i = 0; i < 4; i++) {
    int ny = dy[i] + y, nx = dx[i] + x;
    if (inf(ny, nx) && v[ny][nx] > 0 && left - v[ny][nx] >= 0) {
      int temp = v[ny][nx];
      v[ny][nx] = 0;
      if (f(v, ny, nx, left - temp, now)) {
        return true;
      }
      v[ny][nx] = temp;
    }
  }
  return false;
}

bool cal(vector<vector<int>> &v, int now) {
  if (now == rt.size()) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (v[i][j] != 0)
          return false;
    return true;
  }
  int y = rt[now].first;
  int x = rt[now].second;
  int temp = v[y][x];
  v[y][x] = 0;
  if (f(v, y, x, -temp, now))
    return true;
  v[y][x] = temp;
  return false;
}
int main() {
  while (cin >> n, n) {
    vector<vector<int>> v(n, vector<int>(n));
    rt.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> v[i][j];
        if (v[i][j] < 0)
          rt.pb(P(i, j));
      }
    if (cal(v, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p00620 (C++)

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

using namespace std;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int field[8][8] = {{0}};
bool used[8][8] = {{false}};
vector<int> startX, startY;

bool dfs(int n, int x, int y, int r, int k) {
  if (r == 0) {
    if (k == startX.size()) {
      return true;
    }
    x = startX[k];
    y = startY[k];
    ++k;
  } else if (used[y][x] || field[y][x] < 0) {
    return false;
  }
  r -= field[y][x];
  if (r < 0) {
    return false;
  }
  used[y][x] = true;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (x >= 0 && x < n && y >= 0 && y < n) {
      if (dfs(n, nx, ny, r, k)) {
        return true;
      }
    }
  }
  used[y][x] = false;
  return false;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> field[i][j];
        sum += field[i][j];
        if (field[i][j] < 0) {
          startX.push_back(j);
          startY.push_back(i);
        }
        used[i][j] = false;
      }
    }
    cout << (sum == 0 && dfs(n, 0, 0, 0, 0) ? "YES" : "NO") << endl;
    startX.clear();
    startY.clear();
  }
  return 0;
}

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

using namespace std;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int field[8][8] = {{0}};
bool used[8][8] = {{false}};
vector<int> startX, startY;

bool dfs(int n, int x, int y, int r, int k) {
  if (r == 0) {
    if (k == startX.size()) {
      return true;
    }
    x = startX[k];
    y = startY[k];
    ++k;
  } else if (used[y][x] || field[y][x] < 0) {
    return false;
  }
  r -= field[y][x];
  if (r < 0) {
    return false;
  }
  used[y][x] = true;
  if (r == 0) {
    if (dfs(n, 0, 0, 0, k)) {
      return true;
    }
  } else {
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (x >= 0 && x < n && y >= 0 && y < n) {
        if (dfs(n, nx, ny, r, k)) {
          return true;
        }
      }
    }
  }
  used[y][x] = false;
  return false;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> field[i][j];
        sum += field[i][j];
        if (field[i][j] < 0) {
          startX.push_back(j);
          startY.push_back(i);
        }
        used[i][j] = false;
      }
    }
    cout << (sum == 0 && dfs(n, 0, 0, 0, 0) ? "YES" : "NO") << endl;
    startX.clear();
    startY.clear();
  }
  return 0;
}
Problem p00621 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int d[110], w, q;

int check(int wide) {
  int cnt = 0;
  rep(i, 0, w + 1) {
    if (d[i]) {
      cnt = 0;
    } else {
      cnt++;
    }
    if (cnt = wide)
      return i + 1 - cnt;
  }
  return -1;
}

signed main() {
  while (1) {
    cin >> w >> q;
    if (w + q == 0)
      break;
    char c;
    int id, wide;
    rep(i, 0, w) d[i] = 0;
    d[w] = 1;

    map<int, pii> mp;
    rep(i, 0, q) {
      cin >> c;
      if (c == 's') {
        cin >> id >> wide;
        mp[id].first = wide;
        int ind = check(wide);
        if (ind == -1)
          o("impossible");
        else {
          o(ind);
          mp[id].second = ind;
          rep(j, 0, wide) { d[ind + j] = 1; }
        }
      } else {
        cin >> id;
        rep(j, 0, mp[id].first) { d[mp[id].second + j] = 0; }
      }
    }
    o("END");
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int d[110], w, q;

int check(int wide) {
  int cnt = 0;
  rep(i, 0, w + 1) {
    if (d[i]) {
      cnt = 0;
    } else {
      cnt++;
    }
    if (cnt == wide)
      return i + 1 - cnt;
  }
  return -1;
}

signed main() {
  while (1) {
    cin >> w >> q;
    if (w + q == 0)
      break;
    char c;
    int id, wide;
    rep(i, 0, w) d[i] = 0;
    d[w] = 1;

    map<int, pii> mp;
    rep(i, 0, q) {
      cin >> c;
      if (c == 's') {
        cin >> id >> wide;
        mp[id].first = wide;
        int ind = check(wide);
        if (ind == -1)
          o("impossible");
        else {
          o(ind);
          mp[id].second = ind;
          rep(j, 0, wide) { d[ind + j] = 1; }
        }
      } else {
        cin >> id;
        rep(j, 0, mp[id].first) { d[mp[id].second + j] = 0; }
      }
    }
    o("END");
  }
}
Problem p00622 (C++)

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

string t, l, ad, ar, d, r;
int ct, cl;
char cc;

void push_right() {
  r += cc;
  cc = l[cl++];
}

void push_down() {
  d += cc;
  cc = t[ct++];
}

bool rec(int s) {
  char tmpc;
  if (s == t.length() + l.length() - 1) {
    int f;
    if (d.length() != t.length()) {
      d += cc;
      f = 0;
    } else {
      r += cc;
      f = 1;
    }
    if (d == ad) {
      ar = r;
      return true;
    }
    if (f == 0)
      d = d.substr(0, d.length() - 1);
    else
      r = r.substr(0, r.length() - 1);
    return false;
  }
  if (cl < l.length()) {
    tmpc = cc;
    push_right();
    if (rec(s + 1))
      return true;
    r = r.substr(0, r.length() - 1);
    cl--;
    cc = tmpc;
  }
  if (ct < t.length()) {
    tmpc = cc;
    push_down();
    if (rec(s + 1))
      return true;
    d = d.substr(0, d.length() - 1);
    ct--;
    cc = tmpc;
  }
  return false;
}

string solve() {
  cc = l[0];
  r = d = "";
  ct = 0;
  cl = 1;
  if (rec(0)) {
    return ar;
  } else {
    return "NA";
  }
}

int main() {
  while (1) {
    cin >> t;
    if (t == "-")
      break;
    cin >> l;
    cin >> ad;
    cout << solve() << endl;
  }
  return 0;
}

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

string t, l, ad, ar, d, r;
int ct, cl;
char cc;

void push_right() {
  r += cc;
  cc = l[cl++];
}

void push_down() {
  d += cc;
  cc = t[ct++];
}

bool rec(int s) {
  char tmpc;
  if (s == t.length() + l.length() - 1) {
    int f;
    if (d.length() != t.length()) {
      d += cc;
      f = 0;
    } else {
      r += cc;
      f = 1;
    }
    if (d == ad) {
      ar = r;
      return true;
    }
    if (f == 0)
      d = d.substr(0, d.length() - 1);
    else
      r = r.substr(0, r.length() - 1);
    return false;
  }
  if (d.length() != 0 && d[d.length() - 1] != ad[d.length() - 1])
    return false;
  if (cl < l.length()) {
    tmpc = cc;
    push_right();
    if (rec(s + 1))
      return true;
    r = r.substr(0, r.length() - 1);
    cl--;
    cc = tmpc;
  }
  if (ct < t.length()) {
    tmpc = cc;
    push_down();
    if (rec(s + 1))
      return true;
    d = d.substr(0, d.length() - 1);
    ct--;
    cc = tmpc;
  }
  return false;
}

string solve() {
  cc = l[0];
  r = d = "";
  ct = 0;
  cl = 1;
  if (rec(0)) {
    return ar;
  } else {
    return "NA";
  }
}

int main() {
  while (1) {
    cin >> t;
    if (t == "-")
      break;
    cin >> l;
    cin >> ad;
    cout << solve() << endl;
  }
  return 0;
}