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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll a, b;
  cin >> a >> b;
  ll ab = gcd(a, b);
  int ans = 1;
  for (int i = 2; i * i <= ab; ++i) {
    if (ab % i == 0) {
      ++ans;
      while (ab % i == 0) {
        ab /= i;
      }
    }
  }
  if (ab > 1)
    ++ans;
  cout << ans << endl;
}

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

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll a, b;
  cin >> a >> b;
  ll ab = gcd(a, b);
  int ans = 1;
  for (ll i = 2; i * i <= ab; ++i) {
    if (ab % i == 0) {
      ++ans;
      while (ab % i == 0) {
        ab /= i;
      }
    }
  }
  if (ab > 1)
    ++ans;
  cout << ans << endl;
}
Problem p02900 (C++)

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> P;

int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  ll a, b;
  cin >> a >> b;
  ll c = gcd(a, b);
  ll cnt = 1;
  for (ll i = 2; i * i < c + 1; i++) {
    if (c % i == 0) {
      cnt++;
      while (c % i == 0) {
        c = c / i;
      }
    }
  }
  if (c != 1) {
    cnt++;
  }
  cout << cnt << endl;
}

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> P;

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  ll a, b;
  cin >> a >> b;
  ll c = gcd(a, b);
  ll cnt = 1;
  for (ll i = 2; i * i < c + 1; i++) {
    if (c % i == 0) {
      cnt++;
      while (c % i == 0) {
        c = c / i;
      }
    }
  }
  if (c != 1) {
    cnt++;
  }
  cout << cnt << endl;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

int main() {
  ll a, b;
  cin >> a >> b;
  ll g = __gcd(a, b);
  ll ans = 1;
  for (int i = 2; i * i <= g; ++i) {
    if (g % i == 0) {
      while (g % i == 0) {
        g /= i;
      }
      ans++;
    }
  }
  if (g != 1)
    ans++;
  cout << ans << endl;
  return 0;
}


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

int main() {
  ll a, b;
  cin >> a >> b;
  ll g = __gcd(a, b);
  ll ans = 1;
  for (ll i = 2; i * i <= g; ++i) {
    if (g % i == 0) {
      while (g % i == 0) {
        g /= i;
      }
      ans++;
    }
  }
  if (g != 1)
    ans++;
  cout << ans << endl;
  return 0;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define fi first
#define sc second
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

int main() {
  ll a, b, ans = 1;
  scanf("%lld %lld", &a, &b);

  ll fpb = __gcd(a, b);
  ll temp = fpb;

  for (int i = 2; i * i <= fpb and temp > 1; i++) {
    if (temp % i == 0)
      ans++;
    while (temp % i == 0)
      temp /= i;
  }
  if (temp > 1)
    ans++;
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define fi first
#define sc second
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

int main() {
  ll a, b, ans = 1;
  scanf("%lld %lld", &a, &b);

  ll fpb = __gcd(a, b);
  ll temp = fpb;

  for (ll i = 2; i * i <= fpb and temp > 1; i++) {
    if (temp % i == 0)
      ans++;
    while (temp % i == 0)
      temp /= i;
  }
  if (temp > 1)
    ans++;
  printf("%lld\n", ans);
}
Problem p02900 (C++)

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

typedef long long ll;
const ll INF = 1010000000000000017LL;
const ll MOD = 1000000007LL;

#define REP(i, n) for (ll i = 0; i < n; i++)

// #define DEBUG(fmt, ...)
#define DEBUG(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__)

bool is_prime(ll n) {
  if (n == 1) {
    return false;
  }

  ll i = 2;
  // This will loop from 2 to int(sqrt(x))
  while (i * i <= n) {
    // Check if i divides x without leaving a remainder
    if (n % i == 0) {
      // This means that n has a factor in between 2 and sqrt(n)
      // So it is not a prime number
      return false;
    }
    i += 1;
  }
  // If we did not find any factor in the above loop,
  // then n is a prime number
  return true;
}

int main() {
  std::cout << std::fixed << std::setprecision(10);

  ll A, B;

  cin >> A >> B;

  ll m = min(A, B);
  ll count = 0;

  set<ll> primes;
  for (int i = 1; i * i <= m; i++) {
    if (m % i == 0) {
      if (i == 1 || is_prime(i)) {
        primes.insert(i);
      }
      if (is_prime(m / i)) {
        primes.insert(m / i);
      }
    }
  }

  ll x = max(A, B);
  for (auto p : primes) {
    if (x % p == 0) {
      count++;
    }
  }
  cout << count << endl;
}


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

typedef long long ll;
const ll INF = 1010000000000000017LL;
const ll MOD = 1000000007LL;

#define REP(i, n) for (ll i = 0; i < n; i++)

// #define DEBUG(fmt, ...)
#define DEBUG(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__)

bool is_prime(ll n) {
  if (n == 1) {
    return false;
  }

  ll i = 2;
  // This will loop from 2 to int(sqrt(x))
  while (i * i <= n) {
    // Check if i divides x without leaving a remainder
    if (n % i == 0) {
      // This means that n has a factor in between 2 and sqrt(n)
      // So it is not a prime number
      return false;
    }
    i += 1;
  }
  // If we did not find any factor in the above loop,
  // then n is a prime number
  return true;
}

int main() {
  std::cout << std::fixed << std::setprecision(10);

  ll A, B;

  cin >> A >> B;

  ll m = min(A, B);
  ll count = 0;

  set<ll> primes;
  for (ll i = 1; i * i <= m; i++) {
    if (m % i == 0) {
      if (i == 1 || is_prime(i)) {
        primes.insert(i);
      }
      if (is_prime(m / i)) {
        primes.insert(m / i);
      }
    }
  }

  ll x = max(A, B);
  for (auto p : primes) {
    if (x % p == 0) {
      count++;
    }
  }
  cout << count << endl;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>

template <typename T> T gcd(T a, T b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  int64_t a, b;
  std::cin >> a >> b;

  int64_t g = gcd(a, b);

  std::set<int64_t> primes;
  primes.insert(1);
  for (int i = 2; i * i <= g; i++) {
    while (g % i == 0) {
      primes.insert(i);
      g /= i;
    }
  }
  if (g > 1) {
    primes.insert(g);
  }

  std::cout << primes.size() << std::endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <set>

template <typename T> T gcd(T a, T b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  int64_t a, b;
  std::cin >> a >> b;

  int64_t g = gcd(a, b);

  std::set<int64_t> primes;
  primes.insert(1);
  for (int64_t i = 2; i * i <= g; i++) {
    while (g % i == 0) {
      primes.insert(i);
      g /= i;
    }
  }
  if (g > 1) {
    primes.insert(g);
  }

  std::cout << primes.size() << std::endl;
  return 0;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long a, b;
  cin >> a >> b;

  int ans = 1;
  for (long long i = 2; i <= a; i++)
    if (a % i == 0) {
      if (b % i == 0) {
        ans++;
      }
      while (a % i == 0) {
        a /= i;
      }
    }

  ans += (a > 1 && b % a == 0);
  cout << ans << endl;
}


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

using namespace std;

int main() {
  long long a, b;
  cin >> a >> b;

  int ans = 1;
  for (long long i = 2; i * i <= a; i++)
    if (a % i == 0) {
      if (b % i == 0) {
        ans++;
      }
      while (a % i == 0) {
        a /= i;
      }
    }

  ans += (a > 1 && b % a == 0);
  cout << ans << endl;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using ll = long long;
typedef pair<int, int> Pair;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<pair<ll, ll>> f(ll n) {
  vector<pair<ll, ll>> res;
  for (int i = 2; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    ll cnt = 0;
    while (n % i == 0)
      cnt++, n /= i;
    res.emplace_back(i, cnt);
  }
  if (n > 1)
    res.emplace_back(n, 1);
  return res;
}

int main() {
  ll a, b;
  cin >> a >> b;

  ll g = gcd(a, b);
  auto s = f(g);

  cout << s.size() + 1 << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using ll = long long;
typedef pair<int, int> Pair;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<pair<ll, ll>> f(ll n) {
  vector<pair<ll, ll>> res;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    ll cnt = 0;
    while (n % i == 0)
      cnt++, n /= i;
    res.emplace_back(i, cnt);
  }
  if (n > 1)
    res.emplace_back(n, 1);
  return res;
}

int main() {
  ll a, b;
  cin >> a >> b;

  ll g = gcd(a, b);
  auto s = f(g);

  cout << s.size() + 1 << endl;
  return 0;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }

vector<pair<ll, int>> factorize(ll n) {
  vector<pair<ll, int>> v;
  for (int i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    v.emplace_back(i, 0);
    while (n % i == 0) {
      v.back().second++;
      n /= i;
    }
  }
  if (n != 1) {
    v.emplace_back(n, 1);
  }
  return v;
}

int main() {
  ll a, b;
  cin >> a >> b;
  ll g = gcd(a, b);
  auto f = factorize(g);
  cout << f.size() + 1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }

vector<pair<ll, int>> factorize(ll n) {
  vector<pair<ll, int>> v;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    v.emplace_back(i, 0);
    while (n % i == 0) {
      v.back().second++;
      n /= i;
    }
  }
  if (n != 1) {
    v.emplace_back(n, 1);
  }
  return v;
}

int main() {
  ll a, b;
  cin >> a >> b;
  ll g = gcd(a, b);
  auto f = factorize(g);
  cout << f.size() + 1 << endl;
  return 0;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ":" << x << endl
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

ll gcd(ll a, ll b) {
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int main() {
  ll a, b;
  cin >> a >> b;
  ll g = gcd(a, b);
  int cnt = 1;
  for (int i = 2; i * i <= g; i++) {
    if (g % i)
      continue;
    while (g % i == 0)
      g /= i;
    cnt++;
  }
  if (g != 1)
    cnt++;
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ":" << x << endl
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

ll gcd(ll a, ll b) {
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int main() {
  ll a, b;
  cin >> a >> b;
  ll g = gcd(a, b);
  int cnt = 1;
  for (ll i = 2; i * i <= g; i++) {
    if (g % i)
      continue;
    while (g % i == 0)
      g /= i;
    cnt++;
  }
  if (g != 1)
    cnt++;
  cout << cnt << endl;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define umax(x, y) x = max(x, (y))
#define umin(x, y) x = min(x, (y))
#define For(i, a) for (int i = 1; i <= a; i++)
#define ort (b + s) / 2
#define y2 asrwjaelkf
#define y1 asseopirwjaelkf
#define set multiset

using namespace std;

typedef long long Lint;
typedef double db;
typedef pair<int, int> ii;
typedef pair<int, char> ic;
typedef pair<db, db> dd;
typedef pair<ii, int> iii;
typedef pair<ii, ii> i4;

const int maxn = 100020;
const int maxm = 1000020;
const int MOd = 998244353;

Lint a, b;

Lint gcd(Lint a, Lint b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void solve() {
  scanf("%lld %lld", &a, &b);
  Lint x = gcd(a, b);
  Lint cnt = 1;
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0) {
      while (x % i == 0)
        x /= i;
      cnt++;
    }
  if (x != 1)
    cnt++;
  cout << cnt << endl;
}

int main() {

  // freopen("asd.in","r",stdin);
  // freopen("output17.txt","w",stdout);

  int n = 1;
  // scanf("%d",&n);
  while (n--)
    solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define umax(x, y) x = max(x, (y))
#define umin(x, y) x = min(x, (y))
#define For(i, a) for (int i = 1; i <= a; i++)
#define ort (b + s) / 2
#define y2 asrwjaelkf
#define y1 asseopirwjaelkf
#define set multiset

using namespace std;

typedef long long Lint;
typedef double db;
typedef pair<int, int> ii;
typedef pair<int, char> ic;
typedef pair<db, db> dd;
typedef pair<ii, int> iii;
typedef pair<ii, ii> i4;

const int maxn = 100020;
const int maxm = 1000020;
const int MOd = 998244353;

Lint a, b;

Lint gcd(Lint a, Lint b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void solve() {
  scanf("%lld %lld", &a, &b);
  Lint x = gcd(a, b);
  Lint cnt = 1;
  for (Lint i = 2; i * i <= x; i++)
    if (x % i == 0) {
      while (x % i == 0)
        x /= i;
      cnt++;
    }
  if (x != 1)
    cnt++;
  cout << cnt << endl;
}

int main() {

  // freopen("asd.in","r",stdin);
  // freopen("output17.txt","w",stdout);

  int n = 1;
  // scanf("%d",&n);
  while (n--)
    solve();

  return 0;
}
Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int a, b, cnt;
void solve() {
  cin >> a >> b;
  a = __gcd(a, b);
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      cnt++;
      while (a % i == 0)
        a /= i;
    }
  }
  if (a > 1)
    cnt++;
  cout << cnt + 1 << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

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

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int a, b, cnt;
void solve() {
  cin >> a >> b;
  a = __gcd(a, b);
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      cnt++;
      while (a % i == 0)
        a /= i;
    }
  }
  if (a > 1)
    cnt++;
  cout << cnt + 1 << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
/* while(!cin.eof()) */

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9 + 7)
#define MAXI (int)(1e15 + 100)
#define N 1000050

int n, m;
vector<int> vec;
int dp[(1 << 12) + 1][1005][2];
int cost[1005];

int fun(int mask, int ind, int stage) {
  if (ind > m - 1) {
    if (mask == (1 << n) - 1)
      return 0;
    return MAXI;
  }

  int ans = dp[mask][ind][stage];
  if (ans != -1)
    return ans;

  ans = MAXI;
  int x = vec[ind] | mask;
  if (x > mask)
    ans = min(ans, cost[ind] + fun(x, ind + 1, 1));

  ans = min(ans, fun(mask, ind + 1, 0));
  return dp[mask][ind][stage] = ans;
}

// Driver code to test above functions
int32_t main() {

#ifndef ONLINE_JUDGE
  // for getting input from inpu.txt
  freopen("input.txt", "r", stdin);
// for writing output to output.txt
// freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> cost[i];
    int sz;
    cin >> sz;
    int mask = 0;
    for (int j = 0; j < sz; j++) {
      int temp;
      cin >> temp;
      temp--;
      mask |= (1 << temp);
    }
    vec.push_back(mask);
  }

  memset(dp, -1, sizeof dp);

  int x = fun(0, 0, 0);
  if (x == MAXI)
    cout << -1;
  else
    cout << x;

  return 0;
}

Fixed submission:
/* while(!cin.eof()) */

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9 + 7)
#define MAXI (int)(1e15 + 100)
#define N 1000050

int n, m;
vector<int> vec;
int dp[(1 << 12) + 1][1005][2];
int cost[1005];

int fun(int mask, int ind, int stage) {
  if (ind > m - 1) {
    if (mask == (1 << n) - 1)
      return 0;
    return MAXI;
  }

  int ans = dp[mask][ind][stage];
  if (ans != -1)
    return ans;

  ans = MAXI;
  int x = vec[ind] | mask;
  if (x > mask)
    ans = min(ans, cost[ind] + fun(x, ind + 1, 1));

  ans = min(ans, fun(mask, ind + 1, 0));
  return dp[mask][ind][stage] = ans;
}

// Driver code to test above functions
int32_t main() {

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> cost[i];
    int sz;
    cin >> sz;
    int mask = 0;
    for (int j = 0; j < sz; j++) {
      int temp;
      cin >> temp;
      temp--;
      mask |= (1 << temp);
    }
    vec.push_back(mask);
  }

  memset(dp, -1, sizeof dp);

  int x = fun(0, 0, 0);
  if (x == MAXI)
    cout << -1;
  else
    cout << x;

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef int ll;
typedef long double ld;
const ll N = 1005;
char en = '\n';
ll inf = 1e16;
ll mod = 1e9 + 7;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

ll n, m;
ll mask1[N];
ll cost[N];
ll dp[N][1 << 12];

ll recur(ll pos, ll mask) {
  if (mask == ((1 << n) - 1))
    return 0;
  if (pos == m + 1) {
    return inf;
  }

  ll &ans = dp[pos][mask];

  ans =
      min(recur(pos + 1, mask), cost[pos] + recur(pos + 1, mask | mask1[pos]));
  return ans;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;

  for (ll i = 1; i <= m; i++) {
    cin >> cost[i];
    mask1[i] = 0;
    ll b;
    cin >> b;
    while (b--) {
      ll x;
      cin >> x;
      x--;
      mask1[i] |= (1 << x);
    }
  }
  memset(dp, -1, sizeof(dp));

  ll res = recur(1, 0);
  if (res > 1e12)
    cout << -1 << en;
  else
    cout << recur(1, 0) << en;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef int ll;
typedef long double ld;
const ll N = 1005;
char en = '\n';
ll inf = 1e16;
ll mod = 1e9 + 7;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

ll n, m;
ll mask1[N];
ll cost[N];
ll dp[N][1 << 12];

ll recur(ll pos, ll mask) {
  if (mask == ((1 << n) - 1))
    return 0;
  if (pos == m + 1) {
    return inf;
  }

  ll &ans = dp[pos][mask];
  if (ans != -1)
    return ans;
  ans =
      min(recur(pos + 1, mask), cost[pos] + recur(pos + 1, mask | mask1[pos]));
  return ans;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;

  for (ll i = 1; i <= m; i++) {
    cin >> cost[i];
    mask1[i] = 0;
    ll b;
    cin >> b;
    while (b--) {
      ll x;
      cin >> x;
      x--;
      mask1[i] |= (1 << x);
    }
  }
  memset(dp, -1, sizeof(dp));

  ll res = recur(1, 0);
  if (res > 1e12)
    cout << -1 << en;
  else
    cout << recur(1, 0) << en;

  return 0;
}
Problem p02901 (C++)

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

using namespace std;
using ll = long long;

#define inf 9000000000000
#define mx 1003

int n, m;
int a[mx], b[mx], c[mx];
ll dp[15][1 << 15];

ll solve(int i, int mask) {
  if (i == m) {
    if (mask == (1 << n) - 1)
      return 0;
    return inf;
  }

  if (mask == (1 << n) - 1)
    return 0;
  auto &ans = dp[i][mask];
  if (ans != -1)
    return ans;
  ans = inf;
  int nask = mask | c[i];
  ans = a[i] + solve(i + 1, nask);
  ans = min(ans, solve(i + 1, mask));
  return ans;
}

int main() {
  cin >> n >> m;
  int mark = 0;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    int mask = 0;
    for (int j = 0; j < b[i]; j++) {
      int x;
      cin >> x;
      x--;
      mask |= (1 << x);
    }

    c[i] = mask;
    mark |= mask;
  }

  if (mark != (1 << n) - 1) {
    cout << -1 << endl;
    return 0;
  }

  memset(dp, -1, sizeof dp);
  auto ans = solve(0, 0);
  cout << ans << endl;
}

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

using namespace std;
using ll = long long;

#define inf 9000000000000
#define mx 1003

int n, m;
int a[mx], b[mx], c[mx];
ll dp[mx][1 << 15];

ll solve(int i, int mask) {
  if (i == m) {
    if (mask == (1 << n) - 1)
      return 0;
    return inf;
  }

  if (mask == (1 << n) - 1)
    return 0;
  auto &ans = dp[i][mask];
  if (ans != -1)
    return ans;
  ans = inf;
  int nask = mask | c[i];
  ans = a[i] + solve(i + 1, nask);
  ans = min(ans, solve(i + 1, mask));
  return ans;
}

int main() {
  cin >> n >> m;
  int mark = 0;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    int mask = 0;
    for (int j = 0; j < b[i]; j++) {
      int x;
      cin >> x;
      x--;
      mask |= (1 << x);
    }

    c[i] = mask;
    mark |= mask;
  }

  if (mark != (1 << n) - 1) {
    cout << -1 << endl;
    return 0;
  }

  memset(dp, -1, sizeof dp);
  auto ans = solve(0, 0);
  cout << ans << endl;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

  int n, m, a[1123], b[1123], c[1123][15];

  cin >> n >> m;
  REP(i, m) {
    cin >> a[i] >> b[i];
    REP(j, b[i])
    cin >> c[i][j];
  }

  int dp[110][(1 << 12) + 10];
  memset(dp, -1, sizeof(dp));

  dp[0][0] = 0;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < (1 << n); j++) {
      if (dp[i][j] == -1)
        continue;

      if (dp[i + 1][j] > dp[i][j] || dp[i + 1][j] == -1)
        dp[i + 1][j] = dp[i][j];

      int val = dp[i][j] + a[i];

      int nj = j;
      for (int k = 0; k < b[i]; k++)
        nj |= (1 << (c[i][k] - 1));

      if (dp[i + 1][nj] > val || dp[i + 1][nj] == -1)
        dp[i + 1][nj] = val;
    }
  }

  cout << dp[m][(1 << n) - 1] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

  int n, m, a[1123], b[1123], c[1123][15];

  cin >> n >> m;
  REP(i, m) {
    cin >> a[i] >> b[i];
    REP(j, b[i])
    cin >> c[i][j];
  }

  int dp[1010][(1 << 12) + 10];
  memset(dp, -1, sizeof(dp));

  dp[0][0] = 0;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < (1 << n); j++) {
      if (dp[i][j] == -1)
        continue;

      if (dp[i + 1][j] > dp[i][j] || dp[i + 1][j] == -1)
        dp[i + 1][j] = dp[i][j];

      int val = dp[i][j] + a[i];

      int nj = j;
      for (int k = 0; k < b[i]; k++)
        nj |= (1 << (c[i][k] - 1));

      if (dp[i + 1][nj] > val || dp[i + 1][nj] == -1)
        dp[i + 1][nj] = val;
    }
  }

  cout << dp[m][(1 << n) - 1] << endl;

  return 0;
}
Problem p02901 (C++)

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

#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;

const int INF = 1e9 + 7;

using namespace std;

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

  int n, m;
  cin >> n >> m;

  vector<int> a(m);
  vector<int> b(m);
  vector<vector<int>> c;
  rep(i, 0, m) {
    cin >> a[i] >> b[i];
    vector<int> tmp(b[i]);
    rep(j, 0, b[i]) cin >> tmp[j];
    c.push_back(tmp);
  }

  vector<vector<int>> dp(1000, vector<int>(5000, INF));
  dp[0][0] = 0;

  for (int i = 0; i < m; i++) {
    int open_id = 0;
    for (int j = 0; j < b[i]; j++)
      open_id |= (1 << (c[i][j] - 1));

    dp[i + 1] = dp[i];
    for (int j = 0; j < (1 << n); j++) {
      dp[i + 1][j | open_id] = min({dp[i + 1][j | open_id], dp[i][j | open_id],
                                    dp[i][j] + a[i]}); // dp[i][0]は常にゼロ
    }
  }

  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>

#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;

const int INF = 1e9 + 7;

using namespace std;

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

  int n, m;
  cin >> n >> m;

  vector<int> a(m);
  vector<int> b(m);
  vector<vector<int>> c;
  rep(i, 0, m) {
    cin >> a[i] >> b[i];
    vector<int> tmp(b[i]);
    rep(j, 0, b[i]) cin >> tmp[j];
    c.push_back(tmp);
  }

  vector<vector<int>> dp(2000, vector<int>(5000, INF));
  dp[0][0] = 0;

  for (int i = 0; i < m; i++) {
    int open_id = 0;
    for (int j = 0; j < b[i]; j++)
      open_id |= (1 << (c[i][j] - 1));

    dp[i + 1] = dp[i];
    for (int j = 0; j < (1 << n); j++) {
      dp[i + 1][j | open_id] = min({dp[i + 1][j | open_id], dp[i][j | open_id],
                                    dp[i][j] + a[i]}); // dp[i][0]は常にゼロ
    }
  }

  if (dp[m][(1 << n) - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[m][(1 << n) - 1] << endl;

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <map>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int INF = 1e9 + 7;
  int A[15] = {};
  int B[15] = {};
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    int b;
    cin >> b;
    for (int j = 0; j < b; j++) {
      int c;
      cin >> c;
      B[i] += (1 << (c - 1));
    }
  }
  int dp[4100] = {};
  fill(dp, dp + (1 << N), INF);
  dp[0] = 0;
  for (int i = 0; i < (1 << N) - 1; i++) {
    for (int j = 0; j < M; j++) {
      dp[i | B[j]] = min(dp[i | B[j]], dp[i] + A[j]);
    }
  }
  if (dp[(1 << N) - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[(1 << N) - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <map>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int INF = 1e9 + 7;
  int A[1005] = {};
  int B[1005] = {};
  for (int i = 0; i < M; i++) {
    cin >> A[i];
    int b;
    cin >> b;
    for (int j = 0; j < b; j++) {
      int c;
      cin >> c;
      B[i] += (1 << (c - 1));
    }
  }
  int dp[4100] = {};
  fill(dp, dp + (1 << N), INF);
  dp[0] = 0;
  for (int i = 0; i < (1 << N) - 1; i++) {
    for (int j = 0; j < M; j++) {
      dp[i | B[j]] = min(dp[i | B[j]], dp[i] + A[j]);
    }
  }
  if (dp[(1 << N) - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[(1 << N) - 1] << endl;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int, int> pii;
const int N = 10000;
const db pi = acos(-1.0);
#define lowbit(x) (x) & (-x)
#define sqr(x) (x) * (x)
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
#define fir first
#define sec second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define maxd 998244353
#define eps 1e-8
int n, m, cost[20], p[20], f[5020], g[5020];

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

int main() {
  n = read();
  m = read();
  rep(i, 1, m) {
    cost[i] = read();
    int len = read();
    rep(j, 1, len) {
      int tmp = read();
      p[i] |= (1 << (tmp - 1));
    }
  }
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  int lim = (1 << n) - 1;
  rep(i, 1, m) {
    memcpy(g, f, sizeof(g));
    rep(j, 0, lim) g[j | p[i]] = min(g[j | p[i]], f[j] + cost[i]);
    // rep(j,0,5) cout << g[i] << " ";cout << endl;
    memcpy(f, g, sizeof(f));
    // rep(j,0,5) cout << f[i] << " ";cout << endl;
  }
  if (f[lim] < maxd)
    printf("%d", f[lim]);
  else
    puts("-1");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int, int> pii;
const int N = 10000;
const db pi = acos(-1.0);
#define lowbit(x) (x) & (-x)
#define sqr(x) (x) * (x)
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
#define fir first
#define sec second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define maxd 998244353
#define eps 1e-8
int n, m, cost[1020], p[1020], f[5020], g[5020];

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

int main() {
  n = read();
  m = read();
  rep(i, 1, m) {
    cost[i] = read();
    int len = read();
    rep(j, 1, len) {
      int tmp = read();
      p[i] |= (1 << (tmp - 1));
    }
  }
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  int lim = (1 << n) - 1;
  rep(i, 1, m) {
    memcpy(g, f, sizeof(g));
    rep(j, 0, lim) g[j | p[i]] = min(g[j | p[i]], f[j] + cost[i]);
    // rep(j,0,5) cout << g[i] << " ";cout << endl;
    memcpy(f, g, sizeof(f));
    // rep(j,0,5) cout << f[i] << " ";cout << endl;
  }
  if (f[lim] < maxd)
    printf("%d", f[lim]);
  else
    puts("-1");
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
//
// Created by Hideaki Imamura on 2020-03-23.
//
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

int N, M;
vector<ll> a, b;
vector<int> c;
vector<vector<ll>> dp;

int main() {
  cin >> N >> M;
  a.resize(N);
  b.resize(M);
  c.assign(M, 0);
  for (int i = 0; i < M; ++i) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; ++j) {
      int tmp;
      cin >> tmp;
      c[i] += 1 << (tmp - 1);
    }
  }

  dp.assign(M + 1, vector<ll>(1 << N, INF));
  dp[0][0] = 0;
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < (1 << N); ++j) {
      chmin(dp[i + 1][j], dp[i][j]);
      chmin(dp[i + 1][j | c[i]], dp[i][j] + a[i]);
    }
  }
  cout << (dp[M][(1 << N) - 1] < INF ? dp[M][(1 << N) - 1] : -1) << endl;
  return 0;
}

Fixed submission:
//
// Created by Hideaki Imamura on 2020-03-23.
//
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

int N, M;
vector<ll> a, b;
vector<int> c;
vector<vector<ll>> dp;

int main() {
  cin >> N >> M;
  a.resize(M);
  b.resize(M);
  c.assign(M, 0);
  for (int i = 0; i < M; ++i) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; ++j) {
      int tmp;
      cin >> tmp;
      c[i] += 1 << (tmp - 1);
    }
  }

  dp.assign(M + 1, vector<ll>(1 << N, INF));
  dp[0][0] = 0;
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < (1 << N); ++j) {
      chmin(dp[i + 1][j], dp[i][j]);
      chmin(dp[i + 1][j | c[i]], dp[i][j] + a[i]);
    }
  }
  cout << (dp[M][(1 << N) - 1] < INF ? dp[M][(1 << N) - 1] : -1) << endl;
  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int max_n = 15;
vector<Int> key[max_n];
vector<Int> value;
Int dp[1 << max_n]; // set

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

  for (int i = 0; i < (1 << N); i++) {
    dp[i] = TEN(10);
  }

  for (int i = 0; i < M; i++) {
    Int a, b;
    cin >> a >> b;
    value.push_back(a);
    for (int j = 0; j < b; j++) {
      int c;
      cin >> c;
      key[i].push_back(c);
    }
  }

  dp[0] = 0;
  for (int i = 0; i < (1 << N); i++) {
    for (int j = 0; j < M; j++) {
      int bit = i;
      for (int k = 0; k < key[j].size(); k++) {
        bit |= (1 << (key[j][k] - 1));
      }
      dp[bit] = min(dp[bit], dp[i] + value[j]);
    }
  }

  if (dp[(1 << N) - 1] == TEN(10))
    cout << -1 << endl;
  else
    cout << dp[(1 << N) - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int max_n = 15;
const int max_m = 1000 + 10;
vector<Int> key[max_m];
vector<Int> value;
Int dp[1 << max_n]; // set

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

  for (int i = 0; i < (1 << N); i++) {
    dp[i] = TEN(10);
  }

  for (int i = 0; i < M; i++) {
    Int a, b;
    cin >> a >> b;
    value.push_back(a);
    for (int j = 0; j < b; j++) {
      int c;
      cin >> c;
      key[i].push_back(c);
    }
  }

  dp[0] = 0;
  for (int i = 0; i < (1 << N); i++) {
    for (int j = 0; j < M; j++) {
      int bit = i;
      for (int k = 0; k < key[j].size(); k++) {
        bit |= (1 << (key[j][k] - 1));
      }
      dp[bit] = min(dp[bit], dp[i] + value[j]);
    }
  }

  if (dp[(1 << N) - 1] == TEN(10))
    cout << -1 << endl;
  else
    cout << dp[(1 << N) - 1] << endl;

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx9[9] = {0, 1, 0, -1, 1, -1, 1, 0, -1};
int dy9[9] = {0, 1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;
// << fixed << setprecision
// --------------------------------------
int n, m;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  vector<int> a(n);
  vector<int> t[m];

  REP(i, m) {
    int b;
    cin >> a[i] >> b;
    REP(j, b) {
      int c;
      cin >> c;
      c--;
      t[i].push_back(c);
    }
  }

  int dp[10000];
  REP(i, 10000) dp[i] = INF;
  dp[0] = 0;

  REP(i, m) {
    int h = 0;
    for (int u : t[i])
      h |= (1 << u);
    REP(j, 1 << n) {
      int now = j | h;
      dp[now] = min(dp[now], dp[j] + a[i]);
    }
  }

  cout << (dp[(1 << n) - 1] == INF ? -1 : dp[(1 << n) - 1]) << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx9[9] = {0, 1, 0, -1, 1, -1, 1, 0, -1};
int dy9[9] = {0, 1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;
// << fixed << setprecision
// --------------------------------------
int n, m;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  vector<int> a(m);
  vector<int> t[m];

  REP(i, m) {
    int b;
    cin >> a[i] >> b;
    REP(j, b) {
      int c;
      cin >> c;
      c--;
      t[i].push_back(c);
    }
  }

  int dp[10000];
  REP(i, 10000) dp[i] = INF;
  dp[0] = 0;

  REP(i, m) {
    int h = 0;
    for (int u : t[i])
      h |= (1 << u);
    REP(j, 1 << n) {
      int now = j | h;
      dp[now] = min(dp[now], dp[j] + a[i]);
    }
  }

  cout << (dp[(1 << n) - 1] == INF ? -1 : dp[(1 << n) - 1]) << endl;
}
Problem p02901 (C++)

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

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<pii> vpii;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n, m;
  cin >> n >> m;

  vi a(m), bits(m);
  vi s(1 << n, (1 << 30));
  rep(i, m) {
    cin >> a[i];
    int b;
    cin >> b;
    int bit = 0;
    rep(j, b) {
      int c;
      cin >> c;
      c--;
      bit |= 1 << c;
    }
    bits[i] = bit;
    s[bit] = min(s[bit], a[i]);
    // cout << bit << " " << a[i] << endl;
  }

  rep(k, m) {
    rep(i, m) {
      rep(j, 1 << n) if (s[j] != (1 << 30)) {
        // cout << i << " " << (j | bits[i]) << " " << s[j] << " " << a[i] <<
        // endl;
        s[j | bits[i]] = min(s[j | bits[i]], s[j] + a[i]);
      }
    }
  }

  if (s[(1 << n) - 1] == (1 << 30)) {
    cout << "-1" << endl;
  } else {
    cout << s[(1 << n) - 1] << endl;
  }

  return 0;
}


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

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<pii> vpii;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n, m;
  cin >> n >> m;

  vi a(m), bits(m);
  vi s(1 << n, (1 << 30));
  rep(i, m) {
    cin >> a[i];
    int b;
    cin >> b;
    int bit = 0;
    rep(j, b) {
      int c;
      cin >> c;
      c--;
      bit |= 1 << c;
    }
    bits[i] = bit;
    s[bit] = min(s[bit], a[i]);
    // cout << bit << " " << a[i] << endl;
  }

  rep(i, m) {
    rep(j, 1 << n) if (s[j] != (1 << 30)) {
      // cout << i << " " << (j | bits[i]) << " " << s[j] << " " << a[i] <<
      // endl;
      s[j | bits[i]] = min(s[j | bits[i]], s[j] + a[i]);
    }
  }

  if (s[(1 << n) - 1] == (1 << 30)) {
    cout << "-1" << endl;
  } else {
    cout << s[(1 << n) - 1] << endl;
  }

  return 0;
}
Problem p02901 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define M 1000000000000000000LL
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
typedef vector<ll> vll;
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;
}

int main() {
  ll n, m, x;
  cin >> n >> m;
  vll a(m), b(m);
  vector<vll> c(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    // push_backよりemplace_backの方が速いらしい。改宗します。
    rep(j, b[i]) cin >> x;
    c[i].emplace_back(x);
  }
  vll dp(1 << n, M);
  dp[0] = 0;
  rep(i, m) {
    ll now = 0;
    // get 'OR' of all c[i][j]
    rep(j, b[i]) now |= 1 << (c[i][j] - 1);
    // cout << bitset<10>(now)<< endl;
    rep(bit, 1 << n) chmin(dp[bit | now], dp[bit] + a[i]);
  }
  if (dp[(1 << n) - 1] == M)
    dp[(1 << n) - 1] = -1;
  cout << dp[(1 << n) - 1] << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define M 1000000000000000000LL
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
typedef vector<ll> vll;
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;
}

int main() {
  ll n, m, x;
  cin >> n >> m;
  vll a(m), b(m);
  vector<vll> c(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    // push_backよりemplace_backの方が速いらしい。改宗します。
    rep(j, b[i]) {
      cin >> x;
      c[i].emplace_back(x);
    }
  }
  vll dp(1 << n, M);
  dp[0] = 0;
  rep(i, m) {
    ll now = 0;
    // get 'OR' of all c[i][j]
    rep(j, b[i]) now |= 1 << (c[i][j] - 1);
    // cout << bitset<10>(now)<< endl;
    rep(bit, 1 << n) chmin(dp[bit | now], dp[bit] + a[i]);
  }
  if (dp[(1 << n) - 1] == M)
    dp[(1 << n) - 1] = -1;
  cout << dp[(1 << n) - 1] << endl;
}
Problem p02901 (C++)

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

using namespace std;
int n, m, a[310], dp[310][(1 << 12) + 10];
vector<int> v[310];
const int Inf = 1e9;

int Rec(int i, int Mask) {

  if (i == m)
    return (Mask == ((1 << n) - 1) ? 0 : Inf);

  if (dp[i][Mask] != -1)
    return dp[i][Mask];

  int Res = Inf, temp = 0;
  for (auto j : v[i])
    temp |= (1 << j);
  Res = min(Res, a[i] + Rec(i + 1, Mask | temp));
  Res = min(Res, Rec(i + 1, Mask));

  return dp[i][Mask] = Res;
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int b;
    cin >> a[i] >> b;
    for (int j = 0; j < b; j++) {
      int c;
      cin >> c;
      v[i].push_back(c - 1);
    }
  }
  cout << (Rec(0, 0) == Inf ? -1 : Rec(0, 0));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define ll long long

using namespace std;
int n, m, a[1010], dp[1010][(1 << 12) + 10];
vector<int> v[1010];
const int Inf = 1e9;

int Rec(int i, int Mask) {

  if (i == m)
    return (Mask == ((1 << n) - 1) ? 0 : Inf);

  if (dp[i][Mask] != -1)
    return dp[i][Mask];

  int Res = Inf, temp = 0;
  for (auto j : v[i])
    temp |= (1 << j);
  Res = min(Res, a[i] + Rec(i + 1, Mask | temp));
  Res = min(Res, Rec(i + 1, Mask));

  return dp[i][Mask] = Res;
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int b;
    cin >> a[i] >> b;
    for (int j = 0; j < b; j++) {
      int c;
      cin >> c;
      v[i].push_back(c - 1);
    }
  }
  cout << (Rec(0, 0) == Inf ? -1 : Rec(0, 0));
  return 0;
}