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

// author rhythmicankur
// god_is_almighty
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
#define ll int
#define ld long double
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define vi vector<int>
#define pii pair<int, int>
#define pip pair<int, pii>
#define vpi vector<pii>
#define sbcount(x) __builtin_popcountll(x)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define fill(a, val) memset(a, val, sizeof(a))
#define PI acos(-1)
#define coud(a, b) cout << fixed << setprecision((b)) << (a)
#define M1 998244353
#define M2 1000000007
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;

template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
#define test4(x, y, z, a)                                                      \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << "  |  " << #a << ": " << a << endl;
#define test1(x) cerr << #x << ": " << x << endl
#define test2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define test3(x, y, z)                                                         \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
ll power(ll a, ll b, ll m = M2) {
  ll answer = 1;
  while (b) {
    if (b & 1)
      answer = (answer * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return answer;
}
using namespace std;
void ctrl() { cout << "Control" << endl; }
int make_num(string p) {
  stringstream geek(p);
  int x = 0;
  geek >> x;
  return x;
}
string make_str(int x) {
  ostringstream str1;
  str1 << x;
  string geek = str1.str();
  return geek;
}
int n, m, k;
int pref1[500000 + 5], pref2[500000 + 5];
bool chk(int x) {
  // test3(n,m,k);
  if (x > n + m)
    return false;

  if (x == 0)
    return true;

  // test1(x);
  if (pref1[x - 1] <= k && x <= n)
    return true;
  if (pref2[x - 1] <= k && x <= m)
    return true;
  for (int i = 1; i < x; i++) {
    if (i > n || x - i > m)
      continue;

    int val = pref1[i - 1] + pref2[x - i - 1];
    //  test2(val,x);
    if (val <= k)
      return true;
  }
  return false;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  cin >> n >> m >> k;

  int A[n], B[m];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  for (int i = 0; i < n; i++) {
    if (!i)
      pref1[i] = A[i];
    else {
      pref1[i] = pref1[i - 1] + A[i];
    }
  }

  for (int i = 0; i < m; i++)
    cin >> B[i];

  for (int i = 0; i < m; i++) {
    if (!i)
      pref2[i] = B[i];
    else {
      pref2[i] = pref2[i - 1] + B[i];
    }
  }

  int s = 0;
  for (int z = n + m; z > 0; z = z / 2) {
    while (chk(s + z))
      s += z;
  }
  cout << s;

  return 0;
}

Error: WA

Fixed submission:

// author rhythmicankur
// god_is_almighty
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
#define ll int
#define ld long double
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define vi vector<int>
#define pii pair<int, int>
#define pip pair<int, pii>
#define vpi vector<pii>
#define sbcount(x) __builtin_popcountll(x)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define fill(a, val) memset(a, val, sizeof(a))
#define PI acos(-1)
#define coud(a, b) cout << fixed << setprecision((b)) << (a)
#define M1 998244353
#define M2 1000000007
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;

template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
#define test4(x, y, z, a)                                                      \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << "  |  " << #a << ": " << a << endl;
#define test1(x) cerr << #x << ": " << x << endl
#define test2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define test3(x, y, z)                                                         \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
ll power(ll a, ll b, ll m = M2) {
  ll answer = 1;
  while (b) {
    if (b & 1)
      answer = (answer * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return answer;
}
using namespace std;
void ctrl() { cout << "Control" << endl; }
int make_num(string p) {
  stringstream geek(p);
  int x = 0;
  geek >> x;
  return x;
}
string make_str(int x) {
  ostringstream str1;
  str1 << x;
  string geek = str1.str();
  return geek;
}
int n, m, k;
int pref1[200000 + 5], pref2[200000 + 5];
bool chk(int x) {
  // test3(n,m,k);
  if (x > n + m)
    return false;

  if (x == 0)
    return true;

  // test1(x);
  if (pref1[x - 1] <= k && x <= n)
    return true;
  if (pref2[x - 1] <= k && x <= m)
    return true;
  for (int i = 1; i < x; i++) {
    if (i > n || x - i > m)
      continue;

    int val = pref1[i - 1] + pref2[x - i - 1];
    //  test2(val,x);
    if (val <= k)
      return true;
  }
  return false;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  cin >> n >> m >> k;

  int A[n], B[m];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  for (int i = 0; i < n; i++) {
    if (!i)
      pref1[i] = A[i];
    else {
      pref1[i] = pref1[i - 1] + A[i];
    }
  }

  for (int i = 0; i < m; i++)
    cin >> B[i];

  for (int i = 0; i < m; i++) {
    if (!i)
      pref2[i] = B[i];
    else {
      pref2[i] = pref2[i - 1] + B[i];
    }
  }

  int s = 0;
  for (int z = n + m; z > 0; z = z / 2) {
    while (chk(s + z))
      s += z;
  }
  cout << s;

  return 0;
}