Problem p02901 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define reprrev(i, a, b) for (int i = b - 1; i >= a; i--)
#define reprev(i, n) reprrev(i, 0, n)
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
*/

const ll mod = 1e9 + 7;

void chmod(ll &M) {
  if (M >= mod)
    M %= mod;
  else if (M < 0) {
    M += (abs(M) / mod + 1) * mod;
    M %= mod;
  }
}

ll modpow(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = modpow(x, n / 2);
  if (n % 2 == 0)
    return res * res % mod;
  else
    return res * res % mod * x % mod;
}

ll power(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = power(x, n / 2);
  if (n % 2 == 0)
    return res * res;
  else
    return res * res * x;
}

int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; };
int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; };

/* <--------------------------------------------> */

typedef tuple<ll, ll, ll> T;
// get<K>(tuple型の変数)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vector<int> a(M);
  vector<int> c(M);
  rep(i, M) {
    cin >> a[i];
    int b;
    cin >> b;
    int t = 0;
    rep(j, b) {
      int v;
      cin >> v;
      --v;
      t += 1 << (N - 1 - v);
    }
    c[i] = t;
  }

  const ll inf = 1LL << 60;
  vector<vector<ll>> dp(M + 5, vector<ll>(1 << N, inf));
  dp[0][0] = 0;

  rep(i, M) rep(S, 1 << N) {
    chmin(dp[i + 1][S], dp[i][S]);
    for (int T = c[i]; T > 0; T = (T - 1) & c[i])
      chmin(dp[i + 1][S], dp[i][S ^ T] + a[i]);
  }

  if (dp[M][(1 << N) - 1] == inf)
    cout << -1 << endl;
  else
    cout << dp[M][(1 << N) - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define reprrev(i, a, b) for (int i = b - 1; i >= a; i--)
#define reprev(i, n) reprrev(i, 0, n)
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
*/

const ll mod = 1e9 + 7;

void chmod(ll &M) {
  if (M >= mod)
    M %= mod;
  else if (M < 0) {
    M += (abs(M) / mod + 1) * mod;
    M %= mod;
  }
}

ll modpow(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = modpow(x, n / 2);
  if (n % 2 == 0)
    return res * res % mod;
  else
    return res * res % mod * x % mod;
}

ll power(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = power(x, n / 2);
  if (n % 2 == 0)
    return res * res;
  else
    return res * res * x;
}

int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; };
int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; };

/* <--------------------------------------------> */

typedef tuple<ll, ll, ll> T;
// get<K>(tuple型の変数)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vector<int> a(M);
  vector<int> c(M);
  rep(i, M) {
    cin >> a[i];
    int b;
    cin >> b;
    int t = 0;
    rep(j, b) {
      int v;
      cin >> v;
      --v;
      t += 1 << (N - 1 - v);
    }
    c[i] = t;
  }

  const ll inf = 1LL << 60;
  vector<vector<ll>> dp(M + 5, vector<ll>(1 << N, inf));
  dp[0][0] = 0;

  rep(i, M) rep(S, 1 << N) {
    chmin(dp[i + 1][S], dp[i][S]);
    if ((S | c[i]) > S)
      continue;
    for (int T = c[i]; T > 0; T = (T - 1) & c[i])
      chmin(dp[i + 1][S], dp[i][S ^ T] + a[i]);
  }

  if (dp[M][(1 << N) - 1] == inf)
    cout << -1 << endl;
  else
    cout << dp[M][(1 << N) - 1] << endl;

  return 0;
}