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

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
int n;
vector<vi> t;
vi v;
int dp[105][1 << 17];

int solve(int i, int j) {
  if (i == n)
    return 0;
  if (dp[i][j] >= 0)
    return dp[i][j];
  int tmp = j;
  bool f = true;
  REP(k, t[i].size()) {
    if (((tmp >> t[i][k]) & 1) == 0)
      tmp |= (1 << t[i][k]);
    else
      f = false;
  }
  int res;
  if (f)
    res = max(solve(i + 1, j), solve(i + 1, tmp) + v[i]);
  else
    res = solve(i + 1, j);
  return dp[i][j] = res;
}

int main() {
  while (cin >> n, n) {
    t.clear();
    t.clear();
    MS(dp, -1);
    t.resize(n);
    v.resize(n);
    REP(i, n) {
      int k;
      cin >> k >> v[i];
      REP(j, k) {
        int a, b;
        cin >> a >> b;
        while (a != b) {
          t[i].push_back(a - 6);
          a++;
        }
      }
    }
    cout << solve(0, 0) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
int n;
vector<vi> t;
int v[105];
ll dp[105][1 << 17];

ll solve(int i, int j) {
  if (i == n)
    return 0;
  if (dp[i][j] >= 0)
    return dp[i][j];
  int tmp = j;
  bool f = true;
  REP(k, t[i].size()) {
    if (((tmp >> t[i][k]) & 1) == 0)
      tmp |= (1 << t[i][k]);
    else
      f = false;
  }
  ll res;
  if (f)
    res = max(solve(i + 1, j), solve(i + 1, tmp) + v[i]);
  else
    res = solve(i + 1, j);
  return dp[i][j] = res;
}

int main() {
  while (cin >> n, n) {
    MS(dp, -1);
    t.resize(n);
    REP(i, n) {
      int k;
      cin >> k >> v[i];
      REP(j, k) {
        int a, b;
        cin >> a >> b;
        while (a != b) {
          t[i].push_back(a - 6);
          a++;
        }
      }
    }
    cout << solve(0, 0) << endl;
  }
  return 0;
}