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

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

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int _n = n, i = 0; i < _n; ++i)
#define FOR(i, a, b) for (int64_t i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int64_t i = (a), _b = (b); i >= _b; --i)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

const ll md = 1000000007;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  VI A(n), B(m);
  REP(i, n) cin >> A[i];
  REP(i, m) cin >> B[i];
  int b = m - 1;
  ll ans = 0;
  vector<ll> AA(n), BB(m);
  ll suma = 0;
  REP(i, n) {
    suma += A[i];
    AA[i] = suma;
  }
  ll sumb = 0;
  REP(i, m) {
    sumb += B[i];
    BB[i] = sumb;
  }
  while (b >= 0 && BB[b] > k) {
    b--;
  }
  ans = b + 1;
  FOR(i, 0, n - 1) {
    while (b >= 0 && BB[b] + AA[i] > k) {
      b--;
    }
    if (b < 0) {
      if (AA[i] <= k) {
        ans = max(ans, i + 1);
      } else {
        break;
      }
    } else {
      ans = max(ans, i + 1 + b + 1);
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int _n = n, i = 0; i < _n; ++i)
#define FOR(i, a, b) for (int64_t i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int64_t i = (a), _b = (b); i >= _b; --i)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

const ll md = 1000000007;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  VI A(n), B(m);
  REP(i, n) cin >> A[i];
  REP(i, m) cin >> B[i];
  int a = 0, b = m - 1;
  ll ans = 0;
  vector<ll> AA(n), BB(m);
  ll suma = 0;
  REP(i, n) {
    suma += A[i];
    AA[i] = suma;
  }
  ll sumb = 0;
  REP(i, m) {
    sumb += B[i];
    BB[i] = sumb;
  }
  while (BB[b] > k) {
    b--;
  }
  ans = b + 1;
  FOR(i, 0, n - 1) {
    while (BB[b] + AA[i] > k) {
      b--;
    }
    ans = max(ans, i + 1 + b + 1);
  }

  cout << ans << endl;
}