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

// ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
// ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
// [███████████████████].░░░░░░░░░░░░░░
// ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░

// PointBlank's code (⌐■_■)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

ll power(ll x, ll y);
#define repp(I, A, B) for (int I = A; I <= B; I++)
#define rep(I, A, B) for (int I = A; I < B; I++)
#define rrep(I, B, A) for (int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(this) cerr << "> " << #this << " : " << this << "\\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M)                                                            \\
  repp(I, 1, M) {                                                              \\
    ll A, B;                                                                   \\
    cin >> A >> B;                                                             \\
    v[A].pb(B), v[B].pb(A);                                                    \\
  }
#define in_edges2(M)                                                           \\
  repp(I, 1, M) {                                                              \\
    ll A, B;                                                                   \\
    cin >> A >> B;                                                             \\
    v[A].pb(B);                                                                \\
  }
#define endl "\\n"

const ll MOD = 1000000007;
const int MX = 1e6 + 5;

int a[MX], b[MX];
ll pa[MX], pb[MX];
int n, m;
ll k;

bool chk(int x) {
  // if(n + m < x) return 0;
  repp(i, 0, x) {
    int A = i, B = x - A;
    if (B > m || A > n)
      continue;
    if (pa[A] + pb[B] <= k)
      return 1;
  }
  return 0;
}

int main() // PointBlank's code ¯\\_(ツ)_/¯
{
  quickio cin >> n >> m >> k;

  repp(i, 1, n) cin >> a[i];
  repp(i, 1, m) cin >> b[i];

  rep(i, 1, MX) pa[i] = pa[i - 1] + a[i], pb[i] = pb[i - 1] + b[i];

  ll l = 0, r = n + m + 1;

  while (l <= r) {
    ll mid = (l + r) / 2;
    if (chk(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }

  cout << r;
}

ll power(ll x, ll y) {
  ll res = 1;
  x %= MOD;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1, x = (x * x) % MOD;
  }
  return res;
}

Error: WA

Fixed submission:

// ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
// ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
// [███████████████████].░░░░░░░░░░░░░░
// ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░

// PointBlank's code (⌐■_■)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

ll power(ll x, ll y);
#define repp(I, A, B) for (int I = A; I <= B; I++)
#define rep(I, A, B) for (int I = A; I < B; I++)
#define rrep(I, B, A) for (int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(this) cerr << "> " << #this << " : " << this << "\\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M)                                                            \\
  repp(I, 1, M) {                                                              \\
    ll A, B;                                                                   \\
    cin >> A >> B;                                                             \\
    v[A].pb(B), v[B].pb(A);                                                    \\
  }
#define in_edges2(M)                                                           \\
  repp(I, 1, M) {                                                              \\
    ll A, B;                                                                   \\
    cin >> A >> B;                                                             \\
    v[A].pb(B);                                                                \\
  }
#define endl "\\n"

const ll MOD = 1000000007;
const int MX = 1e6 + 5;

int a[MX], b[MX];
ll pa[MX], pb[MX];
int n, m;
ll k;

bool chk(int x) {
  // if(n + m < x) return 0;
  repp(i, 0, n) {
    int A = i, B = x - A;
    if (B > m || A > n)
      continue;
    if (pa[A] + pb[B] <= k)
      return 1;
  }
  return 0;
}

int main() // PointBlank's code ¯\\_(ツ)_/¯
{
  quickio cin >> n >> m >> k;

  repp(i, 1, n) cin >> a[i];
  repp(i, 1, m) cin >> b[i];

  rep(i, 1, MX) pa[i] = pa[i - 1] + a[i], pb[i] = pb[i - 1] + b[i];

  ll l = 0, r = n + m + 1;

  while (l <= r) {
    ll mid = (l + r) / 2;
    if (chk(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }

  cout << r;
}

ll power(ll x, ll y) {
  ll res = 1;
  x %= MOD;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1, x = (x * x) % MOD;
  }
  return res;
}