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

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define PRINT(a) cout << (a) << endl
#define RALL(v) rbegin(v), rend(v)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second

#define debug(x)                                                               \\
  cerr << x << " "                                                             \\
       << "(L:" << __LINE__ << ")" << '\\n';

using ll = long long int;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;

template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> b[i];

  vector<ll> as(n + 1, 0);
  vector<ll> bs(m + 1, 0);

  REP(i, n) { as[i + 1] = a[i] + as[i]; }
  REP(i, m) { bs[i + 1] = b[i] + bs[i]; }

  // aをi冊選ぶとする
  int j = m;
  int ans = 0;
  REP(i, n + 1) {
    if (as[i] > k)
      break;
    while (as[i] + bs[j] > k && j >= 0)
      j--;
    if (j >= 0) {
      chmax(ans, i + j);
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define PRINT(a) cout << (a) << endl
#define RALL(v) rbegin(v), rend(v)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second

#define debug(x)                                                               \\
  cerr << x << " "                                                             \\
       << "(L:" << __LINE__ << ")" << '\\n';

using ll = long long int;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;

template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> b[i];

  vector<ll> as(n + 1, 0);
  vector<ll> bs(m + 1, 0);

  REP(i, n) { as[i + 1] = a[i] + as[i]; }
  REP(i, m) { bs[i + 1] = b[i] + bs[i]; }

  // aをi冊選ぶとする
  int j = 0;
  int ans = 0;
  REP(i, n + 1) {
    while (as[i] + bs[j] <= k && j <= m)
      j++;
    j--;
    chmax(ans, i + j);
  }
  cout << ans << endl;
}