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

#include <bits/stdc++.h>
#include <string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9 + 7)
#define nax (ll)(1e5)
#define INFL LLONG_MAX
#define INF INT_MAX
#define disp(a, n, arr)                                                        \\
  for (int i = a; i < (ll)n; i++)                                              \\
    cout << arr[i] << " ";                                                     \\
  cout << "\\n";
#define check cout << "Working\\n";
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FIO;

  // Write your code here

  int t = 1;
  // cin >> t;
  while (t--) {
    ll n, m, sum;
    cin >> n >> m >> sum;
    vl a(n), b(m);
    vl prefix_a(n + 1);
    prefix_a[0] = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      prefix_a[i + 1] = a[i] + prefix_a[i];
    }
    // disp(0, n + 1, prefix_a);
    for (int i = 0; i < m; i++) {
      cin >> b[i];
    }

    ll l = 0;
    ll r = n;
    ll mid;
    ll ans = 0;
    ll mx = -1;
    while (l <= r) {
      mid = l + (r - l) / 2;
      if (prefix_a[mid] <= sum) {
        ans = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }

    mx = max(ans, mx);
    ll cnt = mx;

    ll total = prefix_a[ans];
    int j = 0;
    while (j < m) {
      if (total + b[j] <= sum) {
        total += b[j];
        j++;
        cnt++;
      } else {
        if (ans == 0)
          break;
        ans--;
        total -= a[ans];
        cnt--;
      }
      mx = max(cnt, mx);
    }

    cout << mx << "\\n";
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#include <string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9 + 7)
#define nax (ll)(1e5)
#define INFL LLONG_MAX
#define INF INT_MAX
#define disp(a, n, arr)                                                        \\
  for (int i = a; i < (ll)n; i++)                                              \\
    cout << arr[i] << " ";                                                     \\
  cout << "\\n";
#define check cout << "Working\\n";
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FIO;

  // Write your code here

  int t = 1;
  // cin >> t;
  while (t--) {
    ll n, m, sum;
    cin >> n >> m >> sum;
    vl a(n), b(m);
    vl prefix_a(n + 1);
    prefix_a[0] = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      prefix_a[i + 1] = a[i] + prefix_a[i];
    }
    // disp(0, n + 1, prefix_a);
    for (int i = 0; i < m; i++) {
      cin >> b[i];
    }

    ll l = 0;
    ll r = n;
    ll mid;
    ll ans = 0;
    ll mx = -1;
    while (l <= r) {
      mid = l + (r - l) / 2;
      if (prefix_a[mid] <= sum) {
        ans = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }

    mx = max(ans, mx);
    ll cnt = mx;

    ll total = prefix_a[ans];

    for (int i = 0; i < m; i++) {
      if (total + b[i] <= sum) {
        total += b[i];
        cnt++;
        mx = max(mx, cnt);
      } else {
        total -= a[ans - 1];
        ans--;
        cnt--;
      }
    }

    cout << mx << "\\n";
  }

  return 0;
}