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

// Created by ...
#include <bits/stdc++.h>

#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'

#define vll vector<ll>
#define M 100000
#define MD 1000000007
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; ++i)
#define fo(i, a, b) for (int i = a; i <= b; ++i)
#define pii pair<ll, ll>
#define vec(a) vector<a>
#define all(a) a.begin(), a.end()
#define se second
#define fi first
#define inf 0xffffffff
#define inchar getchar_unlocked
#define outchar(x) putchar_unlocked(x)
#define int long long
#define endl '\\n'
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

using namespace std;
using ll = long long;
ll md = MD;

ll exp(ll a, ll b) {
  ll r = 1ll;
  while (b > 0) {
    if (b & 1) {
      r = r * (a % md);
      r = (r + md) % md;
    }
    b >>= 1;
    a = (a % md) * (a % md);
    a = (a + md) % md;
  }
  return (r + md) % md;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll Min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
ll Max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
int ffind(vll &v, int x) {
  int low = 0, high = v.size() - 1, mid, ans = -1;
  while (low <= high) {
    mid = (low + high) / 2;
    if (v[mid] <= x) {
      ans = mid;
      low = mid + 1;
    } else
      high = mid - 1;
  }
  return ans;
}
int32_t main() {
  IOS;
  int n, m, k;
  cin >> n >> m >> k;
  vll a(n), b(m);
  rep(i, 0, n - 1) cin >> a[i];
  rep(i, 0, 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 val = ffind(a, k), ans = val + 1;
  for (int i = val; i >= 0; --i) {
    int x = ffind(b, k - a[i]); // db1(x);
    ans = Max(ans, x + i + 2);
  }
  ans = Max(ans, ffind(b, k) + 1);
  cout << ans;

  cout << '\\n';
  return 0;
}

Error: 0

Fixed submission:

// Created by ...
#include <bits/stdc++.h>

#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'

#define vll vector<ll>
#define M 100000
#define MD 1000000007
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; ++i)
#define fo(i, a, b) for (int i = a; i <= b; ++i)
#define pii pair<ll, ll>
#define vec(a) vector<a>
#define all(a) a.begin(), a.end()
#define se second
#define fi first
#define inf 0xffffffff
#define inchar getchar_unlocked
#define outchar(x) putchar_unlocked(x)
#define int long long
#define endl '\\n'
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

using namespace std;
using ll = long long;
ll md = MD;

ll exp(ll a, ll b) {
  ll r = 1ll;
  while (b > 0) {
    if (b & 1) {
      r = r * (a % md);
      r = (r + md) % md;
    }
    b >>= 1;
    a = (a % md) * (a % md);
    a = (a + md) % md;
  }
  return (r + md) % md;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll Min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
ll Max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
int ffind(vll &v, int x) {
  int low = 0, high = v.size() - 1, mid, ans = -1;
  while (low <= high) {
    mid = (low + high) / 2;
    if (v[mid] <= x) {
      ans = mid;
      low = mid + 1;
    } else
      high = mid - 1;
  }
  return ans;
}
int32_t main() {
  IOS;
  int n, m, k;
  cin >> n >> m >> k;
  vll a(n), b(m);
  rep(i, 0, n - 1) cin >> a[i];
  rep(i, 0, 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 val = Max(ffind(a, k), ffind(b, k)), ans = val;
  for (int i = val; i >= 0; --i) {
    int x = ffind(b, k - a[i]); // db1(x);
    ans = Max(ans, x + i + 2);
  }
  ans = Max(ans, 0);
  cout << ans;

  cout << '\\n';
  return 0;
}