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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define Test                                                                   \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define ls lower_bound
#define up upper_bound
#define bs binary_search
#define S second
#define F first
#define vi vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvi vector<vi>
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define m_n(v) *min_element(all(v))
#define m_x(v) *max_element(all(v))
#define ___(a)                                                                 \\
  for (auto &i : a)                                                            \\
  cin >> i
/*
#include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds;
typedef
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
*/
int power(int x, int y, int mod) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    int q = a / m;
    int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;

  return x;
}
int binomial(int n, int k) {
  int res = 1;
  if (k > n - k)
    k = n - k;

  for (int i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }

  return res;
}
/*
vector<int> prime;
void sieve(){
    bool arr[300000]={0};
    prime.pb(2);
    for(int i=3;i<300000;i+=2){
        if(arr[i])continue;
        for(int j=i*i;j<300000;j+=i) arr[j]=1;
        prime.pb(i);
    }
}*/
// const int N=1e18+2;
const int mod = 1e9 + 7;
const int N = 1e5 + 9;

int32_t main() {
  IOS;
  int n, m, k;
  cin >> n >> m >> k;
  vi a(n + 1, 0), b(m + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i] += a[i - 1];
  for (int i = 1; i <= m; i++)
    cin >> b[i], b[i] += b[i - 1];
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    int want = k - a[i];
    if (want < 0)
      break;
    int l = 0, r = m;
    int mid, take = 0;
    while (l <= r) {
      mid = (l + r) / 2;
      if (b[mid] <= want) {
        take = max(take, mid);
        l = mid + 1;
      } else
        r = mid - 1;
    }
    ans = max(ans, i + take);
  }
  cout << ans;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define Test                                                                   \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define ls lower_bound
#define up upper_bound
#define bs binary_search
#define S second
#define F first
#define vi vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvi vector<vi>
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define m_n(v) *min_element(all(v))
#define m_x(v) *max_element(all(v))
#define ___(a)                                                                 \\
  for (auto &i : a)                                                            \\
  cin >> i
/*
#include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds;
typedef
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
*/
int power(int x, int y, int mod) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    int q = a / m;
    int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;

  return x;
}
int binomial(int n, int k) {
  int res = 1;
  if (k > n - k)
    k = n - k;

  for (int i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }

  return res;
}
/*
vector<int> prime;
void sieve(){
    bool arr[300000]={0};
    prime.pb(2);
    for(int i=3;i<300000;i+=2){
        if(arr[i])continue;
        for(int j=i*i;j<300000;j+=i) arr[j]=1;
        prime.pb(i);
    }
}*/
// const int N=1e18+2;
const int mod = 1e9 + 7;
const int N = 1e5 + 9;

int32_t main() {
  IOS;
  int n, m, k;
  cin >> n >> m >> k;
  int ans = 0;
  vi a(n), b(m);
  ___(a);
  ___(b);
  int i = 0, j = 0;
  while (1) {
    if (a[i] > k and b[i] > k)
      break;
    if (a[i] <= b[j]) {
      k -= a[i];
      i++;
      ans++;
    } else {
      k -= b[j];
      j++;
      ans++;
    }
  }
  cout << ans << "\\n";
  return 0;
}