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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define endl "\\n"
#define loop(i, s, e) for (int i = s; i < e; i++)
#define int long long
#define vi vector<int>
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define R(v) reverse(v.begin(), v.end())
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define prv(v)                                                                 \\
  for (auto &x : v)                                                            \\
    cout << x << " ";
#define piirv(v)                                                               \\
  for (auto &x : v)                                                            \\
    cout << x.ff << " " << x.ss << endl;
#define prvv(v)                                                                \\
  for (auto &x : v)                                                            \\
    for (auto &y : x.ss)                                                       \\
      cout << y << " ";
#define mp make_pair
#define pii pair<int, int>
#define mset(a, f) memset(a, f, sizeof(a))
#define pb push_back
#define inf INT_MAX
#define INF LLONG_MAX
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int N = 400000 + 5;
const int M = 1e9 + 7;
int A[N];
int pA[N];
int B[N];
int pB[N];
int n, m, k;

bool check(int mid) {
  int sum = 0;
  int mn = INF;
  loop(i, 0, mid + 1) {
    if (i > n)
      break;
    int t = INF;
    if (mid - i <= m)
      t = pA[i] + pB[mid - i];
    mn = min(mn, t);
  }

  return mn <= k;
}

void solve() {
  cin >> n >> m >> k;
  int sum = 0;
  loop(i, 0, n) cin >> A[i];
  loop(i, 1, n + 1) sum += A[i - 1], pA[i] = sum;
  sum = 0;
  loop(i, 0, m) cin >> B[i];
  loop(i, 1, m + 1) sum += B[i - 1], pB[i] = sum;

  // loop(i,1,n+1)cout << pA[i] << " ";cout << endl;

  int ans = 0;

  int st = 0, en = n + m;

  while (st <= en) {
    int mid = (st + en) / 2;
    if (check(mid)) {
      ans = mid;
      st = mid + 1;
    } else
      en = mid - 1;
  }

  cout << ans;
}

int32_t main() {

  cout << fixed << setprecision(16);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define endl "\\n"
#define loop(i, s, e) for (int i = s; i < e; i++)
#define int long long
#define vi vector<int>
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define R(v) reverse(v.begin(), v.end())
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define prv(v)                                                                 \\
  for (auto &x : v)                                                            \\
    cout << x << " ";
#define piirv(v)                                                               \\
  for (auto &x : v)                                                            \\
    cout << x.ff << " " << x.ss << endl;
#define prvv(v)                                                                \\
  for (auto &x : v)                                                            \\
    for (auto &y : x.ss)                                                       \\
      cout << y << " ";
#define mp make_pair
#define pii pair<int, int>
#define mset(a, f) memset(a, f, sizeof(a))
#define pb push_back
#define inf INT_MAX
#define INF LLONG_MAX
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int N = 400000 + 5;
const int M = 1e9 + 7;
int A[N];
int pA[N];
int B[N];
int pB[N];
int n, m, k;

bool check(int mid) {
  int sum = 0;
  int mn = INF;

  loop(i, max(mid - m, 0LL), n + 1) { mn = min(mn, pA[i] + pB[mid - i]); }

  loop(i, max(mid - n, 0LL), m + 1) { mn = min(mn, pB[i] + pA[mid - i]); }
  return mn <= k;
}

void solve() {
  cin >> n >> m >> k;
  int sum = 0;
  loop(i, 0, n) cin >> A[i];
  loop(i, 1, n + 1) sum += A[i - 1], pA[i] = sum;
  sum = 0;
  loop(i, 0, m) cin >> B[i];
  loop(i, 1, m + 1) sum += B[i - 1], pB[i] = sum;

  // loop(i,1,n+1)cout << pA[i] << " ";cout << endl;

  int ans = 0;

  int st = 0, en = n + m;

  while (st <= en) {
    int mid = (st + en) / 2;
    if (check(mid)) {
      ans = mid;
      st = mid + 1;
    } else
      en = mid - 1;
  }

  cout << ans;
}

int32_t main() {

  cout << fixed << setprecision(16);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
}