Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;
using ll = long long;

#define eb emplace_back
#define pb push_back
// #define mp make_pair
#define ff first
#define ss second

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
// template<typename T> using oset = tree <T, null_type, less <T>, rb_tree_tag,
// tree_order_statistics_node_update >; template<typename T> using
// MinPriorityQueue = priority_queue <T, vector <T>, greater <T> >;
// template<typename T> void done(T ans) {cout << ans << "\n"; exit(0);}

template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << p.first << ' ' << p.second;
  return os;
}

const int mod = 1e9 + 7;
const int N = 1e6 + 5;

int main() {
  io();
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[n];

  vector<ll> pre1(n), pre2(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i)
      pre1[i] += pre1[i - 1];
    pre1[i] += a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i)
      pre2[i] += pre2[i - 1];
    pre2[i] += b[i];
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    if (k >= pre2[i]) {
      ans = i + 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (pre1[i] > k)
      break;
    int rem = k - pre1[i];
    auto it = upper_bound(pre2.begin(), pre2.end(), rem);
    if (it == pre2.begin()) {
      ans = max(ans, i + 1);
      continue;
    } else {
      ans = max(ans, (int)(it - pre2.begin() + i + 1));
      debug(ans);
    }
  }
  cout << ans;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;
using ll = long long;

#define eb emplace_back
#define pb push_back
// #define mp make_pair
#define ff first
#define ss second

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
// template<typename T> using oset = tree <T, null_type, less <T>, rb_tree_tag,
// tree_order_statistics_node_update >; template<typename T> using
// MinPriorityQueue = priority_queue <T, vector <T>, greater <T> >;
// template<typename T> void done(T ans) {cout << ans << "\n"; exit(0);}

template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << p.first << ' ' << p.second;
  return os;
}

const int mod = 1e9 + 7;
const int N = 1e6 + 5;

int main() {
  io();
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];

  vector<ll> pre1(n), pre2(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i)
      pre1[i] += pre1[i - 1];
    pre1[i] += a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i)
      pre2[i] += pre2[i - 1];
    pre2[i] += b[i];
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    if (k >= pre2[i]) {
      ans = i + 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (pre1[i] > k)
      break;
    int rem = k - pre1[i];
    auto it = upper_bound(pre2.begin(), pre2.end(), rem);
    if (it == pre2.begin()) {
      ans = max(ans, i + 1);
      continue;
    } else {
      ans = max(ans, (int)(it - pre2.begin() + i + 1));
      debug(ans);
    }
  }
  cout << ans;
}