Problem p00258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)), UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a + mod - b) % mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;

int dp[31][1 << 16], a[30][16], b[30][16], d1[30], d2[30];
int po[16];

int bit_cnt(int bit) {
  int ans = 0;
  while (bit > 0) {
    ans += bit % 2;
    bit /= 2;
  }
  return ans;
}

void init() {
  fill((int *)dp, (int *)(dp + 31), -INF);
  dp[0][0] = 0;
  fill(d1, d1 + 30, 0);
  fill(d2, d2 + 30, 0);
  int tmp = 0;
  po[0] = 1;
  repi(i, 1, 16) po[i] = po[i - 1] * 2;
  sort(po, po + 16, greater<int>());
}

signed main() {
  while (1) {
    int n, c;
    scanf("%lld%lld", &n, &c);
    if (n == 0 and c == 0)
      break;
    init();
    rep(i, n) rep(j, 16) scanf("%lld", &a[i][j]);
    rep(i, c) rep(j, 16) scanf("%lld", &b[i][j]);
    rep(i, n) rep(j, 16) d1[i] += a[i][j] * po[j];
    rep(i, c) rep(j, 16) d2[i] += b[i][j] * po[j];

    rep(i, n) {
      rep(j, 1 << 16) {
        rep(k, c) {
          int bit = j | d1[i];
          int bit2 = bit & d2[k];
          int score = bit_cnt(bit2);
          bit -= bit2;
          chmax(dp[i + 1][bit], dp[i][j] + score);
        }
      }
    }

    int ans = 0;
    rep(i, 1 << 16) chmax(ans, dp[n][i]);
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)), UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a + mod - b) % mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;

int dp[31][1 << 16], a[30][16], b[30][16], d1[30], d2[30];
int po[16];

int bit_cnt(int bit) {
  int ans = 0;
  while (bit > 0) {
    ans += bit % 2;
    bit /= 2;
  }
  return ans;
}

void init() {
  fill((int *)dp, (int *)(dp + 31), -INF);
  dp[0][0] = 0;
  fill(d1, d1 + 30, 0);
  fill(d2, d2 + 30, 0);
  int tmp = 0;
  po[0] = 1;
  repi(i, 1, 16) po[i] = po[i - 1] * 2;
  sort(po, po + 16, greater<int>());
}

signed main() {
  while (1) {
    int n, c;
    scanf("%lld%lld", &n, &c);
    if (n == 0 and c == 0)
      break;
    init();
    rep(i, n) rep(j, 16) scanf("%lld", &a[i][j]);
    rep(i, c) rep(j, 16) scanf("%lld", &b[i][j]);
    rep(i, n) rep(j, 16) d1[i] += a[i][j] * po[j];
    rep(i, c) rep(j, 16) d2[i] += b[i][j] * po[j];

    rep(i, n) {
      rep(j, 1 << 16) {
        if (dp[i][j] == -INF)
          continue;
        rep(k, c) {
          int bit = j | d1[i];
          int bit2 = bit & d2[k];
          int score = bit_cnt(bit2);
          bit -= bit2;
          chmax(dp[i + 1][bit], dp[i][j] + score);
        }
      }
    }

    int ans = 0;
    rep(i, 1 << 16) chmax(ans, dp[n][i]);
    printf("%lld\n", ans);
  }
  return 0;
}