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

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

// typedef
// -----------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;

// container util
// -----------------------------------------------------------------------------
#define ALL(c) (c).begin(), (c).end()
#define SZ(c) ((ll)(c).size())
#define MAX(c) *max_element(ALL(c))
#define MIN(c) *min_element(ALL(c))
#define SORT(c) sort((c).begin(), (c).end())

// repetition
// -----------------------------------------------------------------------------
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
// -----------------------------------------------------------------------------
const ll INF = 1e10;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);

// debug
// -----------------------------------------------------------------------------
#define DUMP(x) cerr << #x << " = " << (x) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N + 1, 0);
  FOR(i, 1, N + 1) {
    cin >> A[i];
    A[i] += A[i - 1];
  }
  vector<ll> B(M + 1, 0);
  FOR(i, 1, M + 1) {
    cin >> B[i];
    B[i] += B[i - 1];
  }
  // Aからi冊読むときにBから最大j冊読める
  ll j = M, ans = 0;
  REP(i, N + 1) {
    if (A[i] > K) {
      break;
    }
    while (K - A[i] < B[j]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

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

// typedef
// -----------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;

// container util
// -----------------------------------------------------------------------------
#define ALL(c) (c).begin(), (c).end()
#define SZ(c) ((ll)(c).size())
#define MAX(c) *max_element(ALL(c))
#define MIN(c) *min_element(ALL(c))
#define SORT(c) sort((c).begin(), (c).end())

// repetition
// -----------------------------------------------------------------------------
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
// -----------------------------------------------------------------------------
const ll INF = 1e10;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);

// debug
// -----------------------------------------------------------------------------
#define DUMP(x) cerr << #x << " = " << (x) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N + 1, 0);
  ll c = 0;
  FOR(i, 1, N + 1) {
    cin >> A[i];
    A[i] += A[i - 1];
    if (A[i] > K) {
      A[i] = INF;
    } else {
      c = max(c, i);
    }
  }
  vector<ll> B(M + 1, 0);
  FOR(i, 1, M + 1) {
    cin >> B[i];
    B[i] += B[i - 1];
    if (B[i] > K) {
      B[i] = INF;
    } else {
      c = max(c, i);
    }
  }
  // Aから0冊, Bからc冊本を選ぶとして探索開始
  ll ans = c;
  REP(i, N + 1) {
    ll j = c - i;
    if (j < 0) {
      break;
    }
    while (true) {
      if (A[i] + B[j] > K) {
        break;
      } else {
        c = i + j;
        j++;
      }
    }
  }
  cout << c << "\\n";
}