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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fsp(x) fixed << setprecision(x)
#define xout cerr
const ll inf = LLONG_MAX;
const long double pi = acosl(-1);
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

bool is_ok(ll x, ll n, ll m, ll k, vector<ll> &suma, vector<ll> &sumb) {
  for (ll i = 0; i <= min(x, n); i++) {
    ll j = x - i;
    if (j > m)
      continue;
    if (suma[i] + sumb[j] <= k)
      return true;
  }
  return false;
}

ll binary_search(ll l, ll r, ll n, ll m, ll k, vector<ll> &suma,
                 vector<ll> &sumb) {
  if (l + 1 == r)
    return l;
  ll mid = (l + r) / 2;
  if (is_ok(mid, n, m, k, suma, sumb))
    return binary_search(mid, r, n, m, k, suma, sumb);
  else
    return binary_search(l, mid, n, m, k, suma, sumb);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // const ll p = 1e9 + 7;
  // const ll p = 998244353;

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < m; i++)
    cin >> b[i];

  if (a[0] > k || b[0] > k) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> suma(n + 1, 0), sumb(m + 1, 0);
  for (ll i = 0; i < n; i++)
    suma[i + 1] = suma[i] + a[i];
  for (ll i = 0; i < m; i++)
    sumb[i + 1] = sumb[i] + b[i];
  if (suma[n] + sumb[m] <= k) {
    cout << n + m << endl;
    return 0;
  }

  cout << binary_search(0, n + m, n, m, k, suma, sumb) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fsp(x) fixed << setprecision(x)
#define xout cerr
const ll inf = LLONG_MAX;
const long double pi = acosl(-1);
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

bool is_ok(ll x, ll n, ll m, ll k, vector<ll> &suma, vector<ll> &sumb) {
  for (ll i = 0; i <= min(x, n); i++) {
    ll j = x - i;
    if (j > m)
      continue;
    if (suma[i] + sumb[j] <= k)
      return true;
  }
  return false;
}

ll binary_search(ll l, ll r, ll n, ll m, ll k, vector<ll> &suma,
                 vector<ll> &sumb) {
  if (l + 1 == r)
    return l;
  ll mid = (l + r) / 2;
  if (is_ok(mid, n, m, k, suma, sumb))
    return binary_search(mid, r, n, m, k, suma, sumb);
  else
    return binary_search(l, mid, n, m, k, suma, sumb);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // const ll p = 1e9 + 7;
  // const ll p = 998244353;

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < m; i++)
    cin >> b[i];

  if (a[0] > k || b[0] > k) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> suma(n + 1, 0), sumb(m + 1, 0);
  for (ll i = 0; i < n; i++)
    suma[i + 1] = suma[i] + a[i];
  for (ll i = 0; i < m; i++)
    sumb[i + 1] = sumb[i] + b[i];
  if (suma[n] + sumb[n] <= k) {
    cout << n + m << endl;
    return 0;
  }

  cout << binary_search(0, n + m, n, m, k, suma, sumb) << endl;
}