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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \\
  {}
#define ARDEB(i, X)                                                            \\
  {}
#define END                                                                    \\
  {}
#endif
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
int n, k, ans, m;
int a[211111], b[211111], bsum[211111];
signed main() {
  cin >> n >> m >> k;
  a[0] = 0;
  rep(i, n) cin >> a[i + 1];
  rep(i, m) cin >> b[i];
  rep(i, m) { bsum[i + 1] = bsum[i] + b[i]; }
  int sum = 0;
  for (int i = 0; i < n + 1 && sum + a[i] <= k; i++) {
    sum += a[i];
    int l = -1, r = m + 1, key = k - sum;
    while (abs(r - l) > 1) {
      int mid = (l + r) / 2;
      if (bsum[mid] <= key)
        l = mid;
      else
        r = mid;
    }
    // cout<<l<<" "<<i+1<<endl;
    ans = max(ans, l + i);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \\
  {}
#define ARDEB(i, X)                                                            \\
  {}
#define END                                                                    \\
  {}
#endif
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
int n, k, ans, m;
int a[111111], b[111111];
signed main() {
  cin >> n >> m >> k;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  int in = 0, im = 0;
  while (true) {
    if (k - min(a[in], b[im]) < 0 || (im == m && in == n))
      break;

    if (a[in] < b[im] && in != n) {
      k -= a[in];
      // cout<<a[in]<<endl;
      in++;
      ans++;
    } else if (im != m) {
      k -= b[im];
      // cout<<b[im]<<endl;
      im++;
      ans++;
    }
  }
  cout << ans << endl;
}