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

#include <bits/stdc++.h>
using namespace std;
/* I can't know bit!!! >< */
int n, c, a[31][16], b[31][16];
int dp[31][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2];

int main() {
  while (cin >> n >> c, n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < 16; j++) {
        cin >> b[i][j];
      }
    }
#define R(i, n) for (int i = 0; i < n; i++)
#define get(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)                    \\
  [a][b][c][d][e][f][g][h][i][j][k][l][m][n][o][p]

    memset(dp, -1, sizeof(dp));
    dp[0] get(a[0][0], a[0][1], a[0][2], a[0][3], a[0][4], a[0][5], a[0][6],
              a[0][7], a[0][8], a[0][9], a[0][10], a[0][11], a[0][12], a[0][13],
              a[0][14], a[0][15]) = 0;
    for (int i = 0; i < n; i++) {
      R(A, 2)
      R(B, 2) R(C, 2) R(D, 2) R(E, 2) R(F, 2) R(G, 2) R(H, 2) R(I, 2) R(J, 2)
          R(K, 2) R(L, 2) R(M, 2) R(N, 2) R(O, 2) R(P, 2) {
        if (dp[i] get(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P) == -1)
          continue;
        for (int j = 0; j < c; j++) {
          dp[i + 1] get(
              (A & ~b[j][0]) | a[i + 1][0], (B & ~b[j][1]) | a[i + 1][1],
              (C & ~b[j][2]) | a[i + 1][2], (D & ~b[j][3]) | a[i + 1][3],
              (E & ~b[j][4]) | a[i + 1][4], (F & ~b[j][5]) | a[i + 1][5],
              (G & ~b[j][6]) | a[i + 1][6], (H & ~b[j][7]) | a[i + 1][7],
              (I & ~b[j][8]) | a[i + 1][8], (J & ~b[j][9]) | a[i + 1][9],
              (K & ~b[j][10]) | a[i + 1][10], (L & ~b[j][11]) | a[i + 1][11],
              (M & ~b[j][12]) | a[i + 1][12], (N & ~b[j][13]) | a[i + 1][13],
              (O & ~b[j][14]) | a[i + 1][14], (P & ~b[j][15]) | a[i + 1][15]) =
              max(dp[i + 1] get((A & ~b[j][0]) | a[i + 1][0],
                                (B & ~b[j][1]) | a[i + 1][1],
                                (C & ~b[j][2]) | a[i + 1][2],
                                (D & ~b[j][3]) | a[i + 1][3],
                                (E & ~b[j][4]) | a[i + 1][4],
                                (F & ~b[j][5]) | a[i + 1][5],
                                (G & ~b[j][6]) | a[i + 1][6],
                                (H & ~b[j][7]) | a[i + 1][7],
                                (I & ~b[j][8]) | a[i + 1][8],
                                (J & ~b[j][9]) | a[i + 1][9],
                                (K & ~b[j][10]) | a[i + 1][10],
                                (L & ~b[j][11]) | a[i + 1][11],
                                (M & ~b[j][12]) | a[i + 1][12],
                                (N & ~b[j][13]) | a[i + 1][13],
                                (O & ~b[j][14]) | a[i + 1][14],
                                (P & ~b[j][15]) | a[i + 1][15]),
                  dp[i] get(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P) +
                      (A & b[j][0]) + (B & b[j][1]) + (C & b[j][2]) +
                      (D & b[j][3]) + (E & b[j][4]) + (F & b[j][5]) +
                      (G & b[j][6]) + (H & b[j][7]) + (I & b[j][8]) +
                      (J & b[j][9]) + (K & b[j][10]) + (L & b[j][11]) +
                      (M & b[j][12]) + (N & b[j][13]) + (O & b[j][14]) +
                      (P & b[j][15]));
        }
      }
    }
    int ret = 0;
    R(A, 2)
    R(B, 2) R(C, 2) R(D, 2) R(E, 2) R(F, 2) R(G, 2) R(H, 2) R(I, 2) R(J, 2)
        R(K, 2) R(L, 2) R(M, 2) R(N, 2) R(O, 2) R(P, 2) {
      ret = max(ret, dp[n] get(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P));
    }
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, c, a[30], b[30];
  while (cin >> n >> c, n) {
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      for (int j = 0; j < 16; j++) {
        int aa;
        cin >> aa;
        if (aa)
          a[i] |= 1 << j;
      }
    }
    for (int i = 0; i < c; i++) {
      b[i] = 0;
      for (int j = 0; j < 16; j++) {
        int bb;
        cin >> bb;
        if (bb)
          b[i] |= 1 << j;
      }
    }

    typedef pair<int, int> Pi;
    typedef pair<int, Pi> Pii;
    queue<Pii> que;
    que.push(Pii(0, Pi(0, 0)));
    map<int, int> used[30];
    used[0][0] = 0;
    int ret = 0;
    while (!que.empty()) {
      Pii p = que.front();
      que.pop();
      int bit = p.second.first, now = p.second.second, cost = p.first;
      if (now == n)
        continue;
      if (used[now][bit] > cost)
        continue;
      bit |= a[now];
      for (int i = 0; i < c; i++) {
        int next_cost = cost + __builtin_popcount(bit & b[i]);
        if (next_cost <= used[now + 1][bit & ~b[i]])
          continue;
        ret = max(next_cost, ret);
        used[now + 1][bit & ~b[i]] = next_cost;
        que.push(Pii(next_cost, Pi(bit & ~b[i], now + 1)));
      }
    }
    cout << ret << endl;
  }
}