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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define For(i, a, b) for (auto i = (a); i <= (b); ++i)
#define rFor(i, a, b) for (auto i = (b); i >= (a); --i)
#define call(var) cerr << #var << "=" << var << "\\n";
#define dup(x, y) (((x) + (y)-1) / (y))
#define all(x) (x).begin(), (x).end()
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const double pi = acos(-1);
template <class t, class u> inline bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> inline bool chmin(t &a, u b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> istream &operator>>(istream &is, vector<T> &a) {
  rep(i, a.size()) is >> a[i];
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &a) {
  rep(i, a.size()) os << a[i] << " ";
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  for (auto &t : mp)
    os << t.first << " " << t.second << "\\n";
  return os;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vl a(n), b(m);
  cin >> a;
  cin >> b;

  rep(i, n - 1) a[i + 1] += a[i];
  rep(i, m - 1) b[i + 1] += b[i];
  // cout << a << "\\n";
  // cout << b << "\\n";
  ll ans = 0;
  // 0
  if (a[0] <= k)
    chmax(ans, upper_bound(all(a), k) - a.begin());
  if (b[0] <= k)
    chmax(ans, upper_bound(all(b), k) - b.begin());

  for (int i = 0; a[i] < k && i < n; ++i) {
    if (k - a[i] < b[0]) {
      chmax(ans, i + 1);
      continue;
    }
    int bi = upper_bound(all(b), k - a[i]) - b.begin();
    --bi;
    // call(a[i]);
    // call(k-a[i]);
    // call(bi);
    // call(b[bi]);
    chmax(ans, i + 1 + bi + 1);
    // cerr << "\\n";
  }
  for (int i = 0; b[i] < k && i < m; ++i) {
    if (k - b[i] < a[0]) {
      chmax(ans, i + 1);
      continue;
    }
    int ai = upper_bound(all(a), k - b[i]) - a.begin();
    --ai;
    // call(b[i]);
    // call(k-b[i]);
    // call(ai);
    // call(a[ai]);
    chmax(ans, i + 1 + ai + 1);
    // cerr << "\\n";
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define For(i, a, b) for (auto i = (a); i <= (b); ++i)
#define rFor(i, a, b) for (auto i = (b); i >= (a); --i)
#define call(var) cerr << #var << "=" << var << "\\n";
#define dup(x, y) (((x) + (y)-1) / (y))
#define all(x) (x).begin(), (x).end()
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const double pi = acos(-1);
template <class t, class u> inline bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> inline bool chmin(t &a, u b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> istream &operator>>(istream &is, vector<T> &a) {
  rep(i, a.size()) is >> a[i];
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &a) {
  rep(i, a.size()) os << a[i] << " ";
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  for (auto &t : mp)
    os << t.first << " " << t.second << "\\n";
  return os;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vl a(n), b(m);
  cin >> a;
  cin >> b;

  rep(i, n - 1) a[i + 1] += a[i];
  rep(i, m - 1) b[i + 1] += b[i];
  // cout << a << "\\n";
  // cout << b << "\\n";
  int ans = 0;
  // 0
  if (a[0] < k)
    chmax(ans, lower_bound(all(a), k) - a.begin() + 1);
  if (b[0] < k)
    chmax(ans, lower_bound(all(b), k) - b.begin() + 1);

  for (int i = 0; a[i] < k; ++i) {
    int bi = upper_bound(all(b), k - a[i]) - b.begin();
    --bi;
    // call(a[i]);
    // call(k-a[i]);
    // call(bi);
    // call(b[bi]);
    chmax(ans, i + 1 + bi + 1);
    // cerr << "\\n";
  }
  cout << ans << "\\n";
}