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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep0(i, n) for (ll i = 0; i <= (ll)(n); i++)

ll pow(ll a, ll n) { // compute a^n with o(logn)
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a;
    a = a * a;
    n >>= 1;
  }
  return res;
}

ll pow(ll a, ll n, ll mod) { // compute a^n (mod mod) with o(logn)
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n, m, k, cnt;
  cnt = 0;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  ll suma[n + 1];
  suma[0] = 0;
  ll sumb[m + 1];
  sumb[0] = 0;
  ll tmp_sum = 0;
  rep1(i, n) {
    cin >> a[i];
    tmp_sum += a[i];
    suma[i] = tmp_sum;
  }
  tmp_sum = 0;
  rep1(i, m) {
    cin >> b[i];
    tmp_sum += b[i];
    sumb[i] = tmp_sum;
  }
  int a_idx = n;
  int b_idx = 0;
  ll max_books = -1;
  while (a_idx >= 0) {
    ll tmp_books;
    if (suma[a_idx] > k) {
      a_idx--;
    } else {
      while (suma[a_idx] + sumb[b_idx] <= k && b_idx <= m)
        b_idx++;
      b_idx--;
      tmp_books = a_idx + b_idx;
      max_books = max(max_books, tmp_books);
      a_idx--;
    }
  }
  cout << max_books << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep0(i, n) for (ll i = 0; i <= (ll)(n); i++)

ll pow(ll a, ll n) { // compute a^n with o(logn)
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a;
    a = a * a;
    n >>= 1;
  }
  return res;
}

ll pow(ll a, ll n, ll mod) { // compute a^n (mod mod) with o(logn)
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n, m, k, cnt, time;
  cnt = 0;
  time = 0;
  cin >> n >> m >> k;
  ll a[n + 1], b[n + 1];
  rep1(i, n) cin >> a[i];
  rep1(i, m) cin >> b[i];
  ll a_index = 1;
  ll b_index = 1;
  while (time < k || (a_index > n && b_index > n)) {

    if (a_index > n) {
      time += b[b_index];
      b_index++;
    } else if (b_index > m) {
      time += a[a_index];
      a_index++;
    } else {
      if (a[a_index] > b[b_index]) {
        time += b[b_index];
        b_index++;
      } else if (a[a_index] < b[b_index]) {
        time += a[a_index];
        a_index++;
      } else {
        if (a[a_index + 1] > b[b_index + 1]) {
          time += b[b_index];
          b_index++;
        } else if (a[a_index + 1] < b[b_index + 1]) {
          time += a[a_index];
          a_index++;
        } else {
          time += a[a_index];
          a_index++;
        }
      }
    }

    if (k < time)
      break;
    // cout << a_index << " " << b_index <<endl;
    cnt++;
  }

  cout << cnt << endl;
  return 0;
}