Problem p02101 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int n, p;
  cin >> n >> p;
  vector<int> X(n);
  vector<int> Y(n);
  for (int i = 0; i < n; i++) {
    cin >> X[i] >> Y[i];
  }

  // Need[i][j] := i日目から連続して励まされていて、j日目に必要な励まし力
  vector<vector<int>> Need(n, vector<int>(n, -1));

  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = i; j < n; j++) {
      int now_x = max(0, X[j] - t);
      t = max(0, Y[j] - now_x);
      Need[i][j] = t;
      // cout << t << " ";
    }
    // cout << endl;
  }

  // dp[i][j][k] := いままでj日学校行くことができている,k日連続で励まされている
  int dp[10][10][10];
  // dp初期化
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  dp[0][0][0] = p;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k <= i; k++) {

        // cout << i << " " << j << " " << k << " " << dp[2][0][0] << endl;
        // 行かない
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        /*if(j==0 && k==0){
            cout << i << " " << j << " " << k << " " << dp[i][j][k] << " " <<
        dp[i + 1][j][k] << endl;
        }*/
        // 行く
        int need = Need[i - k][i];
        if (need == 0)
          dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j][k]);
        else
          dp[i + 1][j + 1][k + 1] =
              max(dp[i + 1][j + 1][k + 1], dp[i][j][k] - need);
      }
    }
  }

  int ans = 0;
  for (int j = 0; j <= n; j++) {
    for (int k = 0; k <= n; k++) {
      if (dp[n][j][k] >= 0)
        ans = max(ans, j);
    }
  }

  /*for(int i = 0; i <= n; i++){
      for(int j = 0; j <= n; j++){
          for(int k = 0; k <= n; k++){
              cout << dp[i][j][k] << " ";
          }
          cout << endl;
      }
      cout << endl;
  }*/

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {

  int n, p;
  cin >> n >> p;
  vector<int> X(n);
  vector<int> Y(n);
  for (int i = 0; i < n; i++) {
    cin >> X[i] >> Y[i];
  }

  // Need[i][j] := i日目から連続して励まされていて、j日目に必要な励まし力
  vector<vector<int>> Need(n, vector<int>(n, -1));

  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = i; j < n; j++) {
      int now_x = max(0, X[j] - t);
      t = max(0, Y[j] - now_x);
      Need[i][j] = t;
      // cout << t << " ";
    }
    // cout << endl;
  }

  // dp[i][j][k] := いままでj日学校行くことができている,k日連続で励まされている
  int dp[110][110][110];
  // dp初期化
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  dp[0][0][0] = p;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k <= i; k++) {

        // cout << i << " " << j << " " << k << " " << dp[2][0][0] << endl;
        // 行かない
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        /*if(j==0 && k==0){
            cout << i << " " << j << " " << k << " " << dp[i][j][k] << " " <<
        dp[i + 1][j][k] << endl;
        }*/
        // 行く
        int need = Need[i - k][i];
        if (need == 0)
          dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j][k]);
        else
          dp[i + 1][j + 1][k + 1] =
              max(dp[i + 1][j + 1][k + 1], dp[i][j][k] - need);
      }
    }
  }

  int ans = 0;
  for (int j = 0; j <= n; j++) {
    for (int k = 0; k <= n; k++) {
      if (dp[n][j][k] >= 0)
        ans = max(ans, j);
    }
  }

  /*for(int i = 0; i <= n; i++){
      for(int j = 0; j <= n; j++){
          for(int k = 0; k <= n; k++){
              cout << dp[i][j][k] << " ";
          }
          cout << endl;
      }
      cout << endl;
  }*/

  cout << ans << endl;

  return 0;
}