alexjercan/bugnet huggingface dataset:
Problem p01438 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

  int N;
  while (cin >> N, N) {
    V L(N);
    VV want(N, V(16, 0));
    rep(i, N) {
      int M;
      cin >> M >> L[i];
      rep(j, M) {
        int s, t;
        cin >> s >> t;
        rep2(k, s, t) want[i][k - 6] = 1;
      }
    }

    V dp(1 << 16, 0);
    rep(s, 1 << 16) {
      rep(i, N) {
        bool ok = true;
        rep(j, 16) {
          if (((s >> j) & 1) && want[i][j]) {
            ok = false;
            break;
          }
        }
        if (ok) {
          int ns = s;
          rep(j, 16) { ns |= want[i][j] * (1 << j); }
          dp[ns] = max(dp[ns], dp[s] + L[i]);
        }
      }
    }

    cout << *max_element(all(dp)) << endl;
  }
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

  int N;
  while (cin >> N, N) {
    V L(N);
    VV want(N, V(16, 0));
    rep(i, N) {
      int M;
      cin >> M >> L[i];
      rep(j, M) {
        int s, t;
        cin >> s >> t;
        rep2(k, s, t) want[i][k] = 1;
      }
    }

    V dp(1 << 16, 0);
    rep(s, 1 << 16) {
      rep(i, N) {
        bool ok = true;
        rep(j, 16) {
          if (((s >> j) & 1) && want[i][j]) {
            ok = false;
            break;
          }
        }
        if (ok) {
          int ns = s;
          rep(j, 16) { ns |= want[i][j] * (1 << j); }
          dp[ns] = max(dp[ns], dp[s] + L[i]);
        }
      }
    }

    cout << *max_element(all(dp)) << endl;
  }
}
Problem p01438 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int n;
int l[111];
int times[111];

ll dp[(1 << 17)];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    memset(times, 0, sizeof(times));
    rep(i, n) {
      int m;
      cin >> m >> l[i];
      rep(j, m) {
        int s, e;
        cin >> s >> e;
        s -= 6;
        e -= 6;
        times[i] |= (1 << e) - (1 << s);
      }
    }
    memset(dp, -1, sizeof(dp));
    dp[0] = 0;
    ll res = 0;
    rep(S, 1 << 17) {
      if (dp[S] == -1)
        continue;
      rep(i, n) {
        if ((S & times[i]) == 0)
          maxch(dp[S | times[i]], dp[S] + l[i]);
      }
    }
    rep(S, 1 << 17) maxch(res, dp[S]);
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int n;
int l[111];
int times[111];

ll dp[101][(1 << 17)];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    memset(times, 0, sizeof(times));
    rep(i, n) {
      int m;
      cin >> m >> l[i];
      rep(j, m) {
        int s, e;
        cin >> s >> e;
        s -= 6;
        e -= 6;
        repl(k, s, e) { times[i] |= (1 << k); }
      }
    }
    memset(dp, 0, sizeof(dp));
    rep(i, n) {
      rep(S, 1 << 17) {
        if ((S & times[i]) == 0)
          maxch(dp[i + 1][S | times[i]], dp[i][S] + l[i]);
      }
    }
    ll res = 0;
    rep(S, 1 << 17) maxch(res, dp[n][S]);
    cout << res << endl;
  }
  return 0;
}
Problem p01438 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>
using namespace std;

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

using i64 = long long;
void chmax(i64 &l, i64 r) {
  if (l < r)
    l = r;
}

int main() {
  const int k = 16;
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<i64> dp(1 << k, -1);
    dp[0] = 0;
    while (n--) {
      int m;
      i64 x;
      cin >> m >> x;
      int mask = 0;
      while (m--) {
        int st, ed;
        cin >> st >> ed;
        st -= 6;
        ed -= 6;
        for (int i = st; i < ed; i++)
          mask ^= (1 << i);
      }
      for (int bit = (1 << k) - 1; bit >= 0; bit--) {
        if ((bit & mask) == mask and dp[bit ^ mask] >= 0) {
          chmax(dp[bit], dp[bit ^ mask] + x);
        }
      }
    }
    i64 mx = 0;
    rep(bit, (1 << k)) chmax(mx, dp[bit]);
    cout << mx << endl;
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>
using namespace std;

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

using i64 = long long;
void chmax(i64 &l, i64 r) {
  if (l < r)
    l = r;
}

int main() {
  const int k = 16;
  while (true) {
    int n;
    cin >> n;
    vector<i64> dp(1 << k, -1);
    dp[0] = 0;
    while (n--) {
      int m;
      i64 x;
      cin >> m >> x;
      vector<int> masks;
      while (m--) {
        int st, ed;
        cin >> st >> ed;
        st -= 6;
        ed -= 6;
        int mask = 0;
        for (int i = st; i < ed; i++)
          mask ^= (1 << i);
        masks.push_back(mask);
      }
      for (int bit = (1 << k); bit >= 0; bit--) {
        for (int mask : masks) {
          if ((bit & mask) == mask and dp[bit ^ mask] >= 0) {
            chmax(dp[bit], dp[bit ^ mask] + x);
          }
        }
      }
    }
    i64 mx = 0;
    rep(bit, (1 << k)) chmax(mx, dp[bit]);
    cout << mx << endl;
  }

  return 0;
}
Problem p01438 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int N;
int L[102];
int mask[102];
ll dp[102][1 << 16];

ll dfs(int a, int b) {
  if (a == N)
    return 0;
  if (dp[a][b] != -1)
    return dp[a][b];

  ll res = 0;
  res = max(res, dfs(a + 1, b));
  if ((b & mask[a]) == 0)
    res = max(res, dfs(a + 1, b | mask[a]) + L[a]);

  return dp[a][b] = res;
}

void solve() {
  rep(i, 102) mask[i] = 0;
  memset(dp, -1, sizeof(dp));
  rep(i, N) {
    int m;
    cin >> m >> L[i];
    rep(j, m) {
      int s, e;
      cin >> s >> e;
      s -= 6;
      e -= 6;
      for (int k = s; k < e; k++)
        mask[i] |= 1 << k;
    }
  }
  cout << dfs(0, 0) << endl;
}

int main() {
  ios::sync_with_stdio(false);
  while (cin >> N) {
    if (!N)
      break;
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int N;
int L[102];
int mask[102];
ll dp[102][1 << 16];

ll dfs(int a, int b) {
  if (a == N)
    return 0;
  if (dp[a][b] != -1)
    return dp[a][b];

  ll res = 0;
  res = max(res, dfs(a + 1, b));
  if ((b & mask[a]) == 0)
    res = max(res, dfs(a + 1, b | mask[a]) + L[a]);

  return dp[a][b] = res;
}

void solve() {
  rep(i, 102) mask[i] = 0;
  memset(dp, -1, sizeof(dp));
  rep(i, N) {
    int m;
    cin >> m >> L[i];
    rep(j, m) {
      int s, e;
      cin >> s >> e;
      s -= 6;
      e -= 6;
      for (int k = s; k < e; k++)
        mask[k] |= 1 << k;
    }
  }
  cout << dfs(0, 0) << endl;
}

int main() {
  while (cin >> N) {
    if (!N)
      break;
    solve();
  }
  return 0;
}
Problem p01438 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Man {
  int l;
  int bit;
};
int N, M, a, b;
int main() {
  while (cin >> N, N) {
    vector<Man> D(N);
    for (int i = 0; i < N; i++) {
      cin >> M >> D[i].l;
      for (int j = 0; j < M; j++) {
        cin >> a >> b;
        for (int k = a - 6; k < b - 6; k++)
          D[i].bit |= (1 << k);
      }
    }
    vector<vector<int>> dp(N + 1, vector<int>(1 << 16));
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j < 1 << 16; j++) {
        dp[i][j] = dp[i - 1][j];
        if (~j & D[i - 1].bit)
          continue;
        dp[i][j] = max(dp[i][j], dp[i - 1][j - D[i - 1].bit] + D[i - 1].l);
      }
    }
    cout << dp[N][(1 << 16) - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Man {
  int l;
  vector<int> bit;
};
int N, M, a, b;
int main() {
  while (cin >> N, N) {
    vector<Man> D(N);
    for (int i = 0; i < N; i++) {
      cin >> M >> D[i].l;
      D[i].bit.resize(N);
      for (int j = 0; j < M; j++) {
        cin >> a >> b;
        for (int k = a - 6; k < b - 6; k++)
          D[i].bit[j] += (1 << k);
      }
    }
    vector<vector<int>> dp(N + 1, vector<int>(1 << 16));
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j < 1 << 16; j++) {
        dp[i][j] = dp[i - 1][j];
        for (int k : D[i - 1].bit) {
          if (~j & k)
            continue;
          dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + D[i - 1].l);
        }
      }
    }
    cout << dp[N][(1 << 16) - 1] << endl;
  }
  return 0;
}
Problem p01438 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int dp[102][1 << 16], x[102][16];
int N, M, S, E, L;
int main() {
  while (true) {
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = 0;
        x[i][j % 16] = 0;
      }
    }
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> M >> L;
      int V = 0;
      for (int j = 0; j < M; j++) {
        cin >> S >> E;
        S -= 6;
        E -= 6;
        for (int k = S; k < E; k++) {
          x[i][k] = 1;
          V += (1 << k);
        }
      }
      for (int j = 0; j < (1 << 16); j++)
        dp[i + 1][j] = dp[i][j];
      for (int j = 0; j < (1 << 16); j++) {
        int bit[16], T;
        for (int k = 0; k < 16; k++)
          bit[k] = (j / (1 << k)) % 2;
        int OK = 0;
        for (int k = 15; k >= 0; k--) {
          if (x[i][k] == 1 && bit[k] == 1) {
            j += (1 << k) - 1;
            goto E;
          }
        }
        T = j | V;
        dp[i + 1][T] = max(dp[i + 1][T], dp[i][j] + L);
      E:;
      }
    }
    int maxn = 0;
    for (int i = 0; i < (1 << 16); i++)
      maxn = max(maxn, dp[N][i]);
    cout << maxn << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int dp[102][1 << 16], x[102][16];
int N, M, S, E, L;
int main() {
  while (true) {
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = 0;
        x[i][j % 16] = 0;
      }
    }
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> M >> L;
      int V = 0;
      for (int j = 0; j < M; j++) {
        cin >> S >> E;
        S -= 6;
        E -= 6;
        for (int k = S; k < E; k++) {
          x[i][k] = 1;
          V += (1 << k);
        }
      }
      for (int j = 0; j < (1 << 16); j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        int bit[16], T;
        for (int k = 0; k < 16; k++)
          bit[k] = (j / (1 << k)) % 2;
        int OK = 0;
        for (int k = 0; k < 16; k++) {
          if (x[i][k] == 1 && bit[k] == 1)
            goto E;
        }
        T = j | V;
        dp[i + 1][T] = max(dp[i + 1][T], dp[i][j] + L);
      E:;
      }
    }
    int maxn = 0;
    for (int i = 0; i < (1 << 16); i++)
      maxn = max(maxn, dp[N][i]);
    cout << maxn << endl;
  }
  return 0;
}
Problem p01438 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

ll dp[101][65536];
int main() {
  while (1) {
    fill(dp[0], dp[100] + 65536, 0);

    int n;
    cin >> n;
    if (!n)
      break;
    vector<ll> L(n);
    vector<vector<int>> S, E;
    loop(n, i) {
      int m;
      cin >> m >> L[i];
      vector<int> s(m), e(m);
      loop(m, j) cin >> s[j] >> e[j];
      S.push_back(s);
      E.push_back(e);
    }

    loop(n, i) {
      int mask = 0;
      loop(S[i].size(), k) {
        for (int l = S[i][k]; l < E[i][k]; l++) {
          mask ^= (1 << (l - 6));
        }
      }
      loop(1 << 16, j) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        if ((j & mask) == 0) {
          dp[i + 1][j ^ mask] = max(dp[i + 1][j ^ mask], dp[i][j] + L[i]);
        }
      }
    }

    ll ans = 0;
    loop(1 << 16, i) ans = max(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

ll dp[101][65536];
int main() {
  while (1) {
    fill(dp[0], dp[100] + 65536, 0);

    int n;
    cin >> n;
    vector<ll> L(n);
    vector<vector<int>> S, E;
    loop(n, i) {
      int m;
      cin >> m >> L[i];
      vector<int> s(m), e(m);
      loop(m, j) cin >> s[j] >> e[j];
      S.push_back(s);
      E.push_back(e);
    }

    loop(n, i) {
      int mask = 0;
      loop(S[i].size(), k) {
        for (int l = S[i][k]; l < E[i][k]; l++) {
          mask ^= (1 << (l - 6));
        }
      }
      loop(1 << 16, j) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        if ((j & mask) == 0) {
          dp[i + 1][j ^ mask] = max(dp[i + 1][j ^ mask], dp[i][j] + L[i]);
        }
      }
    }

    ll ans = 0;
    loop(1 << 16, i) ans = max(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p01438 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
int n;
int m[101];
ll l[101];
int s[101][17], e[101][17];
ll dp[2][1 << 17];

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %lld", &m[i], &l[i]);
      for (int j = 0; j < m[i]; j++) {
        scanf("%d %d", &s[i][j], &e[i][j]);
        s[i][j] -= 6;
        e[i][j] -= 6;
      }
    }
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < (1 << 17); j++) {
        dp[i][j] = -1;
      }
    }
    dp[0][0] = 0;
    ll res = 0;
    for (int i = 0; i < n; i++) {
      memset(dp[(i + 1) % 2], -1, sizeof(dp[(i + 1) % 2]));
      for (int j = 0; j < (1 << 17); j++) {
        if (dp[i % 2][j] >= 0) {
          dp[(i + 1) % 2][j] = max(dp[(i + 1) % 2][j], dp[i % 2][j]);
          res = max(dp[(i + 1) % 2][j], res);
          bool flag = true;
          for (int k = 0; k < m[i]; k++) {
            for (int l = s[i][k]; l < e[i][k]; l++) {
              if (j >> l & 1)
                flag = false;
            }
          }
          if (flag) {
            int nj = j;
            for (int k = 0; k < m[i]; k++) {
              for (int l = s[i][k]; l < e[i][k]; l++) {
                nj = nj | (1 << l);
              }
            }
            dp[(i + 1) % 2][nj] = max(dp[(i + 1) % 2][nj], dp[i % 2][j] + l[i]);
            res = max(dp[(i + 1) % 2][nj], res);
          }
        }
      }
    }
    printf("%lld\\n", res);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
int n;
int m[101];
ll l[101];
int s[101][17], e[101][17];
ll dp[101][1 << 17];

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %lld", &m[i], &l[i]);
      for (int j = 0; j < m[i]; j++) {
        scanf("%d %d", &s[i][j], &e[i][j]);
        s[i][j] -= 6;
        e[i][j] -= 6;
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < (1 << 17); j++) {
        dp[i][j] = -1;
      }
    }
    dp[0][0] = 0;
    ll res = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 17); j++) {
        if (dp[i][j] >= 0) {
          dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
          res = max(dp[i + 1][j], res);
          bool flag = true;
          for (int k = 0; k < m[i]; k++) {
            for (int l = s[i][k]; l < e[i][k]; l++) {
              if (j >> l & 1)
                flag = false;
            }
          }
          if (flag) {
            int nj = j;
            for (int k = 0; k < m[i]; k++) {
              for (int l = s[i][k]; l < e[i][k]; l++) {
                nj = nj | (1 << l);
              }
            }
            dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + l[i]);
            res = max(dp[i + 1][nj], res);
          }
        }
      }
    }
    printf("%lld\\n", res);
  }
  return 0;
}
Problem p01438 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, dp[1 << 16];

  while (cin >> N, N) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; i++) {
      int M, L;
      cin >> M >> L;
      int bitMask = 0;
      while (M--) {
        int S, E;
        cin >> S >> E;
        S -= 6, E -= 6;
        bitMask |= (1 << E) - (1 << S);
      }
      for (int j = 0; j < 1 << 16; j++) {
        if (!(bitMask & j)) {
          dp[bitMask | j] = max(dp[bitMask | j], dp[j] + L);
        }
      }
    }
    cout << dp[(1 << 16) - 1] << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  static int dp[1 << 22];

  while (cin >> N, N) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; i++) {
      int M, L;
      cin >> M >> L;
      int bitMask = 0;
      while (M--) {
        int S, E;
        cin >> S >> E;
        bitMask |= (1 << E) - (1 << S);
      }
      for (int j = 0; j < 1 << 22; j++) {
        if (!(bitMask & j)) {
          dp[bitMask | j] = max(dp[bitMask | j], dp[j] + L);
        }
      }
    }
    cout << dp[(1 << 22) - 1] << endl;
  }
}
Problem p01438 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define N 100
#define M 16
using namespace std;
typedef long long ll;
typedef struct data {
  int m, l, s[M], e[M], bit;
} dat;

int main() {
  int n;
  ll ans, dp[(1 << 16)];
  dat d[N];
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> d[i].m >> d[i].l;
      d[i].bit = 0;
      for (int j = 0; j < d[i].m; j++) {
        cin >> d[i].s[j] >> d[i].e[j];
        for (int k = d[i].s[j] - 6; k < d[i].e[j] - 6; k++)
          d[i].bit = d[i].bit | (1 << k);
      }
    }
    for (int i = 0; i < (1 << 16); i++)
      dp[i] = -1;
    dp[0] = ans = 0;
    for (int i = 0; i < (1 << 16); i++) {
      if (dp[i] == -1)
        continue;
      for (int j = 0; j < n; j++) {
        if ((i & d[j].bit) == 0) {
          dp[i | d[j].bit] = max(dp[i | d[j].bit], dp[i] + d[j].l);
          ans = max(ans, dp[i | d[j].bit]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define N 100
#define M 16
using namespace std;
typedef long long ll;
typedef struct data {
  int m, l, s[M], e[M];
} dat;

int main() {
  int n;
  bool used[23];
  dat d[N];
  ll ans;
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> d[i].m >> d[i].l;
      for (int j = 0; j < d[i].m; j++)
        cin >> d[i].s[j] >> d[i].e[j];
    }
    ans = 0;
    for (int i = 0; i < (1 << n); i++) {
      ll sum = 0;
      int flag = 0;
      for (int j = 6; j < 23; j++)
        used[j] = false;
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k = 0; k < d[j].m; k++) {
            for (int p = d[j].s[k]; p < d[j].e[k]; p++) {
              if (!used[p])
                used[p] = true;
              else {
                flag = 1;
                break;
              }
            }
            if (flag)
              break;
          }
          if (flag)
            break;
          else
            sum += d[j].l;
        }
      }
      if (!flag)
        ans = max(ans, sum);
    }
    cout << ans << endl;
  }
  return 0;
}