Problem p01438 (C++) — buggy submission, verdict: Runtime Error


#include <bitset>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

// DP

using namespace std;

struct guy {
  int M;
  int bit;
  unsigned long long int L;
  vector<int> S, E;
  guy() { bit = 0; }
};

int set(int p, vector<int> s, vector<int> e) {
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < e[i] - s[i]; j++) {
      p += (1 << (j + s[i]));
    }
  }
  return p;
}

unsigned long long int max(unsigned long long int p, unsigned long long int q) {
  if (p > q)
    return p;
  return q;
}

int main(void) {

  vector<vector<unsigned long long int>> dp(
      100, vector<unsigned long long int>((1 << 16), 0));

  while (true) {
    int N;
    vector<guy> guys;

    /*
            dp[人の数][スケジュールのbit] = 効用の最大値
    */

    //		unsigned long long int dp[100][1 << 16] = {0};
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < (1 << 16); j++) {
        dp[i][j] = 0;
      }
    }

    unsigned long long int ans = 0;
    cin >> N;
    if (N == 0)
      break;

    for (int i = 0; i < N; i++) {
      guy g;
      cin >> g.M >> g.L;
      for (int j = 0; j < g.M; j++) {
        int tmp, tmp2;
        cin >> tmp >> tmp2;
        tmp -= 6;
        tmp2 -= 6;
        g.S.push_back(tmp);
        g.E.push_back(tmp2);
      }
      g.bit = set(0, g.S, g.E);
      guys.push_back(g);
    }

    for (int i = 0; i < N; i++) {
      dp[i][set(0, guys[i].S, guys[i].E)] = guys[i].L;
    }
    for (int i = 1; i < 16; i++) {
      for (int j = 0; j < 1 << 16; j++) {
        for (int k = 0; k < N; k++) {
          if (!(j & guys[k].bit)) {
            int tmp = (j | guys[k].bit);
            dp[i][tmp] = max(dp[i][tmp], dp[i - 1][j] + guys[k].L);
          }
        }
      }
    }

    for (int i = 0; i < 1 << 16; i++) {
      for (int j = 0; j < N; j++)
        ans = max(dp[j][i], ans);
    }

    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:


#include <bitset>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

// DP

using namespace std;

struct guy {
  int M;
  int bit;
  unsigned long long int L;
  vector<int> S, E;
  guy() { bit = 0; }
};

int set(int p, vector<int> s, vector<int> e) {
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < e[i] - s[i]; j++) {
      p += (1 << (j + s[i]));
    }
  }
  return p;
}

unsigned long long int max(unsigned long long int p, unsigned long long int q) {
  if (p > q)
    return p;
  return q;
}

int main(void) {

  while (true) {
    int N;
    vector<guy> guys;

    /*
            dp[人の数][スケジュールのbit] = 効用の最大値
    */
    unsigned long long int dp[100][1 << 16] = {0};

    unsigned long long int ans = 0;
    cin >> N;
    if (N == 0)
      break;

    for (int i = 0; i < N; i++) {
      guy g;
      cin >> g.M >> g.L;
      for (int j = 0; j < g.M; j++) {
        int tmp, tmp2;
        cin >> tmp >> tmp2;
        tmp -= 6;
        tmp2 -= 6;
        g.S.push_back(tmp);
        g.E.push_back(tmp2);
      }
      g.bit = set(0, g.S, g.E);
      guys.push_back(g);
    }

    for (int i = 0; i < N; i++) {
      dp[i][set(0, guys[i].S, guys[i].E)] = guys[i].L;
    }
    for (int i = 1; i < 16; i++) {
      for (int j = 0; j < 1 << 16; j++) {
        for (int k = 0; k < N; k++) {
          if (!(j & guys[k].bit)) {
            int tmp = (j | guys[k].bit);
            dp[i][tmp] = max(dp[i][tmp], dp[i - 1][j] + guys[k].L);
          }
        }
      }
    }

    for (int i = 0; i < 1 << 16; i++) {
      for (int j = 0; j < N; j++)
        ans = max(dp[j][i], ans);
    }

    cout << ans << endl;
  }
  return 0;
}