Problem p01438 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll << 50;

template <typename T> void printv(vector<T> const &s) {
  REP(i, SZ(s)) {
    cout << s[i];
    if (i == SZ(s) - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

ll solve(const vector<bitset<23>> &sch, vector<ll> &cost, int n, int k,
         bitset<23> now, ll c) {
  if (n == k) {
    return c;
  } else {
    if ((sch[k] & now).any()) {
      return solve(sch, cost, n, k + 1, now, c);
    } else {
      return max(solve(sch, cost, n, k + 1, now, c),
                 solve(sch, cost, n, k + 1, now | sch[k], c + cost[k]));
    }
  }
}

int main() {
  cin.tie(0);
  cout << fixed << setprecision(10);

  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<bitset<23>> sch(n);
    vector<ll> cost(n);
    for (int i = 0; i < n; ++i) {
      int m;
      cin >> m >> cost[i];
      auto bs = bitset<23>();
      for (int j = 0; j < m; ++j) {
        int l, r;
        cin >> l >> r;
        bs |= bitset<23>(((1ll << r) - 1) - ((1ll << l) - 1));
      }
      sch[i] = bs;
    }

    auto now = bitset<23>();
    cout << solve(sch, cost, n, 0, now, 0) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll << 50;

template <typename T> void printv(vector<T> const &s) {
  REP(i, SZ(s)) {
    cout << s[i];
    if (i == SZ(s) - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

/*
  最悪ケースはmax(n)(=100)人についてmax(m)(=16)+1通りずつ見る時
  bitset::any はO(bitset.size())
  bitset::operator& は

  n: 男性の人数
  k: 今見ている男性
  guy: guy[i][j] = i番目の男性についてのj番目のデート候補
  cost: cost[i] = i番目の男性の満足度
  sum: 一日デートしたときの満足度の総和
  bs: 一日の予定
 */
ll solve(int n, int k, const vector<vector<pair<int, int>>> &guy,
         const vector<ll> &cost, ll sum, ll bs) {
  if (n == k) {
    return sum;
  } else {
    int sz = guy[k].size();
    ll res = solve(n, k + 1, guy, cost, sum, bs);
    for (int i = 0; i < sz; ++i) {
      int s = guy[k][i].first, e = guy[k][i].second;
      ll tmp = ((1ll << e) - 1) - ((1ll << s) - 1);
      // すでにある予定と[s, e)にカブりがあれば実行不可能
      // そうでなければsumにcost[k]を足して、スケジュールに[s, e)を追加
      if ((bs & tmp) != 0) {
        // cout << k << ":" << i << ":" << bs << ":" << tmp << endl;
        return 0;
      } else {
        res = max(res, solve(n, k + 1, guy, cost, sum + cost[k], bs | tmp));
      }
    }
    return res;
  }
}

int main() {
  cin.tie(0);
  cout << fixed << setprecision(10);

  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<vector<pair<int, int>>> guy(n); // i番目とのデート時間候補
    vector<ll> cost(n);                    // i番目のコスト
    for (int i = 0; i < n; ++i) {
      int m;
      ll l;
      cin >> m >> l;
      cost[i] = l;
      for (int j = 0; j < m; ++j) {
        int s, e;
        cin >> s >> e;
        guy[i].push_back(make_pair(s, e));
      }
    }

    ll sum = 0;
    ll bs = 0;
    cout << solve(n, 0, guy, cost, sum, bs) << endl;
  }
}