Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#ifdef _DEBUG
#define dout cout
#define debug() if (true)
#define dvout(v) vout(v)
#else
#define dout                                                                   \
  if (false)                                                                   \
  cout
#define debug() if (false)
#define dvout(v)                                                               \
  if (false)                                                                   \
  vout(v)
#endif

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define vout(v)                                                                \
  for (auto &i : v) {                                                          \
    cout << i << " ";                                                          \
  }                                                                            \
  cout << "\n"; // 配列出力
#define vec(v, n) vector<int> v(n, 0);
#define vecll(v, n) vector<ll> v(n, 0);
#define vin(v)                                                                 \
  for (auto &i : v)                                                            \
    cin >> i; // 配列入力

#define mod (int)(1e9 + 7)
typedef long long ll;
typedef unsigned long long ull;
#define next asdnext
#define prev asdprev
#define _n "\n"
#define _n2 "\n\n"
#define _t "\t"

vector<int> a = {1, 14, 32, 51, 51, 51, 243, 419, 750, 910};

void Main() {
  int N, M, K;
  cin >> N >> M >> K;
  vec(A, N);
  vec(B, M);
  vin(A);
  vin(B);

  vec(sumA, N + 1);
  vec(sumB, M + 1);
  rep(i, N) { sumA[i + 1] = sumA[i] + A[i]; }
  rep(i, N) { sumB[i + 1] = sumB[i] + A[i]; }

  int ans = 0;

  rep(i, N + 1) {
    if (sumA[i] > K)
      break;
    ans = max(ans, i);
    int l = -1, r = M + 1;
    while (abs(l - r) > 1) {
      int mid = (l + r) / 2;
      if (sumB[mid] <= (K - sumA[i])) {
        ans = max(ans, i + mid);
        l = mid;
      } else
        r = mid;
    }
  }

  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


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

#ifdef _DEBUG
#define dout cout
#define debug() if (true)
#define dvout(v) vout(v)
#else
#define dout                                                                   \
  if (false)                                                                   \
  cout
#define debug() if (false)
#define dvout(v)                                                               \
  if (false)                                                                   \
  vout(v)
#endif

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define vout(v)                                                                \
  for (auto &i : v) {                                                          \
    cout << i << " ";                                                          \
  }                                                                            \
  cout << "\n"; // 配列出力
#define vec(v, n) vector<int> v(n, 0);
#define vecll(v, n) vector<ll> v(n, 0);
#define vin(v)                                                                 \
  for (auto &i : v)                                                            \
    cin >> i; // 配列入力

#define mod (int)(1e9 + 7)
typedef long long ll;
typedef unsigned long long ull;
#define next asdnext
#define prev asdprev
#define _n "\n"
#define _n2 "\n\n"
#define _t "\t"

vector<int> a = {1, 14, 32, 51, 51, 51, 243, 419, 750, 910};

void Main() {
  int N, M, K;
  cin >> N >> M >> K;
  vec(A, N);
  vec(B, M);
  vin(A);
  vin(B);

  vector<ll> sumA(N + 1, 0);
  vector<ll> sumB(M + 1, 0);
  rep(i, N) {
    sumA[i + 1] = sumA[i] + A[i];
    dout << "sumA" << i << ':' << sumA[i] << _n;
  }
  dout << "sumA" << N << ':' << sumA[N] << _n;
  rep(i, M) {
    sumB[i + 1] = sumB[i] + B[i];
    dout << "sumB" << i << ':' << sumB[i] << _n;
  }
  dout << "sumB" << M << ':' << sumB[M] << _n;

  int ans = 0;

  rep(i, N + 1) {
    if (sumA[i] > K)
      break;
    ans = max(ans, i);
    int l = -1, r = M + 1;
    while (abs(l - r) > 1) {
      int mid = (l + r) / 2;
      if (sumB[mid] <= (K - sumA[i])) {
        ans = max(ans, i + mid);
        l = mid;
      } else
        r = mid;
    }
  }

  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}