Problem p00258 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

/* struct P {
        int x, y, n;
        P(int n, int x, int y):n(n), x(x), y(y){}
        P(){}
}; */

/** Problem0263 : Beat Panel **/
int N, C;
vector<int> ptn;
vector<int> push;

int dp[31][1 << 16];

int solve(int n, int state) {
  if (n == N)
    return 0;
  if (dp[n][state] >= 0)
    return dp[n][state];

  int res = 0;
  for (int i = 0; i < C; i++) {
    res = max(res, solve(n + 1, ((state ^ push[i]) & state) | ptn[n + 1]) +
                       __builtin_popcount(state & push[i]));
  }

  return dp[n][state] = res;
}

int main() {
  while (cin >> N >> C, N) {
    ptn.clear();
    push.clear();
    memset(dp, -1, sizeof(dp));

    rep(i, N) {
      int tmp = 0;
      rep(j, 16) {
        int t;
        cin >> t;
        tmp <<= 1;
        tmp |= t;
      }
      ptn.push_back(tmp);
    }

    rep(i, C) {
      int tmp = 0;
      rep(j, 16) {
        int t;
        cin >> t;
        tmp <<= 1;
        tmp |= t;
      }
      push.push_back(tmp);
    }

    cout << solve(0, ptn[0]) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) cerr << #x << "=" << static_cast<bitset<16>>(x) << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

const int INF = 10000000;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;

/** Problem0263 : Beat Panel **/
typedef struct State {
  int panel;
  int i;
  int sum;
  int push;
} state;

int count(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 main() {
  int n, cn;
  queue<state> Q;

  while (cin >> n >> cn, n || cn) {
    vector<int> button, ptn;
    int tmp, in;
    rep(i, n) {
      tmp = 0;
      rep(j, 16) {
        cin >> in;
        tmp = tmp << 1;
        if (in)
          tmp += 1;
      }
      button.push_back(tmp);
    }
    rep(i, cn) {
      tmp = 0;
      rep(j, 16) {
        cin >> in;
        tmp = tmp << 1;
        if (in)
          tmp += 1;
      }
      ptn.push_back(tmp);
    }

    rep(i, cn) {
      State first = {0, 0, 0, i};
      Q.push(first);
    }
    int max = 0;
    while (Q.size()) {
      State s = Q.front();
      Q.pop();
      if (s.i >= n)
        continue;
      s.panel = s.panel | button[s.i];
      s.sum += count(s.panel & ptn[s.push]);
      s.panel ^= s.panel & ptn[s.push];
      if (max < s.sum)
        max = s.sum;

      rep(j, cn) {
        State next = {s.panel, s.i + 1, s.sum, j};
        Q.push(next);
      }
    }
    cout << max << endl;
  }
  return 0;
}