Problem p00258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 99999999
#define eps 1e-9
int n, c;

int main() {
  static int bp[1 << 16];
  rep(i, 1 << 16) bp[i] = -1;
  while (cin >> n >> c) {
    int bit[31] = {};
    if (n == 0 && c == 0)
      break;

    int p[31][20] = {};
    rep(i, n) rep(j, 16) cin >> p[i][j];

    rep(i, c) {
      int bin = 0;
      rep(j, 16) {
        int a;
        cin >> a;
        bin |= a << j;
      }
      bit[i] = bin;
    }
    bit[c] = 0;

    //        int b[31][20]={};
    //        rep(i,c)rep(j,16)cin>>b[i][j];

    int dp[1 << 16][31] = {};
    rep(i, 1 << 16) rep(j, 31) dp[i][j] = -INF;

    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      int zero = 0;
      rep(k, 16) {
        if (p[i - 1][k] == 1) {
          zero = (zero | (1 << k));
        }
      }
      rep(j, 1 << 16) {
        if (j == zero)
          continue;
        int m = (zero | (int)j);
        dp[m][i - 1] = max(dp[m][i - 1], dp[j][i - 1]);
      }

      rep(j, 1 << 16) {
        rep(k, c + 1) {
          int x = (int)j;
          int z = 0;
          int num = j & bit[k];
          if (bp[num] == -1) {
            z = __builtin_popcount(num);
            bp[num] = z;
          } else {
            z = bp[num];
          }
          unsigned int val = ((unsigned int)j & (~(unsigned int)(bit[k])));
          if (dp[j][i - 1] != -INF)
            dp[val][i] = max(dp[val][i], dp[j][i - 1] + z);
        }
      }
    }
    int ans = -1;
    rep(i, 1 << 16) { ans = max(ans, dp[i][n]); }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 99999999
#define eps 1e-9
int n, c;

int main() {
  static int bp[1 << 16];
  rep(i, 1 << 16) bp[i] = -1;
  while (cin >> n >> c) {
    int bit[31] = {};
    if (n == 0 && c == 0)
      break;

    int p[31][20] = {};
    rep(i, n) rep(j, 16) scanf("%d", &p[i][j]);

    int b[31][20] = {};
    rep(i, c) rep(j, 16) scanf("%d", &b[i][j]);

    int dp[1 << 16][31] = {};
    rep(i, 1 << 16) rep(j, 31) dp[i][j] = -INF;

    rep(i, c) {
      rep(j, 16) {
        if (b[i][j] == 1) {
          bit[i] |= 1 << j;
        }
      }
    }
    bit[c] = 0;

    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      int zero = 0;
      rep(k, 16) { zero = (zero | ((p[i - 1][k]) << k)); }
      rep(j, 1 << 16) {
        if (j == zero)
          continue;
        int m = (zero | (int)j);
        dp[m][i - 1] = max(dp[m][i - 1], dp[j][i - 1]);
      }

      rep(j, 1 << 16) {
        rep(k, c + 1) {
          int x = (int)j;
          int z = 0;
          int num = j & bit[k];
          if (bp[num] == -1) {
            z = __builtin_popcount(num);
            bp[num] = z;
          } else {
            z = bp[num];
          }
          //                    cout<<(x&bit[k])<<"="<<x<<" "<<bit[k]<<endl;
          unsigned int val = ((unsigned int)j & (~(unsigned int)(bit[k])));
          rep(l, 16) {
            if ((bit[k] & (1 << l)) && (x & (1 << l)) != 0) {
              x -= (1 << l);
            }
          }
          if (val != x) {
            cout << j << " " << (bit[k]) << " " << ~(unsigned int)(bit[k])
                 << " " << val << " " << x << endl;
          }
          if (dp[j][i - 1] != -INF)
            dp[x][i] = max(dp[x][i], dp[j][i - 1] + z);
        }
      }
    }
    int ans = -1;
    rep(i, 1 << 16) { ans = max(ans, dp[i][n]); }
    cout << ans << endl;
  }
}