alexjercan/bugnet huggingface dataset:
Problem p00258 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string.h>
#include <utility>
using namespace std;
typedef long long ll;
int st[31][1 << 16], dp[31][1 << 16];
int count(int num) {
  int res = 0;
  for (int i = 0; i < 16; i++)
    if ((num >> i) & 1)
      res++;
  return res;
}
int main() {
  int n, c;
  while (cin >> n >> c && n) {
    memset(st, 0, sizeof(st));
    memset(dp, -1, sizeof(dp));
    int light[31] = {}, push[31] = {}, tmp;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> tmp;
        light[i] *= 2;
        light[i] += tmp;
      }
      light[i] = light[i] & ((1 << 16) - 1);
      // printf("light[%d]:%d\\n",i,light[i]);
    }
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> tmp;
        push[i] *= 2;
        push[i] += tmp;
      }
      for (int j = 0; j < (1 << 16); j++) {
        st[i][j] = count(push[i] & j);
      }
      push[i] = push[i] & ((1 << 16) - 1);
      // printf("push[%d]:%d\\n",i,push[i]);
    }
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] < 0)
          continue;
        int nx = j | light[i];
        // dp[i+1][nx]=max(dp[i+1][nx],dp[i][j]);
        for (int k = 0; k < c; k++) {
          int tx = (nx & ~push[k]);
          dp[i + 1][tx] = max(dp[i + 1][tx], dp[i][j] + st[k][nx]);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++)
      ans = max(ans, dp[n][i]);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;
typedef long long ll;
int st[31][1 << 16], dp[31][1 << 16];
int count(int num) {
  int res = 0;
  for (int i = 0; i < 16; i++)
    if ((num >> i) & 1)
      res++;
  return res;
}
int main() {
  int n, c;
  while (cin >> n >> c && n) {
    memset(st, 0, sizeof(st));
    memset(dp, -1, sizeof(dp));
    int light[31], push[31], tmp;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 16; j++) {
        cin >> tmp;
        light[i] << 1;
        light[i] += tmp;
      }
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> tmp;
        push[i] *= 2;
        push[i] += tmp;
      }
      for (int j = 0; j < (1 << 16); j++) {
        st[i][j] = count(push[i] & j);
        dp[0][j] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] < 0)
          continue;
        int nx = j | light[i];
        dp[i + 1][nx] = max(dp[i + 1][nx], dp[i][j]);
        for (int k = 0; k < c; k++) {
          int tx = nx & ~push[j];
          dp[i + 1][tx] = max(dp[i + 1][tx], dp[i][j] + st[k][nx]);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++)
      ans = max(ans, dp[n][i]);
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00258 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, m, x;
int a[50], b[50];
int dp[50][(1 << 16)];

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &x);
        a[i] = a[i] * 2 + x;
      }
    }
    for (int i = 0; i < m; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &x);
        b[i] = b[i] * 2 + x;
      }
    }

    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] == -1)
          continue;
        for (int k = 0; k < m; k++) {
          int S = (j | a[i] | b[k]) - b[k];
          int cost = __builtin_popcount((j | a[i]) & b[k]);
          dp[i + 1][S] = max(dp[i + 1][S], dp[i][j] + cost);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++)
      ans = max(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, x;
int a[30], b[30];
int dp[30][(1 << 16)];

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &x);
        a[i] = a[i] * 2 + x;
      }
    }
    for (int i = 0; i < m; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &x);
        b[i] = b[i] * 2 + x;
      }
    }

    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] == -1)
          continue;
        for (int k = 0; k < m; k++) {
          int S = (j | a[i] | b[k]) - b[k];
          int cost = __builtin_popcount((j | a[i]) & b[k]);
          dp[i + 1][S] = max(dp[i + 1][S], dp[i][j] + cost);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++)
      ans = max(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int a[33], b[33];
int n, c;
int dp[33][66666];
int pcnt[66666];

int solve() {
  rep(i, 33) rep(rem, 1 << 16) dp[i][rem] = -1e8;
  dp[0][0] = 0;
  rep(i, n) {
    rep(rem, 1 << 16) {
      int on = rem | a[i];
      rep(j, c) {
        int score = pcnt[on & b[j]];
        int nx = on & ~b[j];
        dp[i + 1][nx] = max(dp[i + 1][nx], dp[i][rem] + score);
      }
    }
  }
  int ans = 0;
  rep(i, 1 << 16) ans = max(ans, dp[n][i]);
  return ans;
}

int main() {
  pcnt[0] = 0;
  for (int i = 1; i < 66666; i++) {
    pcnt[i] = pcnt[i / 2] + i % 2;
  }
  while (cin >> n >> c && n) {
    rep(i, n) {
      a[i] = 0;
      rep(j, 16) {
        int x;
        cin >> x;
        a[i] |= (x & 1) << j;
      }
    }
    rep(i, c) {
      b[i] = 0;
      rep(j, 16) {
        int x;
        cin >> x;
        b[i] |= (x & 1) << j;
      }
    }
    cout << solve() << endl;
  }
}

Error: TLE

Fixed submission:

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

int a[33], b[33];
int n, c;
int dp[(1 << 16) + 10], dp_[(1 << 16) + 10];

int popcount(int n) { return n == 0 ? 0 : n % 2 + popcount(n / 2); }

int solve() {
  rep(rem, 1 << 16) dp[rem] = -1e8;
  rep(rem, 1 << 16) dp_[rem] = -1e8;
  dp[0] = 0;
  rep(i, n) {
    rep(rem, 1 << 16) {
      int on = rem | a[i];
      rep(j, c) {
        int score = popcount(on & b[j]);
        int nx = on & ~b[j];
        dp_[nx] = max(dp_[nx], dp[rem] + score);
      }
    }
    rep(rem, 1 << 16) dp[rem] = dp_[rem];
  }
  int ans = 0;
  rep(i, 1 << 16) ans = max(ans, dp[i]);
  return ans;
}

int main() {
  while (cin >> n >> c && n) {
    rep(i, n) {
      a[i] = 0;
      rep(j, 16) {
        int x;
        cin >> x;
        a[i] |= (x & 1) << j;
      }
    }
    rep(i, c) {
      b[i] = 0;
      rep(j, 16) {
        int x;
        cin >> x;
        b[i] |= (x & 1) << j;
      }
    }
    cout << solve() << endl;
  }
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FFOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define RREP(i, b) FFOR(i, 1, b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(), c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut, ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut, pr> ppr;
typedef priority_queue<pr, Vpr, greater<pr>> PQ;
const int SIZE = 5 + 3 * 1e+4;
const ut INF = 1 << 30;
const ld eps = 1e-6;
const LL mod = 7 + 1e+9;

int memo[30][2 << 17];
int A[32], B[32], N, C;

int rec(int n, int p) {
  if (n >= N)
    return 0;

  if (memo[n][p] != -1)
    return memo[n][p];

  int res = 0;
  int t = p | A[n];
  REP(i, C) {
    res = max(res, rec(n + 1, t & ~B[i]) + __builtin_popcount(t & B[i]));
  }
  return memo[n][p] = res;
}

int main() {
  int a, b;
  while (cin >> N >> C && (N || C)) {
    memset(memo, -1, sizeof(memo));
    memset(A, 0, sizeof(A));
    memset(B, 0, sizeof(B));
    REP(i, N) {
      REP(j, 16) {
        cin >> a;
        A[i] |= (a << j);
      }
    }
    REP(i, C) {
      REP(j, 16) {
        cin >> b;
        B[i] |= (b << j);
      }
    }
    B[C] = 0;
    C++;
    cout << rec(0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FFOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define RREP(i, b) FFOR(i, 1, b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(), c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut, ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut, pr> ppr;
typedef priority_queue<pr, Vpr, greater<pr>> PQ;
const int SIZE = 5 + 3 * 1e+4;
const ut INF = 1 << 30;
const ld eps = 1e-6;
const LL mod = 7 + 1e+9;

int memo[30][2 << 17];
int A[32], B[32], N, C;

int rec(int n, int p) {
  if (n >= N)
    return 0;

  if (memo[n][p] != -1)
    return memo[n][p];

  int res = 0;
  p |= A[n];
  REP(i, C) {
    res = max(res, rec(n + 1, p & ~B[i]) + __builtin_popcount(p & B[i]));
  }
  return memo[n][p] = res;
}

int main() {
  int a, b;
  while (cin >> N >> C && (N || C)) {
    memset(memo, -1, sizeof(memo));
    memset(A, 0, sizeof(A));
    memset(B, 0, sizeof(B));
    REP(i, N) {
      REP(j, 16) {
        cin >> a;
        A[i] += (a << j);
      }
    }
    REP(i, C) {
      REP(j, 16) {
        cin >> b;
        B[i] += (b << j);
      }
    }
    cout << rec(0, 0) << endl;
  }
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

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

const int INF = 1001001001;

int N, C;
int panel[30], button[30];
int dp[31][1 << 16];

int popCount(int n) {
  int res = 0;
  REP(i, 16) {
    if (n >> i & 1) {
      res += 1;
    }
  }
  return res;
}

int rec(int index, int state) {
  if (index >= N) {
    return 0;
  }
  if (dp[index][state] != -1) {
    return dp[index][state];
  }

  int p = state | panel[index];
  int res = 0;
  REP(i, C) {
    int match = p & button[i];
    res = std::max(res, popCount(match) + rec(index + 1, p ^ match));
  }

  return dp[index][state] = res;
}

int main() {
  while (scanf("%d %d", &N, &C), N && C) {
    std::fill(panel, panel + 30, 0);
    std::fill(button, button + 30, 0);
    std::fill(&dp[0][0], &dp[0][0] + 31 * (1 << 16), -1);

    REP(i, N) {
      REP(j, 16) {
        int a;
        scanf("%d", &a);
        panel[i] = panel[i] * 2 + a;
      }
    }

    REP(i, C) {
      REP(j, 16) {
        int b;
        scanf("%d", &b);
        button[i] = button[i] * 2 + b;
      }
    }

    rec(0, 0);

    printf("%d\\n", dp[0][0]);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

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

const int INF = 1001001001;

int N, C;
int panel[30], button[30];
int dp[31][1 << 16];

int popCount(int n) {
  int res = 0;
  REP(i, 16) {
    if (n >> i & 1) {
      res += 1;
    }
  }
  return res;
}

int main() {
  while (scanf("%d %d", &N, &C), N && C) {
    std::fill(panel, panel + 30, 0);
    std::fill(button, button + 30, 0);
    std::fill(&dp[0][0], &dp[0][0] + 31 * (1 << 16), -INF);

    REP(i, N) {
      REP(j, 16) {
        int a;
        scanf("%d", &a);
        panel[i] = panel[i] * 2 + a;
      }
    }

    REP(i, C) {
      REP(j, 16) {
        int b;
        scanf("%d", &b);
        button[i] = button[i] * 2 + b;
      }
    }

    dp[0][0] = 0;
    REP(i, N) {
      REP(j, 1 << 16) {
        int p = j | panel[i];
        REP(k, C) {
          int match = p & button[k];
          dp[i + 1][p ^ match] =
              std::max(dp[i + 1][p ^ match], dp[i][j] + popCount(match));
        }
      }
    }

    int res = 0;
    REP(i, 1 << 16) { res = std::max(res, dp[N][i]); }

    printf("%d\\n", res);
  }
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>

using namespace std;

void input(int *a, int n) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 16; j++) {
      int ai;
      cin >> ai;
      a[i] = a[i] * 2 + ai;
    }
  }
}

int main() {
  int pop[1 << 16];
  for (int i = 0; i < 1 << 16; i++) {
    pop[i] = bitset<32>(i).count();
  }
  for (int n, c; cin >> n >> c, n;) {
    int a[30] = {}, b[30] = {};
    input(a, n);
    input(b, c);
    static int dp[31][1 << 16];
    fill(dp[0], dp[31], -(1 << 29));
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 1 << 16; j++) {
        for (int k = 0; k < c; k++) {
          int l = j | a[i];
          int &t = dp[i + 1][l & ~b[k]];
          t = max<int>(t, dp[i][j] + pop[l & b[k]]);
        }
      }
    }
    cout << *max_element(dp[0], dp[31]) << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>

using namespace std;

void input(int *a, int n) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 16; j++) {
      int ai;
      cin >> ai;
      a[i] = a[i] * 2 + ai;
    }
  }
}

int main() {
  for (int n, c; cin >> n >> c, n;) {
    int a[30] = {}, b[30] = {};
    input(a, n);
    input(b, c);
    static int dp[31][1 << 16];
    fill(dp[0], dp[31], -(1 << 29));
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 1 << 16; j++) {
        for (int k = 0; k < c; k++) {
          int l = j | a[i];
          int &t = dp[i + 1][l & ~b[k]];
          int p = 0, bits = l & b[k];
          while (bits) {
            p++;
            bits = bits & bits - 1;
          }
          t = max<int>(t, dp[i][j] + p);
        }
      }
    }
    cout << *max_element(dp[0], dp[31]) << endl;
  }
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

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

using namespace std;

vector<bitset<16>> v, c;
int memo[31][1 << 16];
int n, m;

int dfs(bitset<16> t, int i) {
  if (i == n)
    return 0;

  t |= v[i];
  if (memo[i][t.to_ulong()] != -1)
    return memo[i][t.to_ulong()];

  int ret = 0;
  rep(j, m) {
    bitset<16> bit = t & c[j];
    int temp = t.to_ulong() - bit.to_ulong();

    ret = max(ret, dfs(bitset<16>(temp), i + 1) + (int)bit.count());
  }

  return memo[i][t.to_ulong()] = ret;
};

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

    v.clear();
    c.clear();
    memset(memo, -1, sizeof(memo));
    int temp;

    rep(i, n) {
      double t = pow(2.0, 15);
      int ret = 0;
      rep(j, 16) {
        cin >> temp;
        ret += temp * t;
        t /= 2;
      }

      bitset<16> bit(ret);
      v.push_back(bit);
    }

    rep(i, m) {
      double t = pow(2.0, 15);
      int ret = 0;
      rep(j, 16) {
        cin >> temp;
        ret += temp * t;
        t /= 2;
      }

      bitset<16> bit(ret);
      c.push_back(bit);
    }

    cout << dfs(bitset<16>(0), 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

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

using namespace std;

vector<bitset<16>> v, c;
int memo[31][1 << 16];
int n, m;

int dfs(bitset<16> t, int i) {
  if (i == n)
    return 0;
  if (memo[i][t.to_ulong()] != -1)
    return memo[i][t.to_ulong()];

  t |= v[i];
  int ret = 0;
  rep(j, m) {
    bitset<16> bit = t & c[j];
    int temp = t.to_ulong() - bit.to_ulong();

    ret = max(ret, dfs(bitset<16>(temp), i + 1) + (int)bit.count());
  }

  return memo[i][t.to_ulong()] = ret;
};

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

    v.clear();
    c.clear();
    memset(memo, -1, sizeof(memo));
    int temp;

    rep(i, n) {
      double t = pow(2.0, 15);
      int ret = 0;
      rep(j, 16) {
        cin >> temp;
        ret += temp * t;
        t /= 2;
      }

      bitset<16> bit(ret);
      v.push_back(bit);
    }

    rep(i, m) {
      double t = pow(2.0, 15);
      int ret = 0;
      rep(j, 16) {
        cin >> temp;
        ret += temp * t;
        t /= 2;
      }

      bitset<16> bit(ret);
      c.push_back(bit);
    }

    cout << dfs(bitset<16>(0), 0) << endl;
  }

  return 0;
}
Problem p00261 (C++) — buggy submission, verdict: Runtime Error

#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;
const int V = 6;

bool solve(string &s) {
  int v[V][2];
  v[0][0] = 1;
  v[0][1] = 2;
  v[1][0] = -1;
  v[1][1] = 3;
  v[2][0] = 1;
  v[2][1] = -1;
  v[3][0] = 4;
  v[3][1] = 5;
  v[3][0] = 4;
  v[3][1] = 5;
  v[4][0] = 5;
  v[4][1] = 2;
  v[5][0] = 2;
  v[5][1] = 1;
  int n = 0;
  REP(i, s.length()) {
    if (v[n][s[i] - '0'] == -1)
      return false;
    n = v[n][s[i] - '0'];
  }
  return (n == 5 ? true : false);
}

int main() {
  string s;
  while (cin >> s && s != "#")
    cout << (solve(s) ? "Yes" : "No") << endl;
  return 0;
}

Error: -11

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;
const int V = 6;

int main() {
  string s;
  int v[V][2];
  v[0][0] = 1;
  v[0][1] = 2;
  v[1][0] = -1;
  v[1][1] = 3;
  v[2][0] = 1;
  v[2][1] = -1;
  v[3][0] = 4;
  v[3][1] = 5;
  v[3][0] = 4;
  v[3][1] = 5;
  v[4][0] = 5;
  v[4][1] = 2;
  v[5][0] = 2;
  v[5][1] = 1;
  while (cin >> s && s != "#") {
    int n = 0;
    REP(i, s.length()) n = v[n][s[i] - '0'];
    cout << (n == 5 ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p00261 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int d[10][10];
void init() {
  for (int i = 0; i < 6; i++)
    for (int j = 0; j < 6; j++)
      d[i][j] = -1;
  d[0][0] = d[5][1] = d[2][0] = 1;
  d[0][1] = d[4][1] = d[5][0] = 2;
  d[1][1] = 3;
  d[3][0] = 4;
  d[3][1] = d[4][0] = 5;
}
string s;
void solve() {
  int p = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    int b = s[i] - '0';
    p = d[p][b];
    if (p == -1)
      break;
  }
  cout << (p == 5 ? "Yes" : "No") << endl;
}
int main() {
  init();
  while (cin >> s && s != "#") {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, dmax;
int m[300];
bool visited[300];
bool isVisit[300];
bool isGoal;
void dfs(int p) {
  if (visited[p])
    return;
  visited[p] = true;
  isVisit[p] = true;
  if (p == n + 1) {
    isGoal = true;
    return;
  }
  for (int i = 1; i <= dmax && p + i <= n + 1; i++) {
    int np = p + i;
    np = np + m[np];
    if (np > n + 1)
      np = n + 1;
    if (np < 0)
      np = 0;
    dfs(np);
  }
}
void solve() {
  fill(isVisit, isVisit + (n + 2), false);
  isVisit[0] = true;
  for (int i = 0; i <= n; i++) {
    if (!isVisit[i])
      continue;
    isGoal = false;
    fill(visited, visited + (n + 2), false);
    dfs(i);
    if (!isGoal)
      break;
  }
  cout << (isGoal ? "OK" : "NG") << endl;
}
int main() {
  while (cin >> dmax, dmax) {
    cin >> n;
    fill(m, m + (n + 2), 0);
    for (int i = 0; i < n; i++) {
      cin >> m[i + 1];
    }
    solve();
  }

  return 0;
}
Problem p00261 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main() {

  int p;
  char r;
  char Q;
  r = 'A';
  while (1) {

    scanf("%c", &Q);
    if (Q == '#')
      break;
    if (Q == '\\n') {
      if (r == 'B')
        printf("Yes\\n");
      else
        printf("No\\n");

      r = 'A';
    }

    else if (r == 'A') {
      if (Q == '0')
        r = 'X';
      else if (Q == '1')
        r = 'Y';
      else
        r = 'H';
    }

    else if (r == 'X') {
      if (Q == '1')
        r = 'Z';
      else
        r = 'H';
    }

    else if (r == 'Y') {
      if (Q == '0')
        r = 'X';
      else
        r = 'H';
    }

    else if (r == 'Z') {
      if (Q == '0')
        r = 'W';
      else if (Q == '1')
        r = 'B';
      else
        r = 'H';
    }

    else if (r == 'W') {
      if (Q == '0')
        r = 'B';
      else if (Q == '1')
        r = 'Y';
      else
        r = 'H';
    }

    else if (r == 'B') {
      if (Q == '0')
        r = 'Y';
      else if (Q == '1')
        r = 'X';
      else
        r = 'H';
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {

  int p;
  char r;
  char Q;
  r = 'A';
  while (1) {

    scanf("%c", &Q);

    if (Q == '\\n') {
      if (r == 'B')
        printf("Yes\\n");
      else
        printf("No\\n");

      r = 'A';
    }

    else if (r == 'A') {
      if (Q == '0')
        r = 'X';
      else if (Q == '1')
        r = 'Y';
      else
        r = 'H';
    }

    else if (r == 'X') {
      if (Q == '1')
        r = 'Z';
      else
        r = 'H';
    }

    else if (r == 'Y') {
      if (Q == '0')
        r = 'X';
      else
        r = 'H';
    }

    else if (r == 'Z') {
      if (Q == '0')
        r = 'W';
      else if (Q == '1')
        r = 'B';
      else
        r = 'H';
    }

    else if (r == 'W') {
      if (Q == '0')
        r = 'B';
      else if (Q == '1')
        r = 'Y';
      else
        r = 'H';
    }

    else if (r == 'B') {
      if (Q == '0')
        r = 'Y';
      else if (Q == '1')
        r = 'X';
      else
        r = 'H';
    }

    else if (Q == '#')
      break;
  }

  return 0;
}