Problem p00618 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int n, u;
  while (scanf("%d%d", &n, &u), n | u) {
    int c[32], r[32];
    for (int i = 0; i < n; ++i) {
      int k;
      scanf("%d%d", c + i, &k);
      r[i] = 0;
      while (k--) {
        int t;
        scanf("%d", &t);
        r[i] |= 1 << t;
      }
    }
    if (u == 0) {
      puts("0");
      continue;
    }

    int res = n;
    for (int bits = 1; bits < n; ++bits) {
      int i = (1 << bits) - 1;
      do {
        bool ok = true;
        int s = 0;
        for (int j = 0; j < n; ++j) {
          if (i >> j & 1) {
            if ((i & r[j]) != r[j]) {
              ok = false;
              break;
            } else
              s += c[j];
          }
        }
        if (ok) {
          // int s = 0;
          // for (int j = 0; j < n; ++j)
          //	if (i >> j & 1)
          //		s += c[j];
          if (s >= u) {
            res = bits;
            goto end;
          }
        }

        // combination
        int leastBit = i & -i;
        int mostBits = i + leastBit;
        int leastSequentialBits = i & ~mostBits;
        i = mostBits | ((leastSequentialBits / leastBit) >> 1);
      } while (i < 1 << n);
    }
  end:
    printf("%d\\n", res);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int n, u;
  while (scanf("%d%d", &n, &u), n | u) {
    int c[32], r[32];
    for (int i = 0; i < n; ++i) {
      int k;
      scanf("%d%d", c + i, &k);
      r[i] = 0;
      while (k--) {
        int t;
        scanf("%d", &t);
        r[i] |= 1 << t;
      }
    }
    if (u == 0) {
      puts("0");
      continue;
    }

    int res = n;
    for (int bits = 1; bits < n; ++bits) {
      int i = (1 << bits) - 1;
      do {
        bool ok = true;
        int s = 0;
        for (int j = 0; j < n; ++j) {
          if (i >> j & 1) {
            if ((i & r[i]) != r[i]) {
              ok = false;
              break;
            } else
              s += c[i];
          }
        }
        if (ok) {
          // int s = 0;
          // for (int j = 0; j < n; ++j)
          //	if (i >> j & 1)
          //		s += c[j];
          if (s >= u) {
            res = bits;
            goto end;
          }
        }

        // combination
        int leastBit = i & -i;
        int mostBits = i + leastBit;
        int leastSequentialBits = i & ~mostBits;
        i = mostBits | ((leastSequentialBits / leastBit) >> 1);
      } while (i < 1 << n);
    }
  end:
    printf("%d\\n", res);
  }
}