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

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

typedef long long ll;

#define pb push_back

int cnt_bit(int n) {
  int ret = 0;
  while (n > 0) {
    ret++;
    n -= n & -n;
  }
  return ret;
}

int main() {
  while (1) {
    static int n, c, in;
    static int a[32], b[32];
    static int dp[32][1 << 16];
    scanf("%d%d", &n, &c);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &in);
        a[i] <<= 1;
        a[i] |= in;
      }
    }
    for (int i = 0; i < c; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &in);
        b[i] <<= 1;
        b[i] |= in;
      }
    }
    int t = 0, t_ = 1;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = -1000000;
      }
    }
    dp[t][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[t][j] < 0)
          continue;
        for (int k = 0; k < c; k++) {
          int sc = cnt_bit((j | a[i]) & b[k]);
          int nex = (j | a[i]) - ((j | a[i]) & b[k]);
          dp[t_][nex] = max(dp[t_][nex], dp[t][j] + sc);
        }
      }
      swap(t, t_);
    }
    int ret = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ret = max(ret, dp[t][i]);
    }
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

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

typedef long long ll;

#define pb push_back

int cnt_bit(int n) {
  int ret = 0;
  while (n > 0) {
    ret++;
    n -= n & -n;
  }
  return ret;
}

int main() {
  while (1) {
    static int n, c, in;
    static int a[32], b[32];
    static int dp[32][1 << 16];
    scanf("%d%d", &n, &c);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &in);
        a[i] <<= 1;
        a[i] |= in;
      }
    }
    for (int i = 0; i < c; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &in);
        b[i] <<= 1;
        b[i] |= in;
      }
    }
    int t = 0, t_ = 1;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = -1000000;
      }
    }
    dp[t][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 sc = cnt_bit((j | a[i]) & b[k]);
          int nex = (j | a[i]) - ((j | a[i]) & b[k]);
          dp[t_][nex] = max(dp[t_][nex], dp[t][j] + sc);
        }
      }
      swap(t, t_);
    }
    int ret = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ret = max(ret, dp[t][i]);
    }
    printf("%d\\n", ret);
  }
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define N 16

int bitcount(int n) { return n ? (n & 1) + bitcount(n >> 1) : 0; }

int main() {
  int n, c;
  vector<int> a, b;
  while (cin >> n >> c, n | c) {
    a.clear();
    a.resize(n, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < N; j++) {
        int in;
        cin >> in;
        a[i] |= in << j;
      }
    }
    b.clear();
    b.resize(c, 0);
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < N; j++) {
        int in;
        cin >> in;
        b[i] |= in << j;
      }
    }

    vector<vector<int>> dp(n + 1, vector<int>(1 << N, -1));
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << N); j++) {
        if (dp[i][j] < 0)
          continue;
        for (int k = 0; k < c; k++) {
          int panel = a[i] | j;
          int pushed = panel & b[k];
          int next = panel ^ pushed;
          dp[i + 1][next] = max(dp[i + 1][next], dp[i][j] + bitcount(pushed));
        }
      }
    }
    int point = 0;
    for (int j = 0; j < (1 << N); j++)
      point = max(point, dp[n][j]);
    cout << point << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n, c, a[30], b[30], memo[30][1 << 16];

int bitcount(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = ((bits >> 4) + bits) & 0x0f0f0f0f;
  bits += bits >> 8;
  return (bits + (bits >> 16)) & 0xff;
}

int rec(int step, int panel) {
  if (step == n)
    return 0;
  if (memo[step][panel] >= 0)
    return memo[step][panel];

  panel |= a[step];

  int res = 0;
  for (int i = 0; i < c; i++) {
    int pushed = panel & b[i];
    res = max(res, rec(step + 1, panel ^ pushed) + bitcount(pushed));
  }

  return memo[step][panel] = res;
}

int main() {
  while (cin >> n >> c, n | c) {
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        int in;
        cin >> in;
        a[i] |= in << j;
      }
    }
    for (int i = 0; i < c; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        int in;
        cin >> in;
        b[i] |= in << j;
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < (1 << 16); j++)
        memo[i][j] = -1;
    cout << rec(0, 0) << endl;
  }
}
Problem p00258 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int n, c;
int a[35], b[35];
int dp[35][1 << 16];
int main() {
  while (cin >> n >> c && n | c) {
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 0; i < n + c; i++) {
      int k;
      int res = 0;
      for (int j = 0; j < 16; j++) {
        cin >> k;
        if (k == 1) {
          res <<= 1;
          res += 1;
        } else if (k == 0) {
          res <<= 1;
        }
      }
      if (i < n)
        a[i] = res;
      else
        b[i - n] = res;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] == -1)
          continue;
        int s = (j | a[i]);
        for (int k = 0; k < c; k++) {
          int p = __builtin_popcount(b[k] & s);
          dp[i + 1][s - (b[k] & s)] =
              max(dp[i + 1][s - (b[k] & s)], dp[i][j] + p);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ans = max(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int n, c;
int a[35], b[35];
int main() {
  while (1) {
    cin >> n >> c;
    if (n == 0 && c == 0)
      break;
    int dp[35][(1 << 16)];
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 0; i < n + c; i++) {
      int k;
      int res = 0;
      for (int j = 0; j < 16; j++) {
        cin >> k;
        if (k == 1) {
          res <<= 1;
          res += 1;
        } else if (k == 0) {
          res <<= 1;
        }
      }
      if (i < n)
        a[i] = res;
      else
        b[i - n] = res;
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i][j] == -1)
          continue;
        int s = (j | a[i]);
        for (int k = 1; k <= c; k++) {
          int p = __builtin_popcount(b[k] & s);
          dp[i + 1][s - (b[k] & s)] =
              max(dp[i + 1][s - (b[k] & s)], dp[i][j] + p);
        }
      }
    }
    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 <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int n, c;
    cin >> n >> c;

    vector<bitset<16>> x(n);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 16; ++j) {
        int a;
        cin >> a;
        x[i][j] = (a == 1);
      }
    }
    if (n == 0)
      return 0;

    vector<bitset<16>> y(c);
    for (int i = 0; i < c; ++i) {
      for (int j = 0; j < 16; ++j) {
        int a;
        cin >> a;
        y[i][j] = (a == 1);
      }
    }

    vector<int> dp(1 << 16, -1);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
      vector<int> next(1 << 16, -1);
      for (int j = 0; j < (1 << 16); ++j) {
        if (dp[j] == -1)
          continue;
        for (int k = 0; k < c; ++k) {
          bitset<16> bs(j);
          bs |= x[i];
          int p = (bs & y[k]).count();
          bs &= ~y[k];
          next[bs.to_ulong()] = max(next[bs.to_ulong()], dp[j] + p);
        }
      }
      dp.swap(next);
    }

    cout << *max_element(dp.begin(), dp.end()) << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  for (;;) {
    int n, c;
    cin >> n >> c;

    vector<bitset<16>> x(n);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 16; ++j) {
        int a;
        cin >> a;
        x[i][j] = (a == 1);
      }
    }
    if (n == 0)
      return 0;

    vector<bitset<16>> y(c);
    for (int i = 0; i < c; ++i) {
      for (int j = 0; j < 16; ++j) {
        int a;
        cin >> a;
        y[i][j] = (a == 1);
      }
    }

    vector<int> dp(1 << 16, INT_MIN);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
      vector<int> next(1 << 16, INT_MIN);
      for (int j = 0; j < (1 << 16); ++j) {
        for (int k = 0; k < c; ++k) {
          bitset<16> bs(j);
          bs |= x[i];
          int p = (bs & y[k]).count();
          bs &= ~y[k];
          next[bs.to_ulong()] = max(next[bs.to_ulong()], dp[j] + p);
        }
      }
      dp.swap(next);
    }

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>

using namespace std;

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

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

const int INF = 1001001001;

int input_bit() {
  int r = 0;
  rep(i, 16) {
    int b;
    cin >> b;
    r <<= 1;
    r += b;
  }
  return r;
}

int func(int n, int c) {
  vi a(n);
  rep(i, n) a[i] = input_bit();

  vi b(c);
  rep(i, c) b[i] = input_bit();

  vvi dp(n + 1);
  dp[0].assign(1 << 16, -INF);
  dp[0][0] = 0;

  rep(i, n) {
    dp[i + 1].assign(1 << 16, -INF);
    rep(m, 1 << 16) {
      if (dp[i][m] == -INF)
        continue;

      rep(j, c) {
        int v = m | a[i];
        int cnt = 0;
        rep(k, 16) {
          if (((v >> k) & 1) && ((b[j] >> k) & 1)) {
            ++cnt;
            v ^= 1 << k;
          }
        }
        dp[i + 1][v] = max(dp[i + 1][v], dp[i][m] + cnt);
      }
    }
    dp[i].clear();
  }

  return *max_element(dp[n].begin(), dp[n].end());
}

int main() {
  int n, c;
  while (cin >> n >> c, n) {
    cout << func(n, c) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>

using namespace std;

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

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

const int INF = 1001001001;

int input_bit() {
  int r = 0;
  rep(i, 16) {
    int b;
    cin >> b;
    r <<= 1;
    r += b;
  }
  return r;
}

int func(int n, int c) {
  vi a(n);
  rep(i, n) a[i] = input_bit();

  vi b(c);
  rep(i, c) b[i] = input_bit();

  vvi dp(n + 1);
  dp[0].assign(1 << 16, -INF);
  dp[0][0] = 0;

  rep(i, n) {
    dp[i + 1].assign(1 << 16, -INF);
    rep(m, 1 << 16) {
      rep(j, c) {
        int v = m | a[i];
        int cnt = 0;
        rep(k, 16) {
          if (((v >> k) & 1) && ((b[j] >> k) & 1)) {
            ++cnt;
            v ^= 1 << k;
          }
        }
        dp[i + 1][v] = max(dp[i + 1][v], dp[i][m] + cnt);
      }
    }
    dp[i].clear();
  }

  return *max_element(dp[n].begin(), dp[n].end());
}

int main() {
  int n, c;
  while (cin >> n >> c, n) {
    cout << func(n, c) << endl;
  }
}
Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

int n, c;
int a[31], b[31];
int dp[31][1 << 16];

int dfs(int x, int bit) {
  if (dp[x][bit] >= 0)
    return dp[x][bit];
  if (x == n)
    return 0;
  int nbit = a[x] | bit;
  int res = 0;
  for (int j = 0; j < c; j++) {
    int plu = __builtin_popcount(nbit & b[j]);
    int kbit = nbit - (nbit & b[j]);
    res = max(dfs(x + 1, kbit) + plu, res);
  }
  return dp[x][bit] = res;
}

int main(void) {
  while (1) {
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(dp, -1, sizeof(dp));
    scanf("%d%d", &n, &c);
    if (n == 0 && c == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) {
        int q;
        scanf("%d", &q);
        if (q == 1)
          a[i] |= 1 << j;
      }
    }
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        int q;
        scanf("%d", &q);
        if (q == 1)
          b[i] |= 1 << j;
      }
    }
    printf("%d\\n", dfs(0, 0));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

int n, c;
int res;
int a[31], b[31];
int dp[31][1 << 16];

void dfs(int x, int bit, int cnt) {
  if (dp[x][bit] > cnt)
    return;
  int nbit = a[x] | bit;
  if (x < n)
    for (int j = 0; j < c; j++) {
      int plus = __builtin_popcount(nbit & b[j]);
      int kbit = nbit - (nbit & b[j]);
      if (dp[x + 1][kbit] < cnt + plus) {
        dp[x + 1][kbit] = cnt + plus;
        res = max(dp[x + 1][kbit], res);
        dfs(x + 1, kbit, cnt + plus);
      }
    }
}

int main(void) {
  while (1) {
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(dp, 0, sizeof(dp));
    res = 0;
    scanf("%d%d", &n, &c);
    if (n == 0 && c == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) {
        int q;
        scanf("%d", &q);
        if (q == 1)
          a[i] |= 1 << j;
      }
    }
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        int q;
        scanf("%d", &q);
        if (q == 1)
          b[i] |= 1 << j;
      }
    }
    dfs(0, 0, 0);
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00258 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, c, a[30], b[31];
  while (cin >> n >> c, n) {
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        int aa;
        cin >> aa;
        if (aa)
          a[i] |= 1 << j;
      }
    }
    for (int i = 0; i < c; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        int bb;
        cin >> bb;
        if (bb)
          b[i] |= 1 << j;
      }
    }
    b[c] = 0;
    c++;

    int dp[31][1 << 16];
    fill_n(*dp, 31 * (1 << 16), -1);
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {         // 今押している所
      for (int j = 0; j < 1 << 16; j++) { // 前のbitの状態
        if (dp[i][j] == -1)
          continue;
        for (int k = 0; k < c; k++) {
          int bits = (a[i] | j) & ~b[k];
          dp[i + 1][bits] =
              max(dp[i + 1][bits],
                  dp[i][j] + __builtin_popcount((a[i] | j) & b[k]));
        }
      }
    }
    cout << *max_element(dp[0], dp[0] + 31 * (1 << 16)) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
queue<int> que[33];
int main() {
  int n, c, a[30], b[31];
  while (cin >> n >> c, n) {
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        int aa;
        cin >> aa;
        if (aa)
          a[i] |= 1 << j;
      }
    }
    for (int i = 0; i < c; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        int bb;
        cin >> bb;
        if (bb)
          b[i] |= 1 << j;
      }
    }
    b[c] = 0;
    c++;

    que[0].push(0);
    int dp[31][1 << 16];
    fill_n(*dp, 31 * (1 << 16), -1);
    dp[0][0] = 0;
    int ret = 0;
    for (int i = 0; i < n; i++) { // 今押している所
      while (!que[i].empty()) {
        int j = que[i].front();
        que[i].pop();
        for (int k = 0; k < c; k++) {
          int bits = (a[i] | j) & ~b[k];
          dp[i + 1][bits] =
              max(dp[i + 1][bits],
                  dp[i][j] + __builtin_popcount((a[i] | j) & b[k]));
          que[i + 1].push(bits);
          ret = max(dp[i + 1][bits], ret);
        }
      }
    }
    cout << ret << endl;
  }
}
Problem p00258 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int a[31], b[31], dp[31][1 << 16];

int count(int num) {
  int res = 0;
  for (int i = 0; i < 16; i++)
    if (num & (1 << i))
      res++;
  return res;
}

int main(void) {

  int n, c, in;

  while (cin >> n >> c, n | c) {

    fill(a, a + n, 0);
    fill(b, b + c, 0);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> in;
        a[i] |= in << (15 - j);
      }
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> in;
        b[i] |= in << 15 - j;
      }
    }

    memset(dp, -1, sizeof(dp));

    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 now = a[i] | j;

        for (int k = 0; k < c; k++) {
          int next = now & ~b[k];
          dp[i + 1][next] = max(dp[i + 1][next], dp[i][j] + count(now & b[k]));
        }
      }
    }
    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 <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int a[30], b[30], dp[30][1 << 16];

int count(int num) {
  int res = 0;
  for (int i = 0; i < 16; i++)
    if (num & (1 << i))
      res++;
  return res;
}

int main(void) {

  int n, c, in;

  while (cin >> n >> c, n | c) {

    fill(a, a + n, 0);
    fill(b, b + c, 0);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> in;
        a[i] |= in << (15 - j);
      }
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> in;
        b[i] |= in << 15 - j;
      }
    }

    memset(dp, -1, sizeof(dp));

    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 now = a[i] | j;

        for (int k = 0; k < c; k++) {
          int next = now & ~b[k];
          dp[i + 1][next] = max(dp[i + 1][next], dp[i][j] + count(now & b[k]));
        }
      }
    }
    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;
int dp[31][1 << 16];
int P[1 << 16];
int main() {
  for (int i = 0; i < (1 << 16); i++) {
    for (int j = 0; j < 16; j++) {
      if ((i >> j) & 1)
        P[i]++;
    }
  }
  while (true) {
    int n;
    int c;
    int a[31];
    int b[32];
    for (int i = 0; i <= 30; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = -1;
      }
      a[i] = 0;
      b[i] = 0;
      b[i + 1] = 0;
    }
    dp[0][0] = 0;
    scanf("%d%d", &n, &c);
    if (n == 0 && c == 0)
      break;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 16; j++) {
        int x;
        scanf("%d", &x);
        if (x == 1)
          a[i] += (1 << j);
      }
    }
    for (int i = 1; i <= c; i++) {
      for (int j = 0; j < 16; j++) {
        int x;
        scanf("%d", &x);
        if (x == 1)
          b[i] += (1 << j);
      }
    }
    b[c + 1] = 0;
    c++;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i - 1][j] == -1)
          continue;
        int board = j | a[i];
        // printf("%d %d %d\\n",j,a[i],board);
        for (int k = 1; k <= c; k++) {
          int point = P[b[k] & board] + dp[i - 1][j];
          int cou = board - (b[k] & board);
          dp[i][cou] = max(dp[i][cou], point);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ans = max(ans, dp[n][i]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[31][1 << 16];
int main() {
  while (true) {
    int n;
    int c;
    int a[31][16];
    int b[32][16];
    for (int i = 0; i <= 30; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = -1;
      }
    }
    dp[0][0] = 0;
    scanf("%d%d", &n, &c);
    if (n == 0 && c == 0)
      break;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 16; j++) {
        scanf("%d", &a[i][j]);
      }
    }
    for (int i = 1; i <= c; i++) {
      for (int j = 0; j < 16; j++) {
        scanf("%d", &b[i][j]);
      }
    }
    for (int j = 0; j < 16; j++)
      b[c + 1][j] = 0;
    c++;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        if (dp[i - 1][j] == -1)
          continue;
        int board[16];
        for (int k = 0; k < 16; k++) {
          if ((j >> k) & 1)
            board[k] = 1;
          else if (a[i][k] == 1)
            board[k] = 1;
          else
            board[k] = 0;
        }

        for (int k = 1; k <= c; k++) {
          int cou = 0;
          int point = dp[i - 1][j];
          for (int l = 0; l < 16; l++) {
            if (b[k][l] == 1 && board[l] == 1) {
              point++;
            } else if (board[l] == 1) {
              cou += (1 << l);
            }
          }
          dp[i][cou] = max(point, dp[i][cou]);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << 16); i++) {
      ans = max(ans, dp[n][i]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}