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

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int gets() {
  int res = 0;
  REP(i, 16) {
    int c;
    cin >> c;
    res += c << i;
  }
  return res;
}

int main() {
  int N, C;
  while (cin >> N >> C && N) {
    vector<int> a(N);
    vector<int> b(C);
    REP(i, N) a[i] = gets();
    REP(i, C) b[i] = gets();
    int dp[31][1 << 16] = {};
    REP(i, 31) REP(j, 1 << 16) dp[i][j] = -INF;
    dp[0][0] = 0;
    int ans = 0;
    REP(i, N) REP(S, 1 << 16) if (dp[i][S] >= 0) REP(j, C) {
      int S2 = S | a[i];
      int NS = S2 & (~b[j]);
      int pt = __builtin_popcount(S2 & b[j]);
      dp[i + 1][NS] = max(dp[i + 1][NS], dp[i][S] + pt);
      // printf("dp[%d][%d] <- %d + %d\\n", i + 1, NS, dp[i][S], pt);
      ans = max(ans, dp[i + 1][NS]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int gets() {
  int res = 0;
  REP(i, 16) {
    int c;
    cin >> c;
    res += c << i;
  }
  return res;
}

int main() {
  int N, C;
  while (cin >> N >> C && N) {
    vector<int> a(N);
    vector<int> b(C);
    REP(i, N) a[i] = gets();
    REP(i, C) b[i] = gets();
    int dp[31][1 << 16] = {};
    REP(i, 31) REP(j, 1 << 16) dp[i][j] = -INF;
    dp[0][0] = 0;
    int ans = 0;
    REP(i, N) REP(j, C) REP(S, 1 << 16) {
      int S2 = S | a[i];
      int NS = S2 & (~b[j]);
      int pt = __builtin_popcount(S2 & b[j]);
      dp[i + 1][NS] = max(dp[i + 1][NS], dp[i][S] + pt);
      // printf("dp[%d][%d] <- %d + %d\\n", i + 1, NS, dp[i][S], pt);
      ans = max(ans, dp[i + 1][NS]);
    }
    cout << ans << endl;
  }
  return 0;
}