alexjercan/bugnet huggingface dataset:
Problem p00257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAXN = 256;

int maxi;
int n;
int d[MAXN];
bool vis[MAXN], vis2[MAXN];

int nextPos(int pos, int m) {
  int npos = min(pos + m, n - 1);
  npos += d[npos];
  npos = min(npos, n - 1);
  npos = max(npos, 0);
  return npos;
}

void rec(int pos) {
  vis[pos] = true;
  for (int m = 1; m <= maxi; ++m) {
    int npos = nextPos(pos, m);
    if (vis[npos])
      continue;
    rec(npos);
  }
}

bool rec2(int pos) {
  vis2[pos] = true;
  if (pos == n - 1)
    return true;
  for (int m = 1; m <= maxi; ++m) {
    int npos = nextPos(pos, m);
    if (vis2[npos])
      continue;
    if (rec2(npos))
      return true;
  }
  return false;
}

int main() {
  while (cin >> maxi && maxi) {
    cin >> n;
    for (int i = 0; i < n; ++i)
      cin >> d[i + 1];
    n += 2;
    d[0] = d[n - 1] = 0;
    fill(vis, vis + n, false);
    rec(0);
    bool res = true;
    for (int i = 0; i < n; ++i) {
      if (vis[i]) {
        fill(vis2, vis2 + n, false);
        if (!rec2(i)) {
          res = false;
          break;
        }
      }
    }
    if (res)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

bool check(char data[4]) {
  for (int i = 1; i < 4; i++) {
    if (data[0] != data[i])
      return false;
  }
  return true;
}

int toInt(char data[4]) {
  int ret = 0;
  for (int i = 0; i < 4; i++) {
    ret += (data[i] - '0');
    ret *= 10;
  }
  return ret / 10;
}

void getNext(int a, char data[4]) {
  for (int i = 0; i < 4; i++) {
    data[3 - i] = a % 10 + '0';
    a /= 10;
  }
}

void solve(char data[4]) {

  if (check(data)) {
    cout << "NA" << endl;
    return;
  }

  if (toInt(data) == 6174) {
    cout << 0 << endl;
    return;
  }

  int res;
  for (res = 1;; res++) {
    int L, S;
    sort(data, data + 4);
    S = toInt(data);
    reverse(data, data + 4);
    L = toInt(data);
    if (L - S == 6174)
      break;
    getNext(L - S, data);
  }
  cout << res << endl;
}

int main() {

  char data[4];
  while (1) {
    bool f = true;
    for (int i = 0; i < 4; i++) {
      cin >> data[i];
      if (data[i] != '0')
        f = false;
    }
    if (f)
      break;
    solve(data);
  }
  return 0;
}
Problem p00257 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 255
using namespace std;
int m, n, d[N];
bool used[2][N], ans;

void dfs(int x, int idx) {
  if (used[idx][x])
    return;
  used[idx][x] = true;
  x += d[x];
  if (x < 0)
    x = 0;
  if (n + 1 < x)
    x = n + 1;
  for (int i = 1; i <= m; i++) {
    int nx = x + i;
    if (n + 1 < nx)
      nx = n + 1;
    dfs(nx, idx);
  }
}

int main() {
  while (1) {
    cin >> m;
    if (!m)
      break;
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> d[i];
    memset(used, 0, sizeof(used));
    ans = true;
    dfs(0, 0);
    if (!used[0][n + 1])
      ans = false;
    for (int i = 1; i < n + 1; i++) {
      if (!used[0][i])
        continue;
      for (int j = 0; j <= n + 1; j++)
        used[1][j] = false;
      dfs(i, 1);
      if (!used[1][n + 1])
        ans = false;
    }
    if (ans)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define N 255
using namespace std;
int m, n, d[N];
bool used[2][N], ans;

void dfs(int x, int idx) {
  if (used[idx][x])
    return;
  x += d[x];
  if (x < 0)
    x = 0;
  if (n + 1 < x)
    x = n + 1;
  for (int i = 1; i <= m; i++) {
    int nx = x + i;
    if (n + 1 < nx)
      nx = n + 1;
    dfs(nx, idx);
  }
}

int main() {
  while (1) {
    cin >> m;
    if (!m)
      break;
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> d[i];
    memset(used, 0, sizeof(used));
    ans = true;
    dfs(0, 0);
    if (!used[0][n + 1])
      ans = false;
    for (int i = 1; i < n + 1; i++) {
      if (!used[0][i])
        continue;
      for (int j = 0; j <= n + 1; j++)
        used[1][j] = false;
      dfs(i, 1);
      if (!used[1][n + 1])
        ans = false;
    }
    if (ans)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00257 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef long long ll;

#define pb push_back

int main() {
  while (1) {
    int max;
    int n, d[252];
    scanf("%d", &max);
    if (max == 0)
      break;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &d[i]);
    }
    vector<int> G[252];
    vector<int> rG[252];
    for (int i = 0; i < n + 1; i++) {
      for (int j = 1; j <= max; j++) {
        if (i + j >= n + 1 || i + j + d[i + j] >= n + 1) {
          G[i].pb(n + 1);
          rG[n + 1].pb(i);
        } else if (i + j + d[i + j] >= 0) {
          G[i].pb(i + j + d[i + j]);
          rG[i + j + d[i + j]].pb(i);
        } else {
          G[i].pb(0);
          rG[0].pb(i);
        }
      }
    }
    bool used[252];
    queue<int> que;
    for (int i = 0; i < 252; i++) {
      used[i] = false;
      que.push(0);
    }
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      if (used[p])
        continue;
      used[p] = true;
      for (int i = 0; i < G[p].size(); i++) {
        que.push(G[p][i]);
      }
    }
    bool used_[252];
    queue<int> que_;
    for (int i = 0; i < 252; i++) {
      used_[i] = false;
      que_.push(n + 1);
    }
    while (!que_.empty()) {
      int p = que_.front();
      que_.pop();
      if (used_[p])
        continue;
      used_[p] = true;
      for (int i = 0; i < rG[p].size(); i++) {
        que_.push(rG[p][i]);
      }
    }
    bool t = true;
    for (int i = 0; i < 252; i++) {
      if (used[i] && !used_[i])
        t = false;
    }
    if (t)
      puts("OK");
    else
      puts("NG");
    /*for(int i = 0 ; i <= n+1 ; i ++){
            printf("G[%d]:",i);
            for(int j = 0 ; j < G[i].size() ; j ++){
                    printf("%d ",G[i][j]);
            }
            puts("");
            printf("rG[%d]:",i);
            for(int j = 0 ; j < rG[i].size() ; j ++){
                    printf("%d ",rG[i][j]);
            }
            puts("");
    }*/
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

#define pb push_back

int main() {
  while (1) {
    int max;
    int n, d[252];
    int dp[255] = {};
    scanf("%d", &max);
    if (max == 0)
      break;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &d[i]);
    }
    bool t = true;
    for (int s = 0; s < n + 1; s++) {
      for (int i = 0; i < 255; i++)
        dp[i] = 0;
      dp[s] = 1;
      for (int i = s; i < n + 1; i++) {
        if (dp[i] == 0)
          continue;
        for (int j = 1; j <= max; j++) {
          if (i + j >= n + 1)
            dp[n + 1] = 1;
          else {
            dp[i + j + d[i + j]] = 1;
          }
        }
      }
      t &= (dp[n + 1] != 0);
    }
    if (t)
      puts("OK");
    else
      puts("NG");
  }
}
Problem p00257 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
using namespace std;
int n, a[300], m;
bool u1[300], u2[300];
int main() {
  while (true) {
    cin >> m;
    if (m == 0)
      break;
    cin >> n;
    for (int i = 0; i < 300; i++)
      a[i] = 0;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    for (int i = 0; i < 300; i++)
      u1[i] = false;
    u1[0] = true;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        if (u1[j] == false)
          continue;
        for (int k = 1; k <= m; k++) {
          int G = j + k;
          if (G > n)
            G = n + 1;
          G += a[G];
          if (G < 0)
            G = 0;
          if (G > n)
            G = n + 1;
          u1[G] = true;
        }
      }
    }
    bool flag = false;
    for (int i = 0; i <= n; i++) {
      if (u1[i] == false)
        continue;
      for (int j = 0; j < 300; j++)
        u2[j] = false;
      u2[i] = true;
      queue<int> Q;
      Q.push(i);
      while (!Q.empty()) {
        int a1 = Q.front();
        Q.pop();
        for (int j = 1; j <= m; j++) {
          int G = a1 + j;
          if (G > n)
            G = n + 1;
          G += a[G];
          if (G < 0)
            G = 0;
          if (G > n)
            G = n + 1;
          if (u2[G] == false) {
            u2[G] = true;
            Q.push(G);
          }
        }
      }
      if (u2[n + 1] == false) {
        cout << "NG" << endl;
        flag = true;
        break;
      }
    }
    if (flag == false)
      cout << "OK" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int x[1000], n, maxn, k[1000], t[1000];
queue<int> Q;

void bfs(int s, int p) {
  Q.push(s);
  while (!Q.empty()) {
    for (int i = 1; i <= p; i++) {
      t[Q.front() + i] = 1;
      Q.push(x[Q.front() + i]);
    }
    Q.pop();
  }
}

int main() {
  while (true) {
    memset(k, 0, sizeof(k));
    cin >> maxn;
    if (maxn == 0) {
      break;
    }
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> x[i];
      x[i] += i;
      if (x[i] < 0) {
        x[i] = 0;
      }
      if (x[i] > n) {
        x[i] = n;
      }
    }
    bfs(0, maxn);
    for (int i = 0; i <= n; i++) {
      k[i] = t[i];
    }
    for (int i = 0; i < n; i++) {
      memset(t, 0, sizeof(t));
      if (k[i] == 1) {
        bfs(i, maxn);
        if (t[n + 1] == 0) {
          cout << "NG" << endl;
          goto owata;
        }
      }
    }
    cout << "OK" << endl;
  owata:;
  }
}
Problem p00257 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int max, n, data[255];
  bool MAS[255][255];
  while (1) {
    memset(MAS, false, sizeof(MAS));
    memset(data, false, sizeof(data));
    cin >> max;
    if (max == 0)
      break;
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> data[i];
    for (int j = 0; j < n + 2; j++) {
      for (int i = 1; i <= max && j + i < n + 2; i++) {
        if (j + i == n + 1)
          MAS[j][n + 1] = true;
        else
          j + i + data[j + i] >= n + 1
              ? MAS[j][n + 1] = true
              : (j + i + data[j + i] < 0 ? MAS[j][0] = true
                                         : MAS[j][j + i + data[i + j]] = true);
      }
    }

    for (int i = 0; i < n + 2; i++) {
      for (int j = 0; j < n + 2; j++) {
        for (int k = 0; k < n + 2; k++) {
          if (MAS[j][i] && MAS[i][k]) {
            MAS[j][k] = true;
          }
        }
      }
    }
    bool flg = false;
    for (int i = 0; i <= n; i++) {
      if (MAS[0][i] && (!MAS[i][n + 1])) {
        flg = true;
        break;
      }
    }
    cout << (flg ? "NG" : "OK") << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int max, n, data[255];
  bool MAS[255][255];
  while (1) {
    memset(MAS, false, sizeof(MAS));
    cin >> max;
    if (max == 0)
      break;
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> data[i];
    for (int j = 0; j <= n; j++) {
      for (int i = 1; i <= max && j + i < n + 2; i++) {
        j + i + data[i] > n + 1 ? MAS[j][n + 1] = true
                                : MAS[j][j + i + data[i + j]] = true;
      }
    }

    for (int i = 0; i < n + 2; i++) {
      for (int j = 0; j < n + 2; j++) {
        for (int k = 0; k < n + 2; k++) {
          if (MAS[j][i] && MAS[i][k]) {
            MAS[j][k] = true;
          }
        }
      }
    }
    bool flg = false;
    for (int i = 0; i <= n; i++) {
      if (MAS[0][i] && (!MAS[i][n + 1])) {
        flg = true;
        break;
      }
    }
    cout << (flg ? "NG" : "OK") << endl;
  }
}
Problem p00257 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int mx, n;

int main() {
  while (cin >> mx, mx) {
    int d[300] = {0}, g[300][300] = {0};
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> d[i];

    for (int i = 0; i <= n; i++) {
      for (int j = 1; j <= mx; j++) {
        int v = i + j + d[i + j];
        if (v < 0)
          v = 0;
        if (n + 1 < v)
          v = n + 1;
        g[i][v] = 1;
      }
    }
    for (int k = 0; k < n + 2; k++) {
      for (int i = 0; i < n + 2; i++) {
        for (int j = 0; j < n + 2; j++) {
          if (g[i][k] && g[k][j])
            g[i][j] = 1;
        }
      }
    }
    bool ans = true;
    for (int i = 0; i <= n; i++) {
      if (g[0][i] && !g[i][n + 1]) {
        ans = false;
      }
    }
    cout << (ans ? "OK" : "NG") << endl;
  }
}

Error: 0

Fixed submission:

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

int mx, n, d[300], g[300][300];
int memo[300] = {0};

int dfs(int k) {
  if (d[k] == 0)
    return k;

  if (n + 1 <= k + d[k])
    return n + 1;
  if (k + d[k] <= 0)
    return 0;

  if (memo[k] != -1)
    return memo[k];
  if (0 <= k && k < 300)
    return memo[k] = dfs(k + d[k]);
  return 0;
}

void add_edge(int u, int v) { g[u][v] = 1; }

int main() {
  while (cin >> mx, mx) {
    fill(d, d + 300, 0);
    fill(memo, memo + 300, -1);
    for (int i = 0; i < 300; i++)
      fill(g[i], g[i] + 300, 0);

    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> d[i];

    for (int i = 0; i <= n; i++) {
      if (d[i] == 0) {
        int u = i;
        for (int j = 1; j <= mx; j++) {
          int v = dfs(i + j);
          add_edge(u, v);
        }
      }
    }

    for (int k = 0; k <= n + 1; k++) {
      for (int i = 0; i <= n + 1; i++) {
        for (int j = 0; j <= n + 1; j++) {
          if (g[i][k] && g[k][j])
            g[i][j] = 1;
        }
      }
    }
    bool ans = true;
    for (int u = 0; u <= n; u++) {
      if (g[0][u] && !g[u][n + 1])
        ans = false;
    }
    cout << (ans ? "OK" : "NG") << endl;
  }
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

using namespace std;

int popcnt(int n) {
  n = ((n & 0xAAAAAAAA) >> 1) + (n & 0x55555555);
  n = ((n & 0xCCCCCCCC) >> 2) + (n & 0x33333333);
  n = ((n & 0xF0F0F0F0) >> 4) + (n & 0x0F0F0F0F);
  n = ((n & 0xFF00FF00) >> 8) + (n & 0x00FF00FF);
  n = ((n & 0xFFFF0000) >> 16) + (n & 0x0000FFFF);
  return n;
}

int main() {
  while (1) {
    int n, c;
    cin >> n >> c;
    if (!n)
      break;
    vector<int> a(n);
    vector<int> b(c);
    for (int i = 0; i < n; ++i) {
      bitset<16> bs;
      for (int j = 0; j < 16; ++j) {
        int t;
        cin >> t;
        bs[j] = t == 1;
      }
      a[i] = bs.to_ulong();
    }
    for (int i = 0; i < c; ++i) {
      bitset<16> bs;
      for (int j = 0; j < 16; ++j) {
        int t;
        cin >> t;
        bs[j] = t == 1;
      }
      b[i] = bs.to_ulong();
    }
    vector<vector<int>> dp(n + 1, vector<int>(1 << 16, -1000000000));
    dp[0][0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < (1 << 16); ++j) {
        bitset<16> bs(j);
        for (int k = 0; k < 16; ++k) {
          if ((a[i] >> k) & 1)
            bs[k] = true;
        }
        for (int k = 0; k < c; ++k) {
          int cp = bs.to_ulong();
          int point = dp[i][j];
          int and_cp = cp & b[k];
          cp ^= and_cp;
          point += popcnt(and_cp);
          dp[i + 1][cp] = max(dp[i + 1][cp], point);
        }
      }
    }
    cout << *max_element(begin(dp[n]), end(dp[n])) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

using namespace std;

int main() {
  while (1) {
    int n, c;
    cin >> n >> c;
    if (!n)
      break;
    vector<vector<int>> a(n, vector<int>(16));
    vector<vector<int>> b(c, vector<int>(16));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 16; ++j) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < c; ++i) {
      for (int j = 0; j < 16; ++j) {
        cin >> b[i][j];
      }
    }
    vector<vector<int>> dp(n + 1, vector<int>(1 << 16, -1000000000));
    dp[0][0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < (1 << 16); ++j) {
        bitset<16> bs(j);
        for (int k = 0; k < 16; ++k) {
          if (a[i][k])
            bs[k] = true;
        }
        for (int k = 0; k < c; ++k) {
          bitset<16> cp = bs;
          int point = dp[i][j];
          for (int l = 0; l < 16; ++l) {
            if (b[k][l] & cp[l]) {
              cp[l] = false;
              ++point;
            }
          }
          int m = cp.to_ulong();
          dp[i + 1][m] = max(dp[i + 1][m], point);
        }
      }
    }
    cout << *max_element(begin(dp[n]), end(dp[n])) << endl;
  }
  return 0;
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, c;
int a[30];
int b[30];
int memo[32][65536];

int changeBit(int num[]) {
  int n = 0;
  int p = 1;
  for (int i = 0; i < 16; i++) {
    n += num[15 - i] * p;
    p *= 2;
  }
  return n;
}

int check(int bit1, int bit2) {
  int tmp1, tmp2;
  int ttmp = 1;
  int ans = 0;
  for (int i = 0; i < 16; i++) {
    tmp1 = bit1 & ttmp;
    tmp2 = bit2 & ttmp;
    if (tmp1 == tmp2 && tmp1 != 0)
      ans++;
    ttmp = (ttmp << 1);
  }
  return ans;
}

int dfs(int i, int bit) {
  if (memo[i][bit] != -1)
    return memo[i][bit];
  if (i == n)
    return 0;

  int btmp = bit;
  bit |= a[i];

  int ans = dfs(i + 1, bit);
  for (int j = 0; j < c; j++) {
    ans = max(ans, dfs(i + 1, bit & (~b[j])) + check(bit, b[j]));
  }

  return memo[i][btmp] = ans;
}

int main() {
  while (1) {
    cin >> n >> c;
    if (n == 0 && c == 0)
      break;
    for (int i = 0; i < 32; i++)
      for (int j = 0; j < 65536; j++)
        memo[i][j] = -1;

    int num[16];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++)
        cin >> num[j];
      a[i] = changeBit(num);
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++)
        cin >> num[j];
      b[i] = changeBit(num);
    }

    cout << dfs(0, 0) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int n, c;
int a[30];
int b[30];
int memo[32][65536];

int changeBit(int num[]) {
  int n = 0;
  int p = 1;
  for (int i = 0; i < 16; i++) {
    n += num[15 - i] * p;
    p *= 2;
  }
  return n;
}

int check(int bit1, int bit2) {
  int tmp1, tmp2;
  int ttmp = 1;
  int ans = 0;
  for (int i = 0; i < 16; i++) {
    tmp1 = bit1 & ttmp;
    tmp2 = bit2 & ttmp;
    if (tmp1 == tmp2 && tmp1 != 0)
      ans++;
    ttmp = (ttmp << 1);
  }
  return ans;
}

int dfs(int i, int bit) {
  if (memo[i][bit] != -1)
    return memo[i][bit];
  if (i == n)
    return 0;

  bit |= a[i];

  int ans = dfs(i + 1, bit);
  for (int j = 0; j < c; j++) {
    ans = max(ans, dfs(i + 1, bit & (~b[j])) + check(bit, b[j]));
  }

  return memo[i][bit] = ans;
}

int main() {
  while (1) {
    cin >> n >> c;
    if (n == 0 && c == 0)
      break;
    for (int i = 0; i < 32; i++)
      for (int j = 0; j < 65536; j++)
        memo[i][j] = -1;

    int num[16];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++)
        cin >> num[j];
      a[i] = changeBit(num);
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++)
        cin >> num[j];
      b[i] = changeBit(num);
    }

    cout << dfs(0, 0) << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef bitset<16> bs16;

bs16 input() {
  string s;
  getline(cin, s);
  s.erase(remove(s.begin(), s.end(), ' '), s.end());
  return bs16(s);
}

int main() {
  int n, c;
  vector<int> init(65536, -1000000000);

  while (cin >> n >> c, n) {
    cin.ignore();

    vector<bs16> a(n), b(c);
    for (int i = 0; i < n; ++i) {
      a[i] = input();
    }
    for (int i = 0; i < c; ++i) {
      b[i] = input();
    }

    vector<int> dp1 = init, dp2;
    dp1[0] = 0;

    for (int i = 0; i < n; ++i) {
      dp2 = init;
      for (int S = 0; S < 65536; ++S) {
        if (dp1[S] < 0)
          continue;
        int T = S | a[i].to_ulong();
        dp2[T] = max(dp2[T], dp1[S]);
      }

      dp1 = dp2;
      for (int S = 0; S < 65536; ++S) {
        if (dp2[S] < 0)
          continue;
        bs16 p(S);
        for (int j = 0; j < c; ++j) {
          int x = (p & b[j]).count();
          int T = S & ~b[j].to_ulong();
          dp1[T] = max(dp1[T], dp2[S] + x);
        }
      }
    }

    cout << *max_element(dp1.begin(), dp1.end()) << '\\n';
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef bitset<16> bs16;

bs16 input() {
  string s;
  getline(cin, s);
  s.erase(remove(s.begin(), s.end(), ' '), s.end());
  return bs16(s);
}

int main() {
  int n, c;
  vector<int> init(65536, -1000000000);

  while (cin >> n >> c, n) {
    cin.ignore();

    vector<bs16> a(n), b(c);
    for (int i = 0; i < n; ++i) {
      a[i] = input();
    }
    for (int i = 0; i < c; ++i) {
      b[i] = input();
    }

    vector<int> dp1 = init, dp2;
    dp1[0] = 0;

    for (int i = 0; i < n; ++i) {
      dp2 = init;
      for (int S = 0; S < 65536; ++S) {
        int T = S | a[i].to_ulong();
        dp2[T] = max(dp2[T], dp1[S]);
      }

      dp1 = dp2;
      for (int S = 0; S < 65536; ++S) {
        bs16 p(S);
        for (int j = 0; j < c; ++j) {
          int x = (p & b[j]).count();
          int T = S & ~b[j].to_ulong();
          dp1[T] = max(dp1[T], dp2[S] + x);
        }
      }
    }

    cout << *max_element(dp1.begin(), dp1.end()) << '\\n';
  }
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, C, A[32], B[32];
int memo[1 << 16][32];

int rec(int panel, int now) {
  if (now == N)
    return 0;
  if (memo[panel][now] != -1)
    return memo[panel][now];

  int next = panel | A[now];

  int best = 0;

  for (int i = 0; i < C; i++) {
    best =
        max(best, rec(next & ~B[i], now + 1) + __builtin_popcount(next & B[i]));
  }

  return memo[panel][now] = best;
}

int main() {
  for (; cin >> N >> C && (N || C);) {
    memset(A, 0, sizeof A);
    memset(B, 0, sizeof B);
    memset(memo, -1, sizeof memo);

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 16; j++) {
        int b;
        cin >> b;

        A[i] |= (b << j);
      }
    }

    for (int i = 0; i < C; i++) {
      for (int j = 0; j < 16; j++) {
        int b;
        cin >> b;

        B[i] |= (b << j);
      }
    }

    cout << rec(0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

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

int N, C, A[32], B[32];
int memo[1 << 16][32];

int rec(int panel, int now) {
  if (now == N)
    return 0;
  if (memo[panel][now] != -1)
    return memo[panel][now];

  panel |= A[now];

  int best = 0;

  for (int i = 0; i < C; i++) {
    best = max(best,
               rec(panel & ~B[i], now + 1) + __builtin_popcount(panel & B[i]));
  }

  return memo[panel][now] = best;
}

int main() {
  for (; cin >> N >> C && (N || C);) {
    memset(A, 0, sizeof A);
    memset(B, 0, sizeof B);
    memset(memo, -1, sizeof memo);

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 16; j++) {
        int b;
        cin >> b;

        A[i] |= (b << j);
      }
    }

    for (int i = 0; i < C; i++) {
      for (int j = 0; j < 16; j++) {
        int b;
        cin >> b;

        B[i] |= (b << j);
      }
    }

    cout << rec(0, 0) << endl;
  }
}