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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void dump(vector<T> v) {
  for (auto &x : v)
    cerr << x << " ";
  cerr << endl;
}
template <class T> inline void dump(vector<pair<T, T>> v) {
  for (auto &p : v)
    cerr << p.first << " " << p.second << endl;
}
template <class T> inline void dump(vector<vector<T>> vv) {
  for (auto &v : vv) {
    for (auto &x : v)
      cerr << x << " ";
    cerr << endl;
  }
}

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 62;
constexpr double eps = (1e-9);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int 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];

  // A だけに集中したときに何冊読めるか
  int idx = -1;
  ll time = 0;
  rep(i, n) {
    if (time + a[i] <= k) {
      time += a[i];
      idx = i;
    } else {
      break;
    }
  }

  ll ans = idx + 1;

  // a を諦めながら b を読む
  int idx_b = -1;
  ll tmp = ans;
  rep(i, m) {
    while (time + b[i] > k && idx >= 0) {
      time -= a[idx];
      idx--;
      tmp--;
    }
    if (time + b[i] <= k) {
      time += b[i];
      idx_b = i;
      tmp++;
    } else {
      break;
    }
    chmax(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void dump(vector<T> v) {
  for (auto &x : v)
    cerr << x << " ";
  cerr << endl;
}
template <class T> inline void dump(vector<pair<T, T>> v) {
  for (auto &p : v)
    cerr << p.first << " " << p.second << endl;
}
template <class T> inline void dump(vector<vector<T>> vv) {
  for (auto &v : vv) {
    for (auto &x : v)
      cerr << x << " ";
    cerr << endl;
  }
}

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 62;
constexpr double eps = (1e-9);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int 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];

  // A だけに集中したときに何冊読めるか
  int idx = -1;
  ll time = 0;
  rep(i, n) {
    if (time + a[i] <= k) {
      time += a[i];
      idx = i;
    } else {
      break;
    }
  }

  ll ans = idx + 1;

  // a を諦めながら b を読む
  int idx_b = -1;
  ll tmp = ans;
  rep(i, m) {
    while (time + b[i] > k) {
      time -= a[idx];
      idx--;
      tmp--;
    }
    if (time + b[i] <= k) {
      time += b[i];
      idx_b = i;
      tmp++;
    }
    chmax(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}