prithivMLmods/Coder-Stat huggingface dataset:
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int, int>;
const int Max_N = 15;
const int Max_M = 1e3 + 5;
int c[Max_M][Max_N];
const int INF = 1e9;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M);
  rep(i, M) {
    cin >> a[i] >> b[i];
    rep(j, b[i]) cin >> c[i][j];
    rep(j, b[i]) c[i][j]--;
  }
  vector<int> dp(1 << N, INF);
  dp[0] = 0;
  rep(bit, 1 << N) {
    rep(i, M) {
      int bit_next = bit;
      rep(j, b[i]) if (!(bit & 1 << j)) bit_next += 1 << c[i][j];
      dp[bit_next] = min(dp[bit_next], dp[bit] + a[i]);
    }
  }
  if (dp[(1 << N) - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[(1 << N) - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int, int>;
const int Max_N = 15;
const int Max_M = 1e3 + 5;
int c[Max_M][Max_N];
const int INF = 1e9;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M);
  rep(i, M) {
    cin >> a[i] >> b[i];
    rep(j, b[i]) cin >> c[i][j];
    rep(j, b[i]) c[i][j]--;
  }
  vector<int> dp(1 << N, INF);
  dp[0] = 0;
  rep(bit, 1 << N) {
    rep(i, M) {
      int bit_next = bit;
      rep(j, b[i]) if (!(bit & 1 << c[i][j])) bit_next += 1 << c[i][j];
      dp[bit_next] = min(dp[bit_next], dp[bit] + a[i]);
    }
  }
  if (dp[(1 << N) - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[(1 << N) - 1] << endl;

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#define N 15
#define M 1005
using namespace std;

int n, m, a[M], b[M], c[M][N], d[1 << N];

int f(int p, int q) {
  int i, j, ne, re = 1e9;
  if (p == 0)
    return 0;
  if (q == m)
    return 1e9;
  if (d[p])
    return d[p];
  for (i = q; i < m; i++) {
    ne = p;
    for (j = 0; j < b[i]; j++) {
      ne &= (1 << n) - 1 - (1 << c[i][j] - 1);
    }
    re = min(re, f(ne, i + 1) + a[i]);
  }
  return d[p] = re;
}

int main() {
  int i, j, t;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    scanf("%d %d", &a[i], &b[i]);
    for (j = 0; j < b[i]; j++) {
      scanf("%d", &c[i][j]);
    }
  }
  t = f((1 << n) - 1, 0);
  if (t == 1e9)
    puts("-1");
  else
    cout << t;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define N 15
#define M 1005
using namespace std;

int n, m, a[M], b[M], c[M][N], d[1 << N];

int f(int p, int q) {
  int i, j, ne, re = 1e9;
  if (p == 0)
    return 0;
  if (q == m)
    return 1e9;
  if (d[p])
    return d[p];
  for (i = q; i < m; i++) {
    ne = p;
    for (j = 0; j < b[i]; j++) {
      ne &= (1 << n) - 1 - (1 << c[i][j] - 1);
    }
    if (ne != p)
      re = min(re, f(ne, i + 1) + a[i]);
  }
  return d[p] = re;
}

int main() {
  int i, j, t;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    scanf("%d %d", &a[i], &b[i]);
    for (j = 0; j < b[i]; j++) {
      scanf("%d", &c[i][j]);
    }
  }
  t = f((1 << n) - 1, 0);
  if (t == 1e9)
    puts("-1");
  else
    cout << t;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 11;
const int INF = 0x3f3f3f3f;
int n, m, f[1 << MAXN];
int main() {
  cin >> n >> m;
  memset(f, 255, sizeof(f));
  f[0] = 0;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    int msk = 0;
    while (b--) {
      int t;
      cin >> t;
      msk |= (1 << (t - 1));
    }
    for (int k = 0; k < (1 << n); k++) {
      if (f[k] == -1)
        continue;
      if (f[k | msk] == -1)
        f[k | msk] = f[k] + a;
      else
        f[k | msk] = min(f[k | msk], f[k] + a);
    }
  }
  if (f[(1 << n) - 1] == INF)
    cout << -1 << endl;
  else
    cout << f[(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 15;
const int INF = 0x3f3f3f3f;
int n, m, f[1 << MAXN];
int main() {
  cin >> n >> m;
  memset(f, 255, sizeof(f));
  f[0] = 0;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    int msk = 0;
    while (b--) {
      int t;
      cin >> t;
      msk |= (1 << (t - 1));
    }
    for (int k = 0; k < (1 << n); k++) {
      if (f[k] == -1)
        continue;
      if (f[k | msk] == -1)
        f[k | msk] = f[k] + a;
      else
        f[k | msk] = min(f[k | msk], f[k] + a);
    }
  }
  if (f[(1 << n) - 1] == INF)
    cout << -1 << endl;
  else
    cout << f[(1 << n) - 1] << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
// #define int ll
using namespace std;
const int INF(1 << 30);
const ll LLINF(1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n, m, dp[1 << 14];
  ll a[100010], b[20], c[2000][30];
  cin >> n >> m;
  rep(i, m) {
    cin >> a[i] >> b[i];
    rep(j, b[i]) { cin >> c[i][j]; }
  }
  rep(i, 1 << 14) { dp[i] = LLINF; }
  dp[0] = 0;
  rep(i, m) {
    ll now = 0;
    rep(j, b[i]) { now |= 1 << (c[i][j] - 1); }
    rep(j, 1 << n) { dp[j | now] = min(dp[j | now], dp[j] + a[i]); }
  }
  if (dp[(1 << n) - 1] == LLINF)
    dp[(1 << n) - 1] = -1;
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
// #define int ll
using namespace std;
const int INF(1 << 30);
const ll LLINF(1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n, m, dp[1 << 14];
  ll a[100010], b[2000], c[2000][30];
  cin >> n >> m;
  rep(i, m) {
    cin >> a[i] >> b[i];
    rep(j, b[i]) { cin >> c[i][j]; }
  }
  rep(i, 1 << 14) { dp[i] = LLINF; }
  dp[0] = 0;
  rep(i, m) {
    ll now = 0;
    rep(j, b[i]) { now |= 1 << (c[i][j] - 1); }
    rep(j, 1 << n) { dp[j | now] = min(dp[j | now], dp[j] + a[i]); }
  }
  if (dp[(1 << n) - 1] == LLINF)
    dp[(1 << n) - 1] = -1;
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1LL << 60;
int dp[1001][1 << 12];
int a[12], b[12], c[12];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; j++) {
      int k;
      cin >> k;
      c[i] |= (1 << (k - 1));
    }
  }
  fill((int *)dp, (int *)(dp + 1001), 1 << 30);
  dp[0][0] = 0;
  for (int i = 0; i < M; i++) {
    for (int bit = 0; bit < (1 << N); bit++) {
      dp[i + 1][bit] = min(dp[i + 1][bit], dp[i][bit]);
      dp[i + 1][bit | c[i]] = min(dp[i + 1][bit | c[i]], dp[i][bit] + a[i]);
    }
  }
  if (dp[M][(1 << N) - 1] == 1 << 30)
    cout << -1 << endl;
  else
    cout << dp[M][(1 << N) - 1] << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1LL << 60;
int dp[1001][1 << 12];
int a[1000], b[1000], c[1000];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; j++) {
      int k;
      cin >> k;
      c[i] |= (1 << (k - 1));
    }
  }
  fill((int *)dp, (int *)(dp + 1001), 1 << 30);
  dp[0][0] = 0;
  for (int i = 0; i < M; i++) {
    for (int bit = 0; bit < (1 << N); bit++) {
      dp[i + 1][bit] = min(dp[i + 1][bit], dp[i][bit]);
      dp[i + 1][bit | c[i]] = min(dp[i + 1][bit | c[i]], dp[i][bit] + a[i]);
    }
  }
  if (dp[M][(1 << N) - 1] == 1 << 30)
    cout << -1 << endl;
  else
    cout << dp[M][(1 << N) - 1] << endl;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m), c(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; j++) {
      int x;
      cin >> x;
      x--;
      c[i] += (1 << x);
    }
  }
  vector<int> dp(1 << n, 1e9);
  dp[0] = 0;
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      dp[i | c[j]] = min(dp[i | c[j]], dp[i] + a[i]);
    }
  }
  if (dp[(1 << n) - 1] == 1e9) {
    cout << -1 << endl;
  } else {
    cout << dp[(1 << n) - 1] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m), c(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; j++) {
      int x;
      cin >> x;
      x--;
      c[i] += (1 << x);
    }
  }
  vector<int> dp(1 << n, 1e9);
  dp[0] = 0;
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < m; j++) {
      dp[i | c[j]] = min(dp[i | c[j]], dp[i] + a[j]);
    }
  }
  if (dp[(1 << n) - 1] == 1e9) {
    cout << -1 << endl;
  } else {
    cout << dp[(1 << n) - 1] << endl;
  }
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <utility>
#include <vector>
#define chmin(x, y) x = min(x, y);
#define chmax(x, y) x = max(x, y);
#define rep(i, j) for (int(i) = 0; (i) < (j); (i)++)
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
using namespace std;
typedef long long int ll;
const int INF = 1e8;

int N, M;
vector<pair<int, int>> key;

void input() {
  cin >> N >> M;

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    int s = 0;
    // bitで集合をとる
    rep(j, b) {
      int c;
      cin >> c;
      c--;
      s |= 1 << c;
    }
    key.emplace_back(s, a);
  }
}

void solve() {
  input();
  vector<int> dp(1 << N, INF);
  dp[0] = 0;
  rep(i, 1 << N) {
    rep(j, M) {
      int t = i | key[i].first;
      int cost = dp[i] + key[j].second;
      dp[t] = min(dp[t], cost);
    }
  }
  int ans = dp.back();
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return;
}

int main() {
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <utility>
#include <vector>
#define chmin(x, y) x = min(x, y);
#define chmax(x, y) x = max(x, y);
#define rep(i, j) for (int(i) = 0; (i) < (j); (i)++)
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
using namespace std;
typedef long long int ll;
const int INF = 1e8;

int N, M;
vector<pair<int, int>> key;

void input() {
  cin >> N >> M;

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    int s = 0;
    // bitで集合をとる
    rep(j, b) {
      int c;
      cin >> c;
      c--;
      s |= 1 << c;
    }
    key.emplace_back(s, a);
  }
}

void solve() {
  input();
  vector<int> dp(1 << N, INF);
  dp[0] = 0;
  rep(i, 1 << N) {
    rep(j, M) {
      int t = i | key[j].first;
      int cost = dp[i] + key[j].second;
      dp[t] = min(dp[t], cost);
    }
  }
  int ans = dp.back();
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return;
}

int main() {
  solve();
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < ll(n); i++)
#define ALL(v) v.begin(), v.end()
#define SIZE(x) ll(x.size())
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 2e9;
const ll LINF = (1LL << 60);

int dp[105][4100];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> a(m), key(m, 0);
  REP(i, m) {
    int b;
    cin >> a[i] >> b;
    REP(k, b) {
      int c;
      cin >> c;
      c--;
      key[i] |= (1 << c);
    }
  }

  REP(i, m + 1) REP(j, (1 << n)) dp[i][j] = INF;
  dp[0][0] = 0;
  REP(i, m) REP(j, (1 << n)) {
    chmin(dp[i + 1][j], dp[i][j]);
    chmin(dp[i + 1][j | key[i]], dp[i][j] + a[i]);
  }
  cout << (dp[m][(1 << n) - 1] == INF ? -1 : dp[m][(1 << n) - 1]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < ll(n); i++)
#define ALL(v) v.begin(), v.end()
#define SIZE(x) ll(x.size())
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 2e9;
const ll LINF = (1LL << 60);

int dp[1005][4100];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> a(m), key(m, 0);
  REP(i, m) {
    int b;
    cin >> a[i] >> b;
    REP(k, b) {
      int c;
      cin >> c;
      c--;
      key[i] |= (1 << c);
    }
  }

  REP(i, m + 1) REP(j, (1 << n)) dp[i][j] = INF;
  dp[0][0] = 0;
  REP(i, m) REP(j, (1 << n)) {
    chmin(dp[i + 1][j], dp[i][j]);
    chmin(dp[i + 1][j | key[i]], dp[i][j] + a[i]);
  }
  cout << (dp[m][(1 << n) - 1] == INF ? -1 : dp[m][(1 << n) - 1]) << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (LL)(1e18) + 1;
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

int N, M;
vector<LL> a, b;
vector<int> c; // どの宝箱が開錠可能かを表す

LL ans = 0;

void solve() {
  // dp[i][j]:i番目の鍵まで見たとき、
  // bit列jで表されるような宝箱を開けるときの
  // 最小費用
  const int opened_all = (1 << N) - 1; // 全部開けた時を表すビット列
  vector<vector<LL>> dp(M + 1, vector<LL>(opened_all + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j <= opened_all; j++) {
      // i番目の鍵を購入する場合
      dp[i + 1][j | c[i]] = min(dp[i + 1][j | c[i]], dp[i][j] + a[i]);
      // しない場合
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  ans = dp[N][opened_all];
  if (ans == INF)
    ans = -1;
}

int main() {
  cin >> N >> M;
  a = vector<LL>(M);
  b = vector<LL>(M);
  c = vector<int>(M, 0);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; j++) {
      LL c_input;
      cin >> c_input;
      c[i] |= (1 << (c_input - 1));
    }
  }

  solve();

  cout << ans << endl;
  return 0;
}

Fixed submission:
// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (LL)(1e18) + 1;
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

int N, M;
vector<LL> a, b;
vector<int> c; // どの宝箱が開錠可能かを表す

LL ans = 0;

void solve() {
  // dp[i][j]:i番目の鍵まで見たとき、
  // bit列jで表されるような宝箱を開けるときの
  // 最小費用
  const int opened_all = (1 << N) - 1; // 全部開けた時を表すビット列
  vector<vector<LL>> dp(M + 1, vector<LL>(opened_all + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j <= opened_all; j++) {
      // i番目の鍵を購入する場合
      dp[i + 1][j | c[i]] = min(dp[i + 1][j | c[i]], dp[i][j] + a[i]);
      // しない場合
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  ans = dp[M][opened_all];
  if (ans == INF)
    ans = -1;
}

int main() {
  cin >> N >> M;
  a = vector<LL>(M);
  b = vector<LL>(M);
  c = vector<int>(M, 0);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; j++) {
      LL c_input;
      cin >> c_input;
      c[i] |= (1 << (c_input - 1));
    }
  }

  solve();

  cout << ans << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false),                        \
      cout << fixed << setprecision(20);
#define int long long
#define gcd __gcd
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; }
template <class T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; }
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for (auto &&i : n)
const int INF = 1e18, MOD = 1e9 + 7;
signed main() {
  INCANT;
  int n, m, a, b, c, sesame, dp[5555];
  rep(i, 5555) dp[i] = INF;
  dp[0] = 0;
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b;
    sesame = 0;
    while (b--) {
      cin >> c;
      c--;
      sesame += (int)pow(2, c);
    }
    rep(i, 5000) { chmin(dp[i | sesame], dp[i] + a); }
  }
  cout << (dp[(int)pow(2, n) - 1] == INF ? -1 : dp[(int)pow(2, n) - 1]) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false),                        \
      cout << fixed << setprecision(20);
#define int long long
#define gcd __gcd
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; }
template <class T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; }
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for (auto &&i : n)
const int INF = 1e18, MOD = 1e9 + 7;
signed main() {
  INCANT;
  int n, m, a, b, c, sesame, dp[5555];
  rep(i, 5555) dp[i] = INF;
  dp[0] = 0;
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b;
    sesame = 0;
    while (b--) {
      cin >> c;
      c--;
      sesame += (int)pow(2, c);
    }
    rep(i, (int)pow(2, n)) { chmin(dp[i | sesame], dp[i] + a); }
  }
  cout << (dp[(int)pow(2, n) - 1] == INF ? -1 : dp[(int)pow(2, n) - 1]) << endl;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
const int INF = 1001001001;

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> key;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    int s;
    rep(j, b) {
      int c;
      cin >> c;
      c--;
      s |= 1 << c;
    }
    key.emplace_back(s, a); // push_back（要素の追加）の上位互換
  }
  // bit演算子でｃを管理
  vector<int> dp(1 << N, INF); // 2の12乗は4096. dp=コストの最小値。
  dp[0] = 0;
  rep(s, 1 << N) {
    rep(i, M) {
      int t = s | key[i].first;
      int cost = dp[s] + key[i].second;
      dp[t] = min(dp[t], cost);
    }
  }
  int ans = dp.back();
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
const int INF = 1001001001;

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> key;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    int s = 0;
    rep(j, b) {
      int c;
      cin >> c;
      c--;
      s |= 1 << c;
    }
    key.emplace_back(s, a); // push_back（要素の追加）の上位互換
  }
  // bit演算子でｃを管理
  vector<int> dp(1 << N, INF); // 2の12乗は4096. dp=コストの最小値。
  dp[0] = 0;
  rep(s, 1 << N) {
    rep(i, M) {
      int t = s | key[i].first;
      int cost = dp[s] + key[i].second;
      dp[t] = min(dp[t], cost);
    }
  }
  int ans = dp.back();
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define loop(i, n) rep(i, 0, n)
using namespace std;
using ll = long long;
// dp[i][j]={鍵1,...,iを用いて宝箱j(bitsetで管理)を開けるための費用の最小値}
constexpr int n_max = 12, m_max = 100;
int dp[m_max + 1][1 << n_max];
constexpr int infty = 1e9;
int main() {
  int n, m;
  cin >> n >> m;
  loop(i, 1 + m) {
    loop(j, 1 << n) { dp[i][j] = infty; }
  }
  dp[0][0] = 0;
  rep(i, 1, 1 + m) {
    int a, b, key = 0;
    cin >> a >> b;
    loop(j, b) {
      int c;
      cin >> c;
      key |= (1 << (c - 1));
    }
    loop(j, 1 << n) dp[i][j] = dp[i - 1][j];
    loop(j, 1 << n) {
      dp[i][j | key] =
          min(dp[i][j | key], min(dp[i - 1][j | key], dp[i - 1][j] + a));
    }
  }
  if (dp[m][(1 << n) - 1] == infty)
    cout << -1 << endl;
  else
    cout << dp[m][(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define loop(i, n) rep(i, 0, n)
using namespace std;
using ll = long long;
// dp[i][j]={鍵1,...,iを用いて宝箱j(bitsetで管理)を開けるための費用の最小値}
constexpr int n_max = 12, m_max = 1000;
int dp[m_max + 1][1 << n_max];
constexpr int infty = 1e9;
int main() {
  int n, m;
  cin >> n >> m;
  loop(i, 1 + m) {
    loop(j, 1 << n) { dp[i][j] = infty; }
  }
  dp[0][0] = 0;
  rep(i, 1, 1 + m) {
    int a, b, key = 0;
    cin >> a >> b;
    loop(j, b) {
      int c;
      cin >> c;
      key |= (1 << (c - 1));
    }
    loop(j, 1 << n) dp[i][j] = dp[i - 1][j];
    loop(j, 1 << n) {
      dp[i][j | key] =
          min(dp[i][j | key], min(dp[i - 1][j | key], dp[i - 1][j] + a));
    }
  }
  if (dp[m][(1 << n) - 1] == infty)
    cout << -1 << endl;
  else
    cout << dp[m][(1 << n) - 1] << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
#define ANS_MAX 1000000000
#define M_MAX 1000
#define N_MAX 12
using namespace std;

long dp[M_MAX][1 << N_MAX];
long price[M_MAX];
vector<vector<int>> keys(M_MAX);
int n, m;

long caldp(int k, int close) {
  if (k == m)
    return close == 0 ? 0 : ANS_MAX;
  if (dp[k][close] > 0)
    return dp[k][close];
  long ret = caldp(k + 1, close);
  int nextClose = close;
  for (int key : keys[k]) {
    if ((close & (1 << key)) != 0)
      nextClose -= (1 << key);
  }
  return dp[k][close] = min(ret, caldp(k + 1, nextClose) + price[k]);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int b;
    int key;
    cin >> price[i] >> b;
    for (int j = 0; j < b; j++) {
      cin >> key;
      keys[i].push_back(key - 1);
    }
  }
  long ans = caldp(0, (1 << n) - 1);
  ans = ans < ANS_MAX ? ans : -1;
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
#define ANS_MAX 1000000000
#define M_MAX 1000
#define N_MAX 12
using namespace std;

long dp[M_MAX][1 << N_MAX];
long price[M_MAX];
vector<vector<int>> keys(M_MAX);
int n, m;

long caldp(int k, int close) {
  if (k == m)
    return close == 0 ? 0 : ANS_MAX;
  if (close == 0)
    return dp[k][close] = 0;
  if (dp[k][close] > 0)
    return dp[k][close];
  long ret = caldp(k + 1, close);
  int nextClose = close;
  for (int key : keys[k]) {
    if ((close & (1 << key)) != 0)
      nextClose -= (1 << key);
  }
  return dp[k][close] = min(ret, caldp(k + 1, nextClose) + price[k]);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int b;
    int key;
    cin >> price[i] >> b;
    for (int j = 0; j < b; j++) {
      cin >> key;
      keys[i].push_back(key - 1);
    }
  }
  long ans = caldp(0, (1 << n) - 1);
  ans = ans < ANS_MAX ? ans : -1;
  cout << ans << endl;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int a[100], mask[100];
int n, m, b, dp[100][1 << 12];

int f(int k, int m) {
  if (k == ::m) {
    if (m == (1 << n) - 1)
      return 0;
    return 1e9;
  }
  if (dp[k][m] != -1)
    return dp[k][m];
  return dp[k][m] = min(f(k + 1, m), a[k] + f(k + 1, m | mask[k]));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b;
    for (int j = 0; j < b; ++j) {
      int c;
      cin >> c;
      mask[i] |= (1 << (c - 1));
    }
  }
  memset(dp, -1, sizeof dp);
  if (f(0, 0) >= 1e9)
    cout << -1;
  else
    cout << dp[0][0];
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int a[1000], mask[1000];
int n, m, b, dp[1000][1 << 12];

int f(int k, int m) {
  if (k == ::m) {
    if (m == (1 << n) - 1)
      return 0;
    return 1e9;
  }
  if (dp[k][m] != -1)
    return dp[k][m];
  return dp[k][m] = min(f(k + 1, m), a[k] + f(k + 1, m | mask[k]));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b;
    for (int j = 0; j < b; ++j) {
      int c;
      cin >> c;
      mask[i] |= (1 << (c - 1));
    }
  }
  memset(dp, -1, sizeof dp);
  if (f(0, 0) >= 1e9)
    cout << -1;
  else
    cout << dp[0][0];
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef long long ll;

using namespace std;

int main(void) {
  ll N, M;
  ll key[10004], cost[10004];
  ll dp[15][10000];
  ll a, b, c;
  cin >> N >> M;
  ll goal = pow(2, N) - 1;
  MEMSET(key, 0);
  MEMSET(cost, 0);
  rep(i, M) {
    cin >> a >> b;
    cost[i] = a;
    rep(j, b) {
      cin >> c;
      key[i] += pow(2, c - 1);

    } // cout<<key[i]<<endl;
  }
  rep(i, M + 1) rep(j, goal + 1) dp[i][j] = 10000000001;
  dp[0][0] = 0;
  rep(i, M) rep(j, goal + 1) {
    dp[i + 1][j | key[i]] = min(dp[i + 1][j | key[i]], dp[i][j] + cost[i]);
    dp[i + 1][j] = min(dp[i][j], dp[i + 1][j]);

    /*rep(k, M+1){
        rep(l, goal+1) cout<<dp[k][l]<<" ";
        cout<<endl;
    }
    cout<<endl;*/
  }
  cout << ((dp[M][goal] > 10000000000) ? (ll)-1 : dp[M][goal]) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef long long ll;

using namespace std;

int main(void) {
  ll N, M;
  ll key[10004], cost[10004];
  ll dp[1024][10000];
  ll a, b, c;
  cin >> N >> M;
  ll goal = pow(2, N) - 1;
  MEMSET(key, 0);
  MEMSET(cost, 0);
  rep(i, M) {
    cin >> a >> b;
    cost[i] = a;
    rep(j, b) {
      cin >> c;
      key[i] += pow(2, c - 1);

    } // cout<<key[i]<<endl;
  }
  rep(i, M + 1) rep(j, goal + 1) dp[i][j] = 10000000001;
  dp[0][0] = 0;
  rep(i, M) rep(j, goal + 1) {
    dp[i + 1][j | key[i]] = min(dp[i + 1][j | key[i]], dp[i][j] + cost[i]);
    dp[i + 1][j] = min(dp[i][j], dp[i + 1][j]);

    /*rep(k, M+1){
        rep(l, goal+1) cout<<dp[k][l]<<" ";
        cout<<endl;
    }
    cout<<endl;*/
  }
  cout << ((dp[M][goal] > 10000000000) ? (ll)-1 : dp[M][goal]) << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
#define PI 3.14159265358979
#define MOD 1000000007 // = 10^9 + 7

using namespace std;
using ll = long long;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  int c[1007];
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; ++j) {
      int input;
      cin >> input;
      input--;
      c[i] += (1 << input);
    }
  }

  const int INF = 1000000000;
  vector<int> dp(5000, INF);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    int now = c[i];
    for (int mask = 0; mask < (1 << n); mask++) {
      dp[mask | now] = min(dp[mask | now], dp[mask] + a[i]);
    }
  }
  cout << (dp[(1 << n) - 1] == INF ? -1 : dp[(1 << n) - 1]);
}

int main() {
  fastio;
  solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
#define PI 3.14159265358979
#define MOD 1000000007 // = 10^9 + 7

using namespace std;
using ll = long long;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  vector<int> c(m);
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; ++j) {
      int input;
      cin >> input;
      input--;
      c[i] += (1 << input);
    }
  }

  const int INF = 1000000000;
  vector<int> dp(5000, INF);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    int now = c[i];
    for (int mask = 0; mask < (1 << n); mask++) {
      dp[mask | now] = min(dp[mask | now], dp[mask] + a[i]);
    }
  }
  cout << (dp[(1 << n) - 1] == INF ? -1 : dp[(1 << n) - 1]);
}

int main() {
  fastio;
  solve();

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
// header {{{
#include <bits/stdc++.h>
using namespace std;

// {U}{INT,LONG,LLONG}_{MAX,MIN}
#define INF INT_MAX / 3
#define LLINF LLONG_MAX / 3
#define MOD (1000000007LL)
#define MODA(a, b) a = ((a) + (b)) % MOD
#define MODP(a, b) a = ((a) * (b)) % MOD
#define inc(i, l, r) for (int i = (l); i < (r); i++)
#define dec(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define pb push_back
#define se second
#define fi first
#define mset(a, b) memset(a, b, sizeof(a))

using LL = long long;
using G = vector<vector<int>>;

int di[] = {0, -1, 0, 1};
int dj[] = {1, 0, -1, 0};
// }}}

int dp[1005][1 << 12];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n), k(n, 0);
  inc(i, 0, m) {
    cin >> a[i] >> b[i];
    inc(j, 0, b[i]) {
      int c;
      cin >> c;
      c--;
      k[i] |= (1 << c);
    }
  }
  int inf = 1000000000;
  inc(i, 0, m + 1) inc(j, 0, 1 << n) dp[i][j] = inf;
  dp[0][0] = 0;
  inc(i, 0, m) {
    inc(j, 0, 1 << 12) {
      if (dp[i][j] != inf) {
        dp[i + 1][j | k[i]] = min(dp[i + 1][j | k[i]], dp[i][j] + a[i]);
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      }
    }
  }
  // inc(i, 0, m+1){
  // inc(j, 0, 1<<n){
  // cout << dp[i][j] << " ";
  //}
  // cout << endl;
  //}
  int ans = dp[m][(1 << n) - 1];
  if (ans == inf) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
// header {{{
#include <bits/stdc++.h>
using namespace std;

// {U}{INT,LONG,LLONG}_{MAX,MIN}
#define INF INT_MAX / 3
#define LLINF LLONG_MAX / 3
#define MOD (1000000007LL)
#define MODA(a, b) a = ((a) + (b)) % MOD
#define MODP(a, b) a = ((a) * (b)) % MOD
#define inc(i, l, r) for (int i = (l); i < (r); i++)
#define dec(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define pb push_back
#define se second
#define fi first
#define mset(a, b) memset(a, b, sizeof(a))

using LL = long long;
using G = vector<vector<int>>;

int di[] = {0, -1, 0, 1};
int dj[] = {1, 0, -1, 0};
// }}}

int dp[1005][1 << 12];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m), k(m, 0);
  inc(i, 0, m) {
    cin >> a[i] >> b[i];
    inc(j, 0, b[i]) {
      int c;
      cin >> c;
      c--;
      k[i] |= (1 << c);
    }
  }
  int inf = 1000000000;
  inc(i, 0, m + 1) inc(j, 0, 1 << n) dp[i][j] = inf;
  dp[0][0] = 0;
  inc(i, 0, m) {
    inc(j, 0, 1 << 12) {
      if (dp[i][j] != inf) {
        dp[i + 1][j | k[i]] = min(dp[i + 1][j | k[i]], dp[i][j] + a[i]);
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      }
    }
  }
  // inc(i, 0, m+1){
  // inc(j, 0, 1<<n){
  // cout << dp[i][j] << " ";
  //}
  // cout << endl;
  //}
  int ans = dp[m][(1 << n) - 1];
  if (ans == inf) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

using namespace std;
vector<vector<int>> dp(1001, vector<int>(2048, 1e9));

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  dp[0][0] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    int num = 0;
    rep(j, b) {
      int t;
      cin >> t;
      num += 1 << (t - 1);
    }
    //  cout<<"num"<<num<<endl;
    rep(j, 1 << n) { dp[i + 1][j] = dp[i][j]; }
    rep(j, 1 << n) {
      chmin(dp[i + 1][j | num], dp[i][j] + a);
      // cout<<dp[i+1][j|num]<<endl;
    }
  }
  int res = dp[m][(1 << n) - 1];
  if (res == 1e9)
    cout << -1 << endl;
  else
    cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

using namespace std;
vector<vector<int>> dp(1001, vector<int>(4096, 1e9));

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  dp[0][0] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    int num = 0;
    rep(j, b) {
      int t;
      cin >> t;
      num += 1 << (t - 1);
    }
    //  cout<<"num"<<num<<endl;
    rep(j, 1 << n) { dp[i + 1][j] = dp[i][j]; }
    rep(j, 1 << n) {
      chmin(dp[i + 1][j | num], dp[i][j] + a);
      // cout<<dp[i+1][j|num]<<endl;
    }
  }
  int res = dp[m][(1 << n) - 1];
  if (res == 1e9)
    cout << -1 << endl;
  else
    cout << res << endl;

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
using lint = int64_t;

int main() {
  const int INF = 1000000000;

  int N, M;
  cin >> N >> M;
  vector<int> a(M, 0), c(M, 0);
  rep(i, M) {
    int b;
    cin >> a[i] >> b;
    rep(j, b) {
      int x;
      cin >> x;
      --x;
      c[i] += (1 << x);
    }
  }
  vector<vector<int>> dp(M + 1, vector<int>(1 << N, INF));
  dp[0][0] = 0;
  rep(i, M) rep(j, 1 << N) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    dp[i + 1][j | c[i]] = min(dp[i + 1][j | c[i]], dp[i][j] + a[i]);
  }
  if (dp[M][(1 << M) - 1] == INF)
    cout << -1 << "\n";
  else
    cout << dp[M][(1 << N) - 1] << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
using lint = int64_t;

int main() {
  const int INF = 1000000000;

  int N, M;
  cin >> N >> M;
  vector<int> a(M, 0), c(M, 0);
  rep(i, M) {
    int b;
    cin >> a[i] >> b;
    rep(j, b) {
      int x;
      cin >> x;
      --x;
      c[i] += (1 << x);
    }
  }
  vector<vector<int>> dp(M + 1, vector<int>(1 << N, INF));
  dp[0][0] = 0;
  rep(i, M) rep(j, 1 << N) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    dp[i + 1][j | c[i]] = min(dp[i + 1][j | c[i]], dp[i][j] + a[i]);
  }
  if (dp[M][(1 << N) - 1] == INF)
    cout << -1 << "\n";
  else
    cout << dp[M][(1 << N) - 1] << "\n";
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int mod = 1e9 + 7, A = 1e3 + 2;
int n, m, price[A], masks[888];
ll dp[A][888];
ll solve(int ind, int mask) {
  if (ind == m)
    return (mask == (1 << n) - 1 ? 0 : 1e18);
  if (~dp[ind][mask])
    return dp[ind][mask];
  ll val = solve(ind + 1, mask);
  val = min(val, solve(ind + 1, mask | masks[ind]) + price[ind]);
  return dp[ind][mask] = val;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> price[i] >> x;
    while (x--)
      cin >> y, masks[i] |= (1 << (y - 1));
  }
  memset(dp, -1, sizeof dp);
  ll ans = solve(0, 0);
  cout << (ans >= 1e18 ? -1 : ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
const int mod = 1e9 + 7, A = 1e3 + 2;
int n, m, price[A], masks[8888];
ll dp[A][8888];
ll solve(int ind, int mask) {
  if (ind == m)
    return (mask == (1 << n) - 1 ? 0 : 1e18);
  if (~dp[ind][mask])
    return dp[ind][mask];
  ll val = solve(ind + 1, mask);
  val = min(val, solve(ind + 1, mask | masks[ind]) + price[ind]);
  return dp[ind][mask] = val;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> price[i] >> x;
    while (x--)
      cin >> y, masks[i] |= (1 << (y - 1));
  }
  memset(dp, -1, sizeof dp);
  ll ans = solve(0, 0);
  cout << (ans >= 1e18 ? -1 : ans);
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

typedef long long int ll;

// const ll INF=(ll)1e18;
const int INF = (1 << 21);

int dp[13][100000];

int main() {
  int N, M;
  cin >> N >> M;

  REP(i, M + 1) REP(j, 1 << N) dp[i][j] = INF;
  dp[0][0] = 0;

  REP(i, M) {
    int a, b;
    cin >> a >> b;
    int m = 0;
    REP(j, b) {
      int c;
      cin >> c;
      m += 1 << (c - 1);
    }

    REP(j, 1 << N) {
      if (dp[i][j] == INF)
        continue;
      dp[i + 1][j] = dp[i][j];
    }

    REP(j, 1 << N) {
      if (dp[i][j] == INF)
        continue;

      //      printf("%d %d: %d %d\n", i,j|m, dp[i][j|m], dp[i][j] + a);
      int tmp = min(dp[i][j | m], dp[i][j] + a);
      dp[i + 1][j | m] = min(tmp, dp[i + 1][j | m]);
    }
  }

  // REP(i,M+1){
  //   REP(j,1<<N){
  //     cout << dp[i][j] << " ";
  //   }
  //   cout << endl;
  // }

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


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

typedef long long int ll;

// const ll INF=(ll)1e18;
const int INF = (1 << 21);

int dp[1001][4096];

int main() {
  int N, M;
  cin >> N >> M;

  REP(i, M + 1) REP(j, 1 << N) dp[i][j] = INF;
  dp[0][0] = 0;

  REP(i, M) {
    int a, b;
    cin >> a >> b;
    int m = 0;
    REP(j, b) {
      int c;
      cin >> c;
      m += 1 << (c - 1);
    }

    REP(j, 1 << N) {
      if (dp[i][j] == INF)
        continue;
      dp[i + 1][j] = dp[i][j];
    }

    REP(j, 1 << N) {
      if (dp[i][j] == INF)
        continue;

      //      printf("%d %d: %d %d\n", i,j|m, dp[i][j|m], dp[i][j] + a);
      int tmp = min(dp[i][j | m], dp[i][j] + a);
      dp[i + 1][j | m] = min(tmp, dp[i + 1][j | m]);
    }
  }

  // REP(i,M+1){
  //   REP(j,1<<N){
  //     cout << dp[i][j] << " ";
  //   }
  //   cout << endl;
  // }

  if (dp[M][(1 << N) - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[M][(1 << N) - 1] << endl;
  }
}
Problem p02901 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

int N, M, a[1123], b[1123], c[1123][123];
int memo[1123][1 << 13];

int dp(int i, int j) {
  if (memo[i][j] != -1)
    return memo[i][j];
  if (!i) {
    if (j) {
      return 1000000000;
    } else {
      return 0;
    }
  }

  int ret = dp(i - 1, j);

  int mask = 0; //(1 << N) - 1;
  rep(t, b[i - 1]) { mask ^= 1 << (c[i - 1][t] - 1); }

  for (int t = 0; t < (1 << N); ++t) {
    ret = min(ret, dp(i - 1, j & (~(t & mask))) + a[i - 1]);
  }

  return memo[i][j] = ret;
}

int main() {

  memset(memo, 0xff, sizeof(memo));

  set<int> s;
  scanf("%d%d", &N, &M);
  rep(i, M) {
    scanf("%d%d", &a[i], &b[i]);
    rep(j, b[i]) {
      scanf("%d", &c[i][j]);
      s.insert(c[i][j]);
    }
  }
  if (s.size() != N) {
    printf("-1\n");
    return 0;
  }

  printf("%d\n", dp(M, (1 << N) - 1));

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

int N, M, a[1123], b[1123], c[1123][123];
int memo[1123][1 << 13];

int dp(int i, int j) {
  if (memo[i][j] != -1)
    return memo[i][j];
  if (!i) {
    if (j) {
      return 1000000000;
    } else {
      return 0;
    }
  }

  int ret = dp(i - 1, j);

  int mask = 0; //(1 << N) - 1;
  rep(t, b[i - 1]) { mask ^= 1 << (c[i - 1][t] - 1); }

  for (int t = 0 + (1 << N) - 1; t < (1 << N); ++t) {
    ret = min(ret, dp(i - 1, j & (~(t & mask))) + a[i - 1]);
  }

  return memo[i][j] = ret;
}

int main() {

  memset(memo, 0xff, sizeof(memo));

  set<int> s;
  scanf("%d%d", &N, &M);
  rep(i, M) {
    scanf("%d%d", &a[i], &b[i]);
    rep(j, b[i]) {
      scanf("%d", &c[i][j]);
      s.insert(c[i][j]);
    }
  }
  if (s.size() != N) {
    printf("-1\n");
    return 0;
  }

  printf("%d\n", dp(M, (1 << N) - 1));

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long MIN(long long x, long long y) {
  if (x > y)
    return y;
  else
    return x;
}

int main() {

  long long INF = 100000000 + 100;

  // 情報を取り込む

  int N, M;
  cin >> N >> M;

  vector<int> a(M);
  vector<int> b(M);
  vector<vector<int>> c(M);

  for (int i = 0; i < N; i++) {
    cin >> a[i];
    cin >> b[i];

    for (int j = 0; j < b[i]; j++) {
      int p;
      cin >> p;
      c[i].push_back(p - 1);
    }
  }

  // dp

  vector<vector<long long>> dp(M + 1, vector<long long>((1 << N)));

  // 0個のカギで開けることができる箱は存在しない

  for (long long bit = 1; bit < (1 << N); bit++) {
    dp[0][bit] = INF;
  }

  // 漸化式部分

  for (int i = 0; i < M; i++) {

    for (long long bit = 0; bit < (1 << N); bit++) {

      long long BIT = bit;
      for (int j = 0; j < b[i]; j++) {
        BIT &= ~(1 << c[i][j]);
      }

      dp[i + 1][bit] = MIN(dp[i][bit], dp[i][BIT] + a[i]);
    }
  }

  // 出力

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

  else
    cout << dp[M][(1 << N) - 1] << endl;
}

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

long long MIN(long long x, long long y) {
  if (x > y)
    return y;
  else
    return x;
}

int main() {

  long long INF = 100000000 + 100;

  // 情報を取り込む

  int N, M;
  cin >> N >> M;

  vector<int> a(M);
  vector<int> b(M);
  vector<vector<int>> c(M);

  for (int i = 0; i < M; i++) {
    cin >> a[i];
    cin >> b[i];

    for (int j = 0; j < b[i]; j++) {
      int p;
      cin >> p;
      c[i].push_back(p - 1);
    }
  }

  // dp

  vector<vector<long long>> dp(M + 1, vector<long long>((1 << N)));

  // 0個のカギで開けることができる箱は存在しない

  for (long long bit = 1; bit < (1 << N); bit++) {
    dp[0][bit] = INF;
  }

  // 漸化式部分

  for (int i = 0; i < M; i++) {

    for (long long bit = 0; bit < (1 << N); bit++) {

      long long BIT = bit;
      for (int j = 0; j < b[i]; j++) {
        BIT &= ~(1 << c[i][j]);
      }

      dp[i + 1][bit] = MIN(dp[i][bit], dp[i][BIT] + a[i]);
    }
  }

  // 出力

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

  else
    cout << dp[M][(1 << N) - 1] << endl;
}