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

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define DEBUG(x) cout << #x << ": " << x << endl

#define MAX_N 10
#define MAX_D 10
#define MAX_X 100000

//??\\??????
int n, d, x;
int P[MAX_D][MAX_N];
int dp[MAX_N][MAX_X];

int rec(int hi, int ban, int nokori) {
  if (ban == n) {
    return 0;
  }
  if (dp[ban][nokori] >= 0) {
    return dp[ban][nokori];
  }
  int ans = 0;
  for (int i = 0;; ++i) {
    if (P[hi][ban] * i > nokori) {
      ans = max(ans, rec(hi, ban + 1, nokori));
      break;
    }
    ans = max(ans, max((P[hi + 1][ban] - P[hi][ban]) * i +
                           rec(hi, ban + 1, nokori - P[hi][ban] * i),
                       rec(hi, ban + 1, nokori)));
    // DEBUG(hi);
    // DEBUG(ban);
    // DEBUG(i);
  }
  dp[ban][nokori] = ans;
  return ans;
}

int main() {
  cin >> n >> d >> x;
  REP(i, d) {
    REP(j, n) { cin >> P[i][j]; }
  }
  /*
  //?????\\?????§??????????±???????
  REP(i,d-1){
          int tmpx=x;//????????\\????????\\??????????????????
          int mouke=0;//?????\\????????????
          while(true){
                  vector<tuple<int,int> >
  Q(n);//???????????????????????§?????????????????? REP(j,n){ get<0>(Q[j]) =
  j;//??????????????£???????????¢??£????????? get<1>(Q[j]) = (P[i+1][j] -
  P[i][j]) * (tmpx/P[i][j]);
                  }
                  sort(Q.begin(),Q.end(),[](const tuple<int,int>& a, const
  tuple<int,int>& b){return get<1>(a) > get<1>(b);});//??????????????????
                  //????????\\???????????????or?????????????????´????????????
                  if(get<1>(Q[0])<=0){
                          break;
                  }
                  mouke += get<1>(Q[0]);
                  tmpx -= P[i][get<0>(Q[0])]*(tmpx/P[i][get<0>(Q[0])]);
          }
          x+=mouke;
  }
  */
  REP(i, d - 1) {
    REP(j, MAX_N) {
      REP(k, MAX_X) { dp[j][k] = -1; }
    }
    x = x + rec(i, 0, x);
  }
  cout << x << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define DEBUG(x) cout << #x << ": " << x << endl

#define MAX_N 10
#define MAX_D 10
#define MAX_X 10000

//??\\??????
int n, d, x;
int P[MAX_D][MAX_N];
int dp[MAX_N][MAX_X];

int rec(int hi, int ban, int nokori) {
  if (ban == n) {
    return 0;
  }
  if (dp[ban][nokori] >= 0) {
    return dp[ban][nokori];
  }
  int ans = 0;
  for (int i = 0;; ++i) {
    if (P[hi][ban] * i > nokori) {
      ans = max(ans, rec(hi, ban + 1, nokori));
      break;
    }
    ans = max(ans, max((P[hi + 1][ban] - P[hi][ban]) * i +
                           rec(hi, ban + 1, nokori - P[hi][ban] * i),
                       rec(hi, ban + 1, nokori)));
    // DEBUG(i);
  }
  dp[ban][nokori] = ans;
  return ans;
}

int main() {
  cin >> n >> d >> x;
  REP(i, d) {
    REP(j, n) { cin >> P[i][j]; }
  }
  /*
  //?????\\?????§??????????±???????
  REP(i,d-1){
          int tmpx=x;//????????\\????????\\??????????????????
          int mouke=0;//?????\\????????????
          while(true){
                  vector<tuple<int,int> >
  Q(n);//???????????????????????§?????????????????? REP(j,n){ get<0>(Q[j]) =
  j;//??????????????£???????????¢??£????????? get<1>(Q[j]) = (P[i+1][j] -
  P[i][j]) * (tmpx/P[i][j]);
                  }
                  sort(Q.begin(),Q.end(),[](const tuple<int,int>& a, const
  tuple<int,int>& b){return get<1>(a) > get<1>(b);});//??????????????????
                  //????????\\???????????????or?????????????????´????????????
                  if(get<1>(Q[0])<=0){
                          break;
                  }
                  mouke += get<1>(Q[0]);
                  tmpx -= P[i][get<0>(Q[0])]*(tmpx/P[i][get<0>(Q[0])]);
          }
          x+=mouke;
  }
  */
  REP(i, d - 1) {
    REP(j, MAX_N) {
      REP(k, MAX_X) { dp[j][k] = -1; }
    }
    x = x + rec(i, 0, x);
  }
  cout << x << endl;
  return 0;
}