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

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int t[105], val[105], dp[1 << 16][105];
signed main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    REP(i, n) {
      int m, l;
      cin >> m >> l;
      val[i] = l;
      t[i] = 0;
      REP(j, m) {
        int s, e;
        cin >> s >> e;
        FOR(k, s, e) t[i] |= 1 << (k - 6);
      }
    }

    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    REP(i, n) {
      REP(j, 1 << 16) {
        if (dp[j][i] < 0)
          continue;
        // cout << i << " " << bitset<16>(j) << endl;
        dp[j][i + 1] = max(dp[j][i + 1], dp[j][i]);
        // t[i] ??? j ???????????????????????????
        bool flag = true;
        REP(k, 16) {
          if (((t[i] >> k) & 1) && ((j >> k) & 1)) {
            flag = false;
            // cout << "false" << k << endl;
          }
        }
        // cout << flag << endl;
        if (flag) {
          // cout << bitset<16>(j|t[i]) << " " << dp[j|t[i]][i+1] << " " <<
          // dp[j][i] + val[i] << endl;
          dp[j | t[i]][i + 1] = max(dp[j | t[i]][i + 1], dp[j][i] + val[i]);
        }
      }
    }

    ll ret = 0;
    REP(i, 1 << 16) chmax(ret, dp[i][n]);
    cout << ret << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int t[105], val[105], dp[1 << 17][105];
signed main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    REP(i, n) {
      int m, l;
      cin >> m >> l;
      val[i] = l;
      t[i] = 0;
      REP(j, m) {
        int s, e;
        cin >> s >> e;
        FOR(k, s, e) t[i] |= 1 << (k - 6);
      }
    }

    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    REP(i, n) {
      REP(j, 1 << 16) {
        if (dp[j][i] < 0)
          continue;
        // cout << i << " " << bitset<16>(j) << endl;
        dp[j][i + 1] = max(dp[j][i + 1], dp[j][i]);
        // t[i] ??? j ???????????????????????????
        bool flag = true;
        REP(k, 16) {
          if (((t[i] >> k) & 1) && ((j >> k) & 1)) {
            flag = false;
            // cout << "false" << k << endl;
          }
        }
        // cout << flag << endl;
        if (flag) {
          // cout << bitset<16>(j|t[i]) << " " << dp[j|t[i]][i+1] << " " <<
          // dp[j][i] + val[i] << endl;
          dp[j | t[i]][i + 1] = max(dp[j | t[i]][i + 1], dp[j][i] + val[i]);
        }
      }
    }

    ll ret = 0;
    REP(i, 1 << 16) chmax(ret, dp[i][n]);
    cout << ret << endl;
  }

  return 0;
}