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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define INF 1000000000000
#define MOD 1000000007
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \\
  (__VA_ARGS__)
#else
#define debug(...)
#endif

using namespace std;
using ll = long long;

template <class T> void printVector(const vector<T> &v) {
  for (auto value : v) {
    cout << value << " ";
  }
  cout << endl;
}

int main() {
  ll n, m, k, mi = 0;
  cin >> n >> m >> k;
  vector<ll> a(n), A(n + 1, 0);
  vector<ll> b(m), B(m + 1, 0);
  rep(i, n) {
    cin >> a[i];
    A[i + 1] = a[i] + A[i];
  }
  rep(i, m) {
    cin >> b[i];
    B[i + 1] = b[i] + B[i];
  }
  int start = 0;
  ll read_num = 0;
  rrep(i, n + 1) {
    if (A[i] > k)
      continue;
    for (int j = start; j < m + 1; j++) {
      // debug(A[i] + B[j], i, j, read_num);
      if (A[i] + B[j] >= k) {
        start = j;
        if (A[i] + B[j] > k)
          j--;
        read_num = i + j;
        // debug(A[i] + B[j], A[i], i, j, read_num);
        break;
      }
    }
    mi = max(mi, read_num);
  }
  cout << mi << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define INF 1000000000000
#define MOD 1000000007
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \\
  (__VA_ARGS__)
#else
#define debug(...)
#endif

using namespace std;
using ll = long long;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  stack<int> s_a, s_b;
  rrep(i, n) s_a.push(a[i]);
  rrep(i, m) s_b.push(b[i]);
  debug(s_a.top(), s_b.top());
  ll time = 0, cnt = 0;
  while (true) {
    // debug(time, s_a.top(), s_b.top());

    // 時間が低い方を抜き出す
    if (s_b.empty() || s_a.empty()) { // 空のとき
      if (s_a.empty()) {
        debug("a is empty");
        time += s_b.top();
        if (s_b.empty() == false)
          s_b.pop();
      }
      if (s_b.empty()) {
        debug("b is empty");
        time += s_a.top();
        if (s_a.empty() == false)
          s_a.pop();
      }
    } else {
      if (s_a.top() > s_b.top()) {
        time += s_b.top();
        if (s_b.empty() == false)
          s_b.pop();
      } else if (s_a.top() < s_b.top()) {
        time += s_a.top();
        if (s_a.empty() == false)
          s_a.pop();
        debug(time, s_a.empty());
      }
    }
    if (time > k)
      break;
    cnt++;
    // debug(s_a.empty(), s_b.empty(), s_b.top());
    if (s_b.empty() && s_a.empty())
      break;
  }
  cout << cnt << endl;
  return 0;
}