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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type {
  pre_no,
  pre_yes,
};

struct Info {
  int ENC_POWER, WILL_POWER;
};

int main() {

  int N, P;
  scanf("%d %d", &N, &P);

  int X[N], Y[N];
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &X[i], &Y[i]);
  }

  Info dp[N + 1][N + 1][2];

  for (int i = 0; i <= N; i++) {
    for (int k = 0; k <= N; k++) {
      dp[i][k][pre_no].ENC_POWER = -1;
      dp[i][k][pre_no].WILL_POWER = -1;

      dp[i][k][pre_yes].ENC_POWER = -1;
      dp[i][k][pre_yes].WILL_POWER = -1;
    }
  }

  dp[0][0][pre_no].ENC_POWER = P;
  int today_value;

  for (int day = 0; day <= N - 1; day++) {
    for (int num = 0; num <= day; num++) {
      if (dp[day][num][pre_no].ENC_POWER != -1) {
        if (X[day] >= Y[day]) {
          dp[day + 1][num + 1][pre_no].ENC_POWER =
              max(dp[day + 1][num + 1][pre_no].ENC_POWER,
                  dp[day][num][pre_no].ENC_POWER);
        } else {

          dp[day + 1][num][pre_no].ENC_POWER =
              max(dp[day + 1][num][pre_no].ENC_POWER,
                  dp[day][num][pre_no].ENC_POWER);

          if (dp[day][num][pre_no].ENC_POWER >= Y[day] - X[day]) {

            if (dp[day + 1][num + 1][pre_yes].ENC_POWER <
                dp[day][num][pre_no].ENC_POWER - (Y[day] - X[day])) {

              dp[day + 1][num + 1][pre_yes].ENC_POWER =
                  dp[day][num][pre_no].ENC_POWER - (Y[day] - X[day]);
              dp[day + 1][num + 1][pre_yes].WILL_POWER =
                  X[day + 1] - (Y[day] - X[day]);

            } else if (dp[day + 1][num + 1][pre_yes].ENC_POWER ==
                       dp[day][num][pre_no].ENC_POWER - (Y[day] - X[day])) {
              if (dp[day + 1][num + 1][pre_yes].WILL_POWER <
                  X[day + 1] - (Y[day] - X[day])) {
                dp[day + 1][num + 1][pre_yes].WILL_POWER =
                    X[day + 1] - (Y[day] - X[day]);
              }
            }
          }
        }
      }

      if (dp[day][num][pre_yes].ENC_POWER != -1) {

        today_value = max(0, dp[day][num][pre_yes].WILL_POWER);

        if (today_value >= Y[day]) {
          dp[day + 1][num + 1][pre_no].ENC_POWER =
              max(dp[day + 1][num + 1][pre_no].ENC_POWER,
                  dp[day][num][pre_yes].ENC_POWER);
        } else {

          dp[day + 1][num][pre_no].ENC_POWER =
              max(dp[day + 1][num][pre_no].ENC_POWER,
                  dp[day][num][pre_yes].ENC_POWER);

          if (dp[day][num][pre_yes].ENC_POWER >= Y[day] - today_value) {

            if (dp[day + 1][num + 1][pre_yes].ENC_POWER <
                dp[day][num][pre_yes].ENC_POWER - (Y[day] - today_value)) {

              dp[day + 1][num + 1][pre_yes].ENC_POWER =
                  dp[day][num][pre_yes].ENC_POWER - (Y[day] - today_value);
              dp[day + 1][num + 1][pre_yes].WILL_POWER =
                  X[day + 1] - (Y[day] - today_value);

            } else if (dp[day + 1][num + 1][pre_yes].ENC_POWER ==
                       dp[day][num][pre_yes].ENC_POWER -
                           (Y[day] - today_value)) {

              if (dp[day + 1][num + 1][pre_yes].WILL_POWER <
                  X[day + 1] - (Y[day] - today_value)) {
                dp[day + 1][num + 1][pre_yes].WILL_POWER =
                    X[day + 1] - (Y[day] - today_value);
              }
            }
          }
        }
      }
    }
  }

  int ans = 0;
  for (int num = 1; num <= N; num++) {
    if (dp[N][num][pre_no].ENC_POWER != -1 ||
        dp[N][num][pre_yes].ENC_POWER != -1)
      ans = num;
  }

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type {
  pre_yes,
  pre_no,
};

struct Info {
  Info(int arg_rest, Type arg_type) {
    rest = arg_rest;
    type = arg_type;
  }
  int rest;
  Type type;
};

int main() {

  int N, P;
  scanf("%d %d", &N, &P);

  int **dp = new int *[P + 1];
  int **next_dp = new int *[P + 1];
  for (int i = 0; i <= P; i++) {
    dp[i] = new int[2];
    dp[i][pre_no] = 0;
    dp[i][pre_yes] = 0;

    next_dp[i] = new int[2];
    next_dp[i][pre_no] = 0;
    next_dp[i][pre_yes] = 0;
  }

  int X[N], Y[N];
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &X[i], &Y[i]);
  }

  int ans = 0, minimum = P, next_minimum = P, today_value;
  vector<Info> Updated;

  for (int day = 0; day < N; day++) {
    for (int rest = P; rest >= minimum; rest--) {

      if (X[day] >= Y[day]) {

        next_dp[rest][pre_no] = dp[rest][pre_no] + 1;
        ans = max(ans, next_dp[rest][pre_no]);
        Updated.push_back(Info(rest, pre_no));

      } else { // X[day] < Y[day]

        if (rest >= Y[day] - X[day]) {
          if (next_dp[rest - (Y[day] - X[day])][pre_yes] <
              dp[rest][pre_no] + 1) {
            next_dp[rest - (Y[day] - X[day])][pre_yes] = dp[rest][pre_no] + 1;
            ans = max(ans, dp[rest][pre_no] + 1);
            Updated.push_back(Info(rest - (Y[day] - X[day]), pre_yes));
            next_minimum = min(next_minimum, rest - (Y[day] - X[day]));
          }
        }
      }

      if (dp[rest][pre_yes] == 0)
        continue;

      today_value = max(0, X[day] - (Y[day - 1] - X[day - 1]));

      if (today_value >= Y[day]) { //?????\\?????¨??????????????????
        if (next_dp[rest][pre_no] < dp[rest][pre_yes] + 1) {
          next_dp[rest][pre_no] = dp[rest][pre_yes] + 1;
          ans = max(ans, next_dp[rest][pre_no]);
          Updated.push_back(Info(rest, pre_no));
        }
      } else { // today_value < Y[day]

        if (next_dp[rest][pre_no] < dp[rest][pre_yes]) {
          next_dp[rest][pre_no] = dp[rest][pre_yes];
          Updated.push_back(Info(rest, pre_no));
        }

        if (rest >= Y[day] - X[day]) {
          if (next_dp[rest - (Y[day] - X[day])][pre_yes] <
              dp[rest][pre_yes] + 1) {
            next_dp[rest - (Y[day] - X[day])][pre_yes] = dp[rest][pre_yes] + 1;
            ans = max(ans, dp[rest][pre_yes] + 1);
            Updated.push_back(Info(rest - (Y[day] - X[day]), pre_yes));
            next_minimum = min(next_minimum, rest - (Y[day] - X[day]));
          }
        }
      }
    }
    for (int k = 0; k < Updated.size(); k++) {
      dp[Updated[k].rest][Updated[k].type] =
          next_dp[Updated[k].rest][Updated[k].type];
    }
    Updated.clear();
    minimum = next_minimum;
  }

  printf("%d\\n", ans);

  return 0;
}