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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

const static int tx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const static int ty[] = {-1, -1, 0, 1, 1, 1, 0, -1};

static const double EPS = 1e-8;

class Data {
public:
  Data() : satisfaction_vol(0), mask(0) {}
  ll satisfaction_vol;
  int mask;
};

ll dp[101][1 << 16];

int main() {
  int guy_num;
  while (~scanf("%d", &guy_num)) {
    if (guy_num == 0)
      break;
    Data guy[100];

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < guy_num; i++) {

      int date_num;
      ll satisfaction_vol;
      scanf("%d %lld", &date_num, &satisfaction_vol);
      int mask = 0;
      for (int j = 0; j < date_num; j++) {
        int begin, end;
        scanf("%d %d", &begin, &end);
        for (int k = begin - 6; k < end - 6; k++)
          mask |= (1 << k);
      }
      guy[i].mask = mask;
      guy[i].satisfaction_vol = satisfaction_vol;
    }

    for (int i = 0; i < guy_num; i++) {
      for (int S = 0; S <= (1 << 16) - 1; S++) {
        dp[i + 1][S] = max(dp[i + 1][S], dp[i][S]);
        if ((S & guy[i].mask) == 0) {
          int next = S | guy[i].mask;
          dp[i + 1][next] =
              max(dp[i + 1][next], dp[i][S] + guy[i].satisfaction_vol);
        }
      }
    }

    ll max_v = 0;
    for (int S = 0; S <= (1 << 16) - 1; S++)
      max_v = max(dp[guy_num][S], max_v);
    printf("%lld\\n", max_v);
  }
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

const static int tx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const static int ty[] = {-1, -1, 0, 1, 1, 1, 0, -1};

static const double EPS = 1e-8;

class Data {
public:
  Data() : satisfaction_vol(0), mask(0) {}
  int satisfaction_vol;
  int mask;
};

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

int main() {
  int guy_num;
  while (~scanf("%d", &guy_num)) {
    if (guy_num == 0)
      break;
    Data guy[100];

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < guy_num; i++) {

      int date_num, satisfaction_vol;
      scanf("%d %d", &date_num, &satisfaction_vol);
      int mask = 0;
      for (int j = 0; j < date_num; j++) {
        int begin, end;
        scanf("%d %d", &begin, &end);
        for (int k = begin - 6; k < end - 6; k++)
          mask |= (1 << k);
      }
      guy[i].mask = mask;
      guy[i].satisfaction_vol = satisfaction_vol;
    }

    for (int i = 0; i < guy_num; i++) {
      for (int S = 0; S <= (1 << 17) - 1; S++) {
        if ((S & guy[i].mask) == 0) {
          int next = S | guy[i].mask;
          dp[i + 1][next] = max(max(dp[i][next], dp[i + 1][next]),
                                dp[i][S] + guy[i].satisfaction_vol);
        } else {
          dp[i + 1][S] = max(dp[i + 1][S], dp[i][S]);
        }
      }
    }

    ll max_v = 0;
    for (int S = 0; S <= (1 << 17) - 1; S++)
      max_v = max(dp[guy_num][S], max_v);
    printf("%d\\n", max_v);
  }
}