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

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, C;
int a[32], b[32][16], p[32];
int dp[1 << 16][32];

int rec(int S, int n) {
  if (n == N)
    return 0;
  if (dp[S][n] >= 0)
    return dp[S][n];
  //  printf("%x %d\\n", S,n);

  int tmp = S | a[n];
  int res = 0;
  rep(i, C) {
    int cb = tmp & p[i];
    int c = __builtin_popcount(cb);
    res = max(res, rec(tmp - cb, n + 1) + c);
  }
  return dp[S][n] = res;
  //  return res;
}

int main() {
  while (scanf("%d%d", &N, &C) && N + C) {
    memset(dp, -1, sizeof(dp));
    memset(p, 0, sizeof(p));
    memset(a, 0, sizeof(a));
    int in;
    rep(i, N) {
      rep(j, 16) {
        scanf("%d", &in);
        a[i] |= (in << j);
      }
    }
    rep(i, C) {
      rep(j, 16) scanf("%d", &b[i][j]);
      int tmp = 0;
      rep(j, 16) p[i] |= (b[i][j] << j);
      //      printf("%x\\n", p[i]);
    }

    printf("%d\\n", rec(0, 0));
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, C;
int a[32], b[32][16], p[32];
int dp[1 << 16][32];

int rec(int S, int n) {
  if (n == N)
    return 0;
  //  if(dp[S][n] >= 0) return dp[S][n];

  S |= a[n];
  int res = 0;
  rep(i, C) {
    int cb = S & p[i];
    int c = __builtin_popcount(cb);
    res = max(res, rec(S - cb, n + 1) + c);
  }
  //  return dp[S][n] = res;
  return res;
}

int main() {
  while (scanf("%d%d", &N, &C) && N + C) {
    memset(dp, -1, sizeof(dp));
    memset(p, 0, sizeof(p));
    memset(a, 0, sizeof(a));
    int in;
    rep(i, N) {
      rep(j, 16) {
        scanf("%d", &in);
        a[i] |= (in << j);
      }
    }
    rep(i, C) {
      rep(j, 16) scanf("%d", &b[i][j]);
      int tmp = 0;
      rep(j, 16) p[i] |= (b[i][j] << j);
      //      printf("%x\\n", p[i]);
    }

    printf("%d\\n", rec(0, 0));
  }
  return 0;
}