Problem p02901 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>

#define REP(i, x) for (int i{0}; i < (int)(x); i++)
#define REPC(i, x) for (int i{0}; i <= (int)(x); i++)
#define RREP(i, x) for (int i{(int)(x)-1}; i >= 0; i--)
#define RREPC(i, x) for (int i{(int)(x)}; i >= 0; i--)
#define REP1O(i, x) for (int i{1}; i < (int)(x); i++)
#define REP1C(i, x) for (int i{1}; i <= (int)(x); i++)
#define REPIT(i, x) for (auto i{(x).begin()}; i != (x).end(); i++)

#define PB push_back
#define MP make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

using namespace std;

typedef int64_t ll;
typedef double dbl;
typedef vector<bool> Vb;
typedef vector<char> Vc;
typedef vector<double> Vd;
typedef vector<int> Vi;
typedef vector<ll> Vl;
typedef vector<string> Vs;
typedef vector<vector<int>> VVi;
typedef vector<vector<ll>> VVl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
inline void swap(ll &a, ll &b) {
  a ^= b;
  b ^= a;
  a ^= b;
}
inline void swap(int &a, int &b) {
  a ^= b;
  b ^= a;
  a ^= b;
}
inline void YES() { cout << "YES" << endl; }
inline void Yes() { cout << "Yes" << endl; }
inline void NO() { cout << "NO" << endl; }
inline void No() { cout << "No" << endl; }

const int inf = 2e8;
const ll linf = 1LL << 60;
const int MOD = 1000000007;

int n, m;
Vi va, open;

void init() {
  cin >> n >> m;
  va.resize(m);
  open.resize(m);

  REP(i, m) {
    int a, b;
    cin >> a >> b;
    va[i] = a;
    int open_mask = 0;

    REP(_i, b) {
      int chest;
      cin >> chest;
      open_mask |= 1 << chest - 1;
    }

    open[i] = open_mask;
  }
}

int main() {
  init();

  Vi dp(1 << n, inf);
  dp[0] = 0;

  REP(i, m)
  chmin(dp[open[i]], va[i]);

  REP(target, 1 << n) {
    REP(i, target - 1)
    for (int j = i + 1; j < target; ++j) {
      if ((i | j) == target)
        chmin(dp[target], dp[i] + dp[j]);
    }
  }

  if (dp[(1 << n) - 1] == inf)
    cout << -1 << endl;
  else
    cout << dp[(1 << n) - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>

#define REP(i, x) for (int i{0}; i < (int)(x); i++)
#define REPC(i, x) for (int i{0}; i <= (int)(x); i++)
#define RREP(i, x) for (int i{(int)(x)-1}; i >= 0; i--)
#define RREPC(i, x) for (int i{(int)(x)}; i >= 0; i--)
#define REP1O(i, x) for (int i{1}; i < (int)(x); i++)
#define REP1C(i, x) for (int i{1}; i <= (int)(x); i++)
#define REPIT(i, x) for (auto i{(x).begin()}; i != (x).end(); i++)

#define PB push_back
#define MP make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

using namespace std;

typedef int64_t ll;
typedef double dbl;
typedef vector<bool> Vb;
typedef vector<char> Vc;
typedef vector<double> Vd;
typedef vector<int> Vi;
typedef vector<ll> Vl;
typedef vector<string> Vs;
typedef vector<vector<int>> VVi;
typedef vector<vector<ll>> VVl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
inline void swap(ll &a, ll &b) {
  a ^= b;
  b ^= a;
  a ^= b;
}
inline void swap(int &a, int &b) {
  a ^= b;
  b ^= a;
  a ^= b;
}
inline void YES() { cout << "YES" << endl; }
inline void Yes() { cout << "Yes" << endl; }
inline void NO() { cout << "NO" << endl; }
inline void No() { cout << "No" << endl; }

const int inf = 2e8;
const ll linf = 1LL << 60;
const int MOD = 1000000007;

int n, m;
Vi va, open;

void init() {
  cin >> n >> m;
  va.resize(m);
  open.resize(m);

  REP(i, m) {
    int a, b;
    cin >> a >> b;
    va[i] = a;
    int open_mask = 0;

    REP(_i, b) {
      int chest;
      cin >> chest;
      open_mask |= 1 << chest - 1;
    }

    open[i] = open_mask;
  }
}

int main() {
  init();

  Vi dp(1 << n, inf);
  dp[0] = 0;

  REP(i, m)
  chmin(dp[open[i]], va[i]);

  int target = (1 << n) - 1;

  REP(i, target) {
    for (int j = i + 1; j <= target; ++j)
      chmin(dp[i | j], dp[i] + dp[j]);
  }

  if (dp[(1 << n) - 1] == inf)
    cout << -1 << endl;
  else
    cout << dp[(1 << n) - 1] << endl;
}