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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int cntn = 0, cntm = 0;
long long int a[200001], b[200001], sa[200001], sb[200001];
int n, m;
long int k;
long int ans = 0;
int dp[200][200][2000];
long int idx;

int dfs(int i, int j, long int k) {
  int res = 0;
  if (dp[i][j][k] != -1 && n - i < 200 && m - j < 200 && k < 2000)
    return dp[i][j][k];
  if (i + j == n + m || (k - a[i] < 0 && k - b[j] < 0))
    return 0;
  else if (i == n && k - b[j] > 0) {
    res = dfs(i, j + 1, k - b[j]) + 1;
  } else if (j == m && k - a[i] > 0) {
    res = dfs(i + 1, j, k - a[i]) + 1;
  } else if (k - a[i] > 0 && k - b[j] > 0) {
    res = max(dfs(i + 1, j, k - a[i]) + 1, dfs(i + 1, j, k - b[j]) + 1);
  } else if (k - a[i] > 0) {
    res = dfs(i + 1, j, k - a[i]) + 1;
  } else if (k - b[j] > 0) {
    res = dfs(i, j + 1, k - b[j]) + 1;
  }
  if (n - i < 200 && m - j < 200 && k < 2000)
    dp[i][j][k] = res;
  return res;
}

void bsearch(long int left, long int right, long long int thresh) {
  long int res = -1;
  long int mid = (left + right) / 2;
  // cout << sb[mid] << " " << left << " " << right << " " << mid << " " <<
  // thresh << endl;
  if (left == right) {
    ;
    // return mid;
  } else if (sb[mid] > thresh) {
    bsearch(left, mid, thresh);
  } else {

    idx = max(mid, idx);
    bsearch(mid + 1, right, thresh);
    // cout << idx << " " << endl;
  }
  // return res ;
}

int main() {
  cin >> n >> m >> k;
  rep(i, n) {
    cin >> a[i];
    if (i == 0) {
      sa[i] = 0;
    }
    sa[i + 1] = sa[i] + a[i];
    // cout << sa[i+1] << endl;
  }
  rep(i, m) {
    cin >> b[i];
    if (i == 0) {
      sb[i] = 0;
    }
    sb[i + 1] = sb[i] + b[i];
    // cout << sb[i+1] << endl;
  }

  // cout << dp[1][1][1];
  rep(i, n + 1) {
    if (k >= sa[i]) {
      // cout << sa[i] << " i= " << i << "--------------" << endl;
      bsearch(0, m + 1, k - sa[i]);
      ans = max(idx + i, ans);
      idx = 0;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;
#define rep(i, n) for (long int i = 0; i < (n); i++)

int cntn = 0, cntm = 0;
long int a[200001], b[200001];
int n, m;
long int k;
int ans = 0;
int dp[200][200][2000];

int dfs(int i, int j, long int k) {
  int res = 0;
  // cout << dp[i][j][k] << endl;
  // cout << i << " " << j << " " << a[i] << " " << b[j] << endl;
  if (dp[i][j][k] != -1 && n - i < 200 && m - j < 200 && k < 2000)
    return dp[i][j][k];
  if (i + j == n + m || (k - a[i] < 0 && k - b[j] < 0))
    return 0;
  else if (i == n && k - b[j] > 0) {
    res = dfs(i, j + 1, k - b[j]) + 1;
  } else if (j == m && k - a[i] > 0) {
    res = dfs(i + 1, j, k - a[i]) + 1;
  } else if (k - a[i] > 0 && k - b[j] > 0) {
    res = max(dfs(i + 1, j, k - a[i]) + 1, dfs(i + 1, j, k - b[j]) + 1);
  } else if (k - a[i] > 0) {
    res = dfs(i + 1, j, k - a[i]) + 1;
  } else if (k - b[j] > 0) {
    res = dfs(i, j + 1, k - b[j]) + 1;
  }
  if (n - i < 200 && m - j < 200 && k < 2000)
    dp[i][j][k] = res;
  return res;
}

int main() {
  cin >> n >> m >> k;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  a[n] = 100000000;
  b[m] = 100000000;
  rep(i, 200) {
    rep(j, 200) {
      rep(l, 2000) { dp[i][j][l] = -1; }
    }
  }
  // cout << dp[1][1][1];
  ans = dfs(0, 0, k);

  cout << ans << endl;

  return 0;
}