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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep0(i, n) for (int i = 0; i < n; i++)
#define chmin(a, b)                                                            \\
  if ((a) > (b)) {                                                             \\
    a = (b);                                                                   \\
  }
#define chmax(a, b)                                                            \\
  if ((a) < (b)) {                                                             \\
    a = (b);                                                                   \\
  }
#ifdef _DEBUG
string _format(string args) {
  string r = "", t;
  istringstream s(args);
  while (getline(s, t, ','))
    r += t + ":%d ";
  return r + "\\n";
}
#define debug(...) printf(_format(#__VA_ARGS__).c_str(), __VA_ARGS__);
#else
#define debug(...)
#endif

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n + 1], SA[n + 1];
  ll B[m + 1], SB[n + 1];
  SA[0] = 0;
  SB[0] = 0;
  A[0] = 0;
  B[0] = 0;
  rep1(i, n) {
    cin >> A[i];
    SA[i] = SA[i - 1] + A[i];
    debug(A[i], SA[i]);
  }
  rep1(i, m) {
    cin >> B[i];
    SB[i] = SB[i - 1] + B[i];
    debug(B[i], SB[i]);
  }

  int ib = m;
  int ans = 0;
  rep0(i, n + 1) {
    if (k < A[i])
      break;
    k -= A[i];
    while (SB[ib] > k && ib > 0)
      ib--;
    debug(i, k, ib, SB[ib], B[ib], ans);
    chmax(ans, i + ib);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep0(i, n) for (int i = 0; i < n; i++)
#define chmin(a, b)                                                            \\
  if ((a) > (b)) {                                                             \\
    a = (b);                                                                   \\
  }
#define chmax(a, b)                                                            \\
  if ((a) < (b)) {                                                             \\
    a = (b);                                                                   \\
  }
#ifdef _DEBUG
string _format(string args) {
  string r = "", t;
  istringstream s(args);
  while (getline(s, t, ','))
    r += t + ":%d ";
  return r + "\\n";
}
#define debug(...) printf(_format(#__VA_ARGS__).c_str(), __VA_ARGS__);
#else
#define debug(...)
#endif

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n + 1];
  ll B[n + 1];
  rep1(i, n) {
    ll a;
    cin >> a;
    A[i] = a;
  }
  rep1(i, m) {
    ll b;
    cin >> b;
    B[i] = b;
  }

  int ai = 1;
  int bi = 1;

  while (k >= B[bi] || k >= A[ai]) {
    ll an = 0, bn = 0;
    ll ak = k, bk = k;
    while (ak > A[ai + an])
      ak -= A[ai + an++];
    while (bk > A[bi + bn])
      bk -= B[bi + bn++];
    debug(ak, bk);
    if (bn > an) {
      if (k >= B[bi] && bi <= m) {
        k -= B[bi++];
      } else if (k >= A[ai] && ai <= n) {
        k -= A[ai++];
      } else {
        break;
      }
    } else {
      if (k >= A[ai] && ai <= n) {
        k -= A[ai++];
      } else if (k >= B[bi] && bi <= m) {
        k -= B[bi++];
      } else {
        break;
      }
    }
    debug(ai, bi, k, A[ai], B[bi]);
  }

  ll ans = ai - 1 + bi - 1;
  cout << ans << endl;
}