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

// 問題の URL を書いておく
//

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// #define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v)                                                               \\
  do {                                                                         \\
    cout << #v << ": " << v << endl;                                           \\
  } while (0)

#define PrintVec(v)                                                            \\
  do {                                                                         \\
    for (int __i = 0; __i < v.size(); ++__i) {                                 \\
      cout << #v << "[" << __i << "]: " << v[__i] << endl;                     \\
    }                                                                          \\
  } while (0)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)

#endif

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(int, const char **) {
  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0);
  vector<int64_t> b(m + 1, 0);

  rep(i, n) {
    int64_t aa;
    cin >> aa;
    a[i + 1] += a[i] + aa;
  }

  rep(i, m) {
    int64_t bb;
    cin >> bb;
    b[i + 1] += b[i] + bb;
  }
  int ans = 0;
  rep(i, n + 1) {
    auto rest = k - a[i];
    if (rest < 0) {
      break;
    }
    auto tb = upper_bound(b.begin(), b.end(), rest);
    auto bcount = (int)(tb - b.begin()) - 1;
    //        cout << "i: " << i << ", bcount: " << bcount << endl;
    ans = max(ans, i + bcount);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// 問題の URL を書いておく
//

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// #define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v)                                                               \\
  do {                                                                         \\
    cout << #v << ": " << v << endl;                                           \\
  } while (0)

#define PrintVec(v)                                                            \\
  do {                                                                         \\
    for (int __i = 0; __i < v.size(); ++__i) {                                 \\
      cout << #v << "[" << __i << "]: " << v[__i] << endl;                     \\
    }                                                                          \\
  } while (0)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)

#endif

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(int, const char **) {
  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0);
  vector<int64_t> b(m + 1, 0);
  int aMax = 0;
  rep(i, n) {
    int64_t aa;
    cin >> aa;
    a[i + 1] += a[i] + aa;
    if (a[i + 1] <= k) {
      aMax++;
    }
  }
  int bMax = 0;
  rep(i, m) {
    int64_t bb;
    cin >> bb;
    b[i + 1] += a[i] + bb;
    if (b[i + 1] <= k) {
      bMax++;
    }
  }
  int count = aMax;
  int ca = aMax;
  int cb = 0;
  int maxCount = count;
  rep(i, aMax) {
    if (cb == bMax) {
      break;
    }
    if (a[ca] + b[cb + 1] > k) {
      ca--;
      continue;
    }
    while (true) {
      if (a[ca] + b[cb + 1] <= k && cb < bMax) {
        cb++;
      } else {
        break;
      }
    }
    maxCount = max(maxCount, ca + cb);
  }
  cout << maxCount << endl;

  return 0;
}