Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
typedef std::pair<int, int> P;
struct edge {
  int to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define MAX(X, Y) (*max_element(X, Y))
#define MIN(X, Y) (*min_element(X, Y))
#define NP(X, Y) next_permutation(X, Y)
#define setp(X, Y) cout << fixed << setprecision(Y) << X;
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int N, M;
int A[13], B[13];
int C[13][13];
set<int> U;
int dp[1005][4097];

signed main() {
  cin >> N >> M;
  REP(i, M) {
    cin >> A[i] >> B[i];
    REP(j, B[i]) {
      cin >> C[i][j];
      U.insert(C[i][j]);
    }
  }
  if (U.size() != N) {
    cout << -1 << endl;
    return 0;
  }
  REP(i, 1005) REP(j, 4097) { dp[i][j] = INF; }
  REP(i, 4097) { dp[0][0] = 0; }
  int b = 0;
  REP(i, B[0]) { b |= (1 << (C[0][i] - 1)); }
  dp[0][b] = A[0];
  for (int i = 1; i < M; i++) {
    b = 0;
    REP(j, B[i]) { b |= (1 << (C[i][j] - 1)); }
    DEB(b) END for (int j = 0; j <= 4096; j++) {
      if (dp[i - 1][j] != INF) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j]);
        dp[i][j | b] = min(dp[i - 1][j] + A[i], dp[i][j | b]);
        if (j | b == 15) {
          DEB(dp[i - 1][13]) DEB(dp[i][15]) DEB((13 | 14)) END
        }
      }
    }
    DEB(dp[i - 1][13]) DEB(dp[i][15]) DEB((13 | 14)) END
  }
  b = 0;
  REP(i, N) { b |= (1 << i); }
  cout << dp[M - 1][b] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
typedef std::pair<int, int> P;
struct edge {
  int to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define MAX(X, Y) (*max_element(X, Y))
#define MIN(X, Y) (*min_element(X, Y))
#define NP(X, Y) next_permutation(X, Y)
#define setp(X, Y) cout << fixed << setprecision(Y) << X;
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int N, M;
int A[1005], B[1005];
int C[1005][13];
set<int> U;
int dp[1005][4097];

signed main() {
  cin >> N >> M;
  REP(i, M) {
    cin >> A[i] >> B[i];
    REP(j, B[i]) {
      cin >> C[i][j];
      U.insert(C[i][j]);
    }
  }
  if (U.size() != N) {
    cout << -1 << endl;
    return 0;
  }
  REP(i, 1005) REP(j, 4097) { dp[i][j] = INF; }
  REP(i, 4097) { dp[0][0] = 0; }
  int b = 0;
  REP(i, B[0]) { b |= (1 << (C[0][i] - 1)); }
  dp[0][b] = A[0];
  for (int i = 1; i < M; i++) {
    b = 0;
    REP(j, B[i]) { b |= (1 << (C[i][j] - 1)); }
    DEB(b) END for (int j = 0; j <= 4096; j++) {
      if (dp[i - 1][j] != INF) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j]);
        dp[i][j | b] = min(dp[i - 1][j] + A[i], dp[i][j | b]);
        if (j | b == 15) {
          DEB(dp[i - 1][13]) DEB(dp[i][15]) DEB((13 | 14)) END
        }
      }
    }
    DEB(dp[i - 1][13]) DEB(dp[i][15]) DEB((13 | 14)) END
  }
  b = 0;
  REP(i, N) { b |= (1 << i); }
  cout << dp[M - 1][b] << endl;
  return 0;
}