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

// "for if God is with them who can be against them."
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define endl '\\n'
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  ll ans = 0, res = 0;
  vector<ll> prea(n);
  vector<ll> preb(m);
  ll ind = -1;
  prea[0] = a[0];
  if (prea[0] <= k) {
    ans++;
    ind = 0;
  }
  preb[0] = b[0];
  for (int i = 1; i < n; i++) {
    prea[i] = a[i] + prea[i - 1];
    if (prea[i] <= k) {
      // cout << prea[i] << endl;
      ans++;
      ind = i;
    }
  }
  REP(i, 1, m - 1) { preb[i] = b[i] + preb[i - 1]; }
  if (ind == -1) {
    auto it = upper_bound(all(preb), k);
    if (it != preb.end()) {
      cout << (it - preb.begin());
    } else
      cout << m;
    return;
  }
  // cout << ans << endl;
  res = max(res, ans);
  for (int i = ind; i >= 0; i--) {
    auto it = upper_bound(all(preb), k - prea[i]);
    if (it != preb.end()) {
      res = max(res, ll(ans + int(it - preb.begin())));
    } else
      res = max(res, ll(ans + m));
    ans--;
  }
  auto it = upper_bound(all(preb), k);
  if (it != preb.end()) {
    res = max(res, ll(it - preb.begin()));
  } else
    res = max(res, m);
  cout << res;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
  return 0;
}

Error: -11

Fixed submission:

// "for if God is with them who can be against them."
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define endl '\\n'
#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
void solve() {
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  ll z = 0;
  ll a[n], b[m];
  REP(i, 0, n - 1) { cin >> a[i]; }
  REP(i, 0, m - 1) { cin >> b[i]; }
  ll i = 0, j = 0, res = 0;
  while (i < n and j < m) {
    if (a[i] < b[j]) {
      ans += a[i];
      i++;
    } else {
      ans += b[j];
      j++;
    }
    if (ans >= k)
      break;
    res++;
  }
  while (i < n) {
    ans += a[i];
    if (ans >= k)
      break;
    res++;
    i++;
  }
  while (j < m) {
    ans += a[j];
    if (ans >= k)
      break;
    res++;
    j++;
  }
  cout << res;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
  return 0;
}