Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, N) for (int i = 0; i < (int)N; i++)
#define FORIN(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define MAX (1 << 29)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

using Pi = pair<int, int>;
using ll = long long;
const int INF = 1 << 28;

string to_string(string s) { return s; }

template <class S, class T> string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}

template <class S> string to_string(unordered_set<S> s) {
  string ret = "{";
  for (S x : s)
    ret += to_string(x) + ",";
  return ret + "}";
}

template <class T> string to_string(vector<T> v) {
  string ret = "{";
  for (int i = 0; i < v.size() - 1; ++i) {
    ret += to_string(v[i]) + ",";
  }
  if (v.size() > 0) {
    ret += to_string(v.back());
  }
  ret += "}";
  return ret;
}

void debug() { cerr << endl; }

template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}

void print() { cout << endl; }

template <class Head, class... Tail> void print(Head head, Tail... tail) {
  cout << to_string(head);
  print(tail...);
}

void get() {}

template <class Head, class... Tail> void get(Head &head, Tail &...tail) {
  cin >> head;
  get(tail...);
}

template <class T> void getv(vector<T> &vec) {
  for (int i = 0; i < vec.size(); ++i)
    cin >> vec[i];
}

unordered_set<ll> enum_div(ll n) // nの約数を列挙
{
  std::unordered_set<ll> ret;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.insert(i);
      if (i * i != n) {
        ret.insert(n / i);
      }
    }
  }
  return ret;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N);
  vector<int> dp(1 << N, 1 << 29);
  dp[0] = 0;
  FOR(i, M) {
    int b;
    cin >> a[i] >> b;
    int add = 0;
    FOR(j, b) {
      int c;
      cin >> c;
      add |= 1 << (c - 1);
    }
    FOR(j, 1 << N) { dp[j | add] = min(dp[j | add], dp[j] + a[i]); }
  }
  print(dp[(1 << N) - 1] == 1 << 29 ? -1 : dp[(1 << N) - 1]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, N) for (int i = 0; i < (int)N; i++)
#define FORIN(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define MAX (1 << 29)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

using Pi = pair<int, int>;
using ll = long long;
const int INF = 1 << 28;

string to_string(string s) { return s; }

template <class S, class T> string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}

template <class S> string to_string(unordered_set<S> s) {
  string ret = "{";
  for (S x : s)
    ret += to_string(x) + ",";
  return ret + "}";
}

template <class T> string to_string(vector<T> v) {
  string ret = "{";
  for (int i = 0; i < v.size() - 1; ++i) {
    ret += to_string(v[i]) + ",";
  }
  if (v.size() > 0) {
    ret += to_string(v.back());
  }
  ret += "}";
  return ret;
}

void debug() { cerr << endl; }

template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}

void print() { cout << endl; }

template <class Head, class... Tail> void print(Head head, Tail... tail) {
  cout << to_string(head);
  print(tail...);
}

void get() {}

template <class Head, class... Tail> void get(Head &head, Tail &...tail) {
  cin >> head;
  get(tail...);
}

template <class T> void getv(vector<T> &vec) {
  for (int i = 0; i < vec.size(); ++i)
    cin >> vec[i];
}

unordered_set<ll> enum_div(ll n) // nの約数を列挙
{
  std::unordered_set<ll> ret;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.insert(i);
      if (i * i != n) {
        ret.insert(n / i);
      }
    }
  }
  return ret;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M);
  vector<int> dp(1 << N, 1 << 29);
  dp[0] = 0;
  FOR(i, M) {
    int b;
    cin >> a[i] >> b;
    int add = 0;
    FOR(j, b) {
      int c;
      cin >> c;
      add |= 1 << (c - 1);
    }
    FOR(j, 1 << N) { dp[j | add] = min(dp[j | add], dp[j] + a[i]); }
  }
  print(dp[(1 << N) - 1] == 1 << 29 ? -1 : dp[(1 << N) - 1]);
  return 0;
}