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

#include <bits/stdc++.h>
#define int long long int
#define ld long double
#define getFaster                                                              \\
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define rev(i, n, init) for (int i = n; i >= init; i--)
#define MAXN 200005
#define MOD2 998244353
#define MOD1 1e9 + 7
#define f first
#define s second
#define endl '\\n'
#define pii pair<int, int>
#define piii pair<pii, int>
#define precise(i) cout << fixed << setprecision(i)
#define codejam cout << "Case #" << ii + 1 << ": ";
#define impossible cout << "IMPOSSIBLE" << endl;
#define error(s) throw runtime_error(s)
// std::mt19937_64
// rng(std::chrono::steady_clock::now().time_since_epoch().count()); int
// myRand(int R) { 	int val=rng() % R;
//       assert(val>=0);
//       return val;
// }
using namespace std;
const long double PI = atan(1.0) * 4;
const int64_t INF = 3e18;
const int32_t LOG = 21;
const int32_t MOD = MOD1;

int a[200005];
int b[200005];
int32_t main() {
  getFaster;
  int tests = 1;
  // cin>>tests;
  rep(ii, 0, tests) {
    int n, m, k;
    cin >> n >> m >> k;
    rep(i, 1, n + 1) cin >> a[i];
    rep(i, 1, m + 1) cin >> b[i];
    rep(i, 1, n + 1) a[i] += a[i - 1];
    rep(i, 1, m + 1) b[i] += b[i - 1];

    int ans = 0;
    int l = 1, r = n + m;
    while (l <= r) {
      int mid = (l + r) >> 1;
      bool ok = false;
      rep(i, 0, n + 1) {
        int rem = mid - i;
        if (rem < 0 || rem > m)
          continue;
        if (a[i] + b[rem] <= k) {
          ok = true;
          break;
        }
      }
      if (ok) {
        ans = max(ans, mid);
        l = mid + 1;
      } else
        r = mid - 1;
    }
    cout << ans << endl;
  }
  return 0;
}

/*
 TIPS:
 1.) Check for corner cases (like 0 is exception case)
 2.) Check for global memory sizes,memory refreshments and its complexity
 3.) Check for sorting criteria (if used 1-based indexing)
 4.) Check for numbers overflow (while multiplying and doing MOD)
 5.) Check for typecasting issues like int to float( while dividing numbers)
*/

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define ld long double
#define getFaster                                                              \\
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define rev(i, n, init) for (int i = n; i >= init; i--)
#define MAXN 200005
#define MOD2 998244353
#define MOD1 1e9 + 7
#define count count1
#define NIL -1
#define f first
#define s second
#define endl '\\n'
#define pii pair<int, int>
#define piii pair<pii, int>
#define precise(i) cout << fixed << setprecision(i)
#define codejam cout << "Case #" << ii + 1 << ": ";
#define impossible cout << "IMPOSSIBLE" << endl;
#define error(s) throw runtime_error(s)
// std::mt19937_64
// rng(std::chrono::steady_clock::now().time_since_epoch().count()); int
// myRand(int R) { 	int val=rng() % R;
//       assert(val>=0);
//       return val;
// }
using namespace std;
const long double PI = atan(1.0) * 4;
const int64_t INF = 3e18;
const int32_t LOG = 21;
const int32_t MOD = MOD1;

int a[200005];
int b[200005];
int32_t main() {
  getFaster;
  int tests = 1;
  // cin>>tests;
  rep(ii, 0, tests) {
    int n, m, k;
    cin >> n >> m >> k;
    rep(i, 1, n + 1) cin >> a[i];
    rep(i, 1, m + 1) cin >> b[i];
    int ans = 0;
    int cost = 0;
    int p1 = 1, p2 = 1;
    while (p1 <= n || p2 <= m) {
      if (p1 > n || a[p1] >= b[p2]) {
        if (cost + b[p2] > k)
          break;
        cost += b[p2];
        ans++;
        p2++;
        continue;
      }
      if (p2 > m || a[p1] <= b[p2]) {
        if (cost + a[p1] > k)
          break;
        cost += a[p1];
        ans++;
        p1++;
        continue;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

/*
 TIPS:
 1.) Check for corner cases (like 0 is exception case)
 2.) Check for global memory sizes,memory refreshments and its complexity
 3.) Check for sorting criteria (if used 1-based indexing)
 4.) Check for numbers overflow (while multiplying and doing MOD)
 5.) Check for typecasting issues like int to float( while dividing numbers)
*/