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

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define display(v)                                                             \\
  for (auto x : v)                                                             \\
  cout << x << ", "
#define print(x)                                                               \\
  cout << (#x) << " = [ ";                                                     \\
  display(x);                                                                  \\
  cout << "]\\n"
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
#define ceel(x, y) ((x) / (y) + !((x) % (y) == 0))
#define ss second
#define ff first
#define pb push_back
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;

// DEBUG TEMPLATE by livw
void __print(int x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifdef INCLUDEWHENOFFLINE
#define debug(x...)                                                            \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

bool check(ll a[], ll b[], ll n, ll m, ll k, ll val) {
  for (int i = 0; i <= n; i++) {
    int j = val - i;
    if (j >= 0 and j <= m and a[i] + b[j] <= k)
      return true;
  }
  for (int i = 0; i <= m; i++) {
    int j = val - i;
    if (j >= 0 and j <= n and a[j] + b[i] <= k)
      return true;
  }
  return false;
}

int main() {
  fastio;
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  ll a[n + 1], b[m + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i];
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i <= m; i++)
    b[i] += b[i - 1];
  int l = 1, r = n + m;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(a, b, n, m, k, mid)) {
      l = mid + 1;
      ans = mid;
    } else
      r = mid - 1;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define display(v)                                                             \\
  for (auto x : v)                                                             \\
  cout << x << ", "
#define print(x)                                                               \\
  cout << (#x) << " = [ ";                                                     \\
  display(x);                                                                  \\
  cout << "]\\n"
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
#define ceel(x, y) ((x) / (y) + !((x) % (y) == 0))
#define ss second
#define ff first
#define pb push_back
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;

// DEBUG TEMPLATE by livw
void __print(int x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifdef INCLUDEWHENOFFLINE
#define debug(x...)                                                            \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

bool check(ll a[], ll b[], ll n, ll m, ll k, ll val) {
  for (int i = 0; i <= n; i++) {
    int j = val - i;
    if (j <= m and a[i] + b[j] <= k)
      return true;
  }
  for (int i = 0; i <= m; i++) {
    int j = val - i;
    if (i <= n and a[j] + b[i] <= k)
      return true;
  }
  return false;
}

int main() {
  fastio;
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  ll a[n + 1], b[m + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i];
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i <= m; i++)
    b[i] += b[i - 1];
  int l = 1, r = n + m;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(a, b, n, m, k, mid)) {
      l = mid + 1;
      ans = mid;
    } else
      r = mid - 1;
  }
  cout << ans;
  return 0;
}