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

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

typedef long long ll;

#define rep(x, n) for (int i = x; i < (n); ++i)
#define Fast_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

int binarySearch(long long a[], int beg, int end, int item) {
  int mid;
  if (end >= beg) {
    mid = (beg + end) / 2;

    if (a[mid] == item)
      return mid + 1;
    else if (a[mid] < item) {
      if (a[mid + 1] == item)
        return mid + 2;
      else if (a[mid + 1] > item)
        return mid + 1;
      return (binarySearch(a, mid + 1, end, item));
    } else {
      if (a[mid - 1] <= item)
        return mid;
      return binarySearch(a, beg, mid - 1, item);
    }
  }
  return beg;
}

int main() {
  Fast_IO;
  int n, m, k;
  cin >> n >> m >> k;

  int arr1[n], arr2[m];
  rep(0, n) cin >> arr1[i];
  rep(0, m) cin >> arr2[i];

  ll pfx2[m + 1] = {0};
  pfx2[0] = arr2[0];
  rep(1, m) pfx2[i] = pfx2[i - 1] + arr2[i];
  pfx2[m] = LONG_LONG_MAX;

  int i = 0, res = ((k < pfx2[0]) ? 0 : binarySearch(pfx2, 0, m, k));
  while (i < n && k - arr1[i] >= 0) {
    int temp = i + 1;
    k -= arr1[i++];
    temp += binarySearch(pfx2, 0, m, k);
    res = max(res, temp);
  }
  cout << res;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, m;
  long long k;
  cin >> n >> m >> k;
  vector<long long> a(n);
  vector<long long> b(m);
  vector<long long> sum_a(n);
  vector<long long> sum_b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  sum_a[0] = a[0];
  sum_b[0] = b[0];
  for (int i = 1; i < n; i++)
    sum_a[i] = sum_a[i - 1] + a[i];
  for (int i = 1; i < m; i++)
    sum_b[i] = sum_a[i - 1] + b[i];

  // for (int i = 0; i < n; i++) cout << sum_a[i] << " ";
  // for (int i = 0; i < m; i++) cout << sum_b[i] << " ";

  int lower_a = 0;
  int lower_b = 0;
  for (int i = 0; i < n; i++)
    if (sum_a[i] <= k)
      lower_a = i;
  for (int i = 0; i < m; i++)
    if (sum_b[i] <= k)
      lower_b = i;
  if (lower_a == 0 && lower_b == 0) {
    // if(sum_a[lower_a] <)
    cout << 0;
  } else if (lower_a > lower_b) {
    int remain = k - sum_a[lower_a];
    int i = 0;
    while (i < sum_b.size() && sum_b[i] <= remain) {
      i++;
    }
    cout << lower_a + i + 1;
  } else {
    int remain = k - sum_b[lower_b];
    int i = 0;
    while (i < sum_a.size() && sum_a[i] <= remain) {
      i++;
    }
    cout << lower_b + i + 1;
  }
  // long long sum = 0;
  // int out = 0;
  // int i = 0, j = 0;
  // while (sum < k) {
  //     if (i == a.size()) {
  //         while (j < b.size() && sum < k) {
  //             if (sum + b[j] <= k) {
  //                 sum += b[j];
  //                 j++;
  //                 out++;
  //             }
  //             else {
  //                 break;
  //             }
  //         }
  //         break;
  //     }
  //     if (j == b.size()) {
  //         while (sum < k) {
  //             if (i < a.size() && sum + a[i] <= k) {
  //                 sum += a[i];
  //                 i++;
  //                 out++;
  //             }
  //             else {
  //                 break;
  //             }
  //         }
  //         break;
  //     }
  //     if (sum_a[i] < sum_b[j]) {
  //         if (sum + a[i] <= k) {
  //             sum += a[i];
  //             i++;
  //             out++;
  //         }
  //         else {
  //             break;
  //         }
  //     }
  //     else {
  //         if (sum + b[j] <= k) {
  //             sum += b[j];
  //             j++;
  //             out++;
  //         }
  //         else {
  //             break;
  //         }
  //     }
  // }
  // cout << out;
}