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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

struct Guy {
  int sc;
  int L;
  bool ok;
};

typedef long long ll;
int n;
Guy guys[101];

ll dp[2][1 << 17];

ll dfs(int pos, int s) {
  if (pos == n)
    return 0;
  if (dp[pos][s] != -1)
    return dp[pos][s];
  ll res = 0;
  // ツ債。ツ嘉アツ偲ヲツづゥツつゥツ氾崢つゥ
  // no get
  res = max(res, dfs(pos + 1, s));
  // get
  if (guys[pos].ok) {
    bool ok = true;
    for (int i = 0; i < 17; i++) {
      if (((s >> i) & 1) && ((guys[pos].sc >> i) & 1)) {
        ok = false;
        break;
      }
    }
    if (ok) {
      res = max(res, dfs(pos + 1, s | guys[pos].sc) + guys[pos].L);
    }
  }
  return dp[pos][s] = res;
}

int main() {
  while (cin >> n && n != 0) {
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m >> guys[i].L;
      guys[i].sc = 0;
      guys[i].ok = true;
      for (int j = 0; j < m; j++) {
        int s, e;
        cin >> s >> e;
        s -= 6;
        e -= 6;
        for (int k = s; k < e; k++) {
          if (!((guys[i].sc >> k) & 1)) {
            guys[i].sc |= (1 << k);
          } else {
            guys[i].ok = false;
          }
        }
      }
    }
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < (1 << 17); j++)
        dp[i][j] = 0;
    for (int i = n - 1; i >= 0; i--) {
      int cur = (i + 1) % 2;
      int nxt = (i) % 2;
      for (int j = 0; j < (1 << 17); j++)
        dp[nxt][j] = 0;
      for (int j = 0; j < (1 << 17); j++) {
        dp[nxt][j] = dp[cur][j];
        if (guys[i].ok) {
          bool ok = (j & guys[i].sc) ? 0 : 1;
          //					for(int k=0;k<17;k++){
          //						if(((j>>k)&1)&&((guys[i].sc>>k)&1)){
          //							ok=false;
          //							break;
          //						}
          //					}
          if (ok) {
            dp[nxt][j] = max(dp[nxt][j], dp[cur][j | guys[i].sc] + guys[i].L);
          }
        }
      }
    }
    ll res = dp[0][0];
    // ll res=dfs(0,0);
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

struct Guy {
  int sc;
  int L;
  bool ok;
};

typedef long long ll;
int n;
Guy guys[101];

ll dp[2][1 << 17];

ll dfs(int pos, int s) {
  if (pos == n)
    return 0;
  if (dp[pos][s] != -1)
    return dp[pos][s];
  ll res = 0;
  // ツ債。ツ嘉アツ偲ヲツづゥツつゥツ氾崢つゥ
  // no get
  res = max(res, dfs(pos + 1, s));
  // get
  if (guys[pos].ok) {
    bool ok = true;
    for (int i = 0; i < 17; i++) {
      if (((s >> i) & 1) && ((guys[pos].sc >> i) & 1)) {
        ok = false;
        break;
      }
    }
    if (ok) {
      res = max(res, dfs(pos + 1, s | guys[pos].sc) + guys[pos].L);
    }
  }
  return dp[pos][s] = res;
}

int main() {
  while (cin >> n && n != 0) {
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m >> guys[i].L;
      guys[i].sc = 0;
      guys[i].ok = true;
      for (int j = 0; j < m; j++) {
        int s, e;
        cin >> s >> e;
        s -= 6;
        e -= 6;
        for (int k = s; k < e; k++) {
          if (!((guys[i].sc >> k) & 1)) {
            guys[i].sc |= (1 << k);
          } else {
            guys[i].ok = false;
          }
        }
      }
    }
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < (1 << 17); j++)
        dp[i][j] = 0;
    for (int i = n - 1; i >= 0; i--) {
      int cur = (i + 1) % 2;
      int nxt = (i) % 2;
      for (int j = 0; j < (1 << 17); j++)
        dp[nxt][j] = 0;
      for (int j = 0; j < (1 << 17); j++) {
        dp[nxt][j] = dp[cur][j];
        if (guys[i].ok) {
          bool ok = true;
          for (int k = 0; k < 17; k++) {
            if (((j >> k) & 1) && ((guys[i].sc >> k) & 1)) {
              ok = false;
              break;
            }
          }
          if (ok) {
            dp[nxt][j] = max(dp[nxt][j], dp[cur][j | guys[i].sc] + guys[i].L);
          }
        }
      }
    }
    ll res = dp[0][0];
    // ll res=dfs(0,0);
    cout << res << endl;
  }

  return 0;
}