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

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

#define endl "\\n"
#define ll long long
#define ld long double
#define ull unsigned long long int
#define MOD (ll)(1e9 + 7)
#define vi vector<int>
#define vl vector<long>
#define vll vector<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define len(x) x.length()
#define all(x) x.begin(), x.end()
#define deb1(x) cout << #x << " " << x << endl;
#define deb2(x, y) cout << #x << " " << x << "\\t" << #y << " " << y << endl;
#define deb3(x, y, z)                                                          \\
  cout << #x << " " << x << "\\t" << #y << " " << y << "\\t" << #z << " " << z   \\
       << endl;
#define debug1(x) cerr << #x << " " << x << endl;
#define debug2(x, y) cerr << #x << " " << x << "\\t" << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \\
  cerr << #x << " " << x << "\\t" << #y << " " << y << "\\t" << #z << " " << z   \\
       << endl;
#define info(s) cerr << s << " " << endl;
#define fr(a, b, c) for (auto a = b; b < c ? a < c : a >= c; b < c ? a++ : a--)
#define frc(a, b, c) for (auto a = b; a != c; a++)
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define lb(v, x) lower_bound(v.begin(), v.end(), x)
#define ub(v, x) upper_bound(v.begin(), v.end(), x)
#define clr(x) memset(x, 0, sizeof(x))
#define GR (double)(1 + sqrt(5)) / 2
#define fib(n) (ll) round(pow(GR, n) / sqrt(5))
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define out1(x) cout << x << " " << endl
#define out2(x, y) cout << x << " " << y << endl
#define SORT(a) sort(all(a));
#define SORTR(a) sort(all(a), greater<ll>());
#define ARRAY(a, n)                                                            \\
  vll a;                                                                       \\
  fr(i, 0, n) {                                                                \\
    ll temp;                                                                   \\
    cin >> temp;                                                               \\
    a.pb(temp);                                                                \\
  }
#define PRINT(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define MAX 100050
#define pb push_back
#define mp make_pair
#define F first
#define S second

template <typename T> void output_vector(const vector<T>, bool, bool, int, int);

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v);

template <typename T> istream &operator>>(istream &in, vector<T> &v);

void solve() {
  ll n;
  ll m;
  ll x;
  cin >> n >> m >> x;

  vector<ll> a(n);
  for (ll a_i = 0; a_i < n; a_i++) {
    cin >> a[a_i];
  }

  vector<ll> b(m);
  for (ll b_i = 0; b_i < m; b_i++) {
    cin >> b[b_i];
  }

  ll sum = 0, count = 0, i = 0, j = 0;

  while (i < n && sum + a[i] <=
                      x) { // considering only first stack and calculating count
    sum += a[i];
    i++;
  }
  count = i;

  while (j < m && i >= 0) { // now adding one element of second stack at a time
                            // and subtracting the top element of first stack
                            // and calculating the count
    sum += b[j];
    j++;
    while (sum > x && i > 0) {
      i--;
      sum -= a[i];
    }
    if (sum <= x && i + j > count)
      count = i + j;
  }
  cout << count << endl;
}

int main() {

  FIO;
  // ll test;cin>>test;
  // while(test--)
  solve();
  return 0;
}

template <typename T>
void output_vector(const vector<T> &v, bool debug_print = false,
                   bool add_one = false, int start = -1, int end = -1) {
  if (start < 0)
    start = 0;
  if (end < 0)
    end = int(v.size());

  for (int i = start; i < end; i++)
    (debug_print)
        ? (cerr << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\\n'))
        : (cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\\n'));
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  int i = 0;
  for (T x : v) {
    out << x;
    (i++ != v.size()) ? out << " " : out << "\\n";
  }
  return out;
}

template <typename T> istream &operator>>(istream &in, vector<T> &v) {
  for (T &i : v)
    in >> i;
  return in;
}

Error: 0

Fixed submission:

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

#define endl "\\n"
#define ll long long
#define ld long double
#define ull unsigned long long int
#define MOD (ll)(1e9 + 7)
#define vi vector<int>
#define vl vector<long>
#define vll vector<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define len(x) x.length()
#define all(x) x.begin(), x.end()
#define deb1(x) cout << #x << " " << x << endl;
#define deb2(x, y) cout << #x << " " << x << "\\t" << #y << " " << y << endl;
#define deb3(x, y, z)                                                          \\
  cout << #x << " " << x << "\\t" << #y << " " << y << "\\t" << #z << " " << z   \\
       << endl;
#define debug1(x) cerr << #x << " " << x << endl;
#define debug2(x, y) cerr << #x << " " << x << "\\t" << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \\
  cerr << #x << " " << x << "\\t" << #y << " " << y << "\\t" << #z << " " << z   \\
       << endl;
#define info(s) cerr << s << " " << endl;
#define fr(a, b, c) for (auto a = b; b < c ? a < c : a >= c; b < c ? a++ : a--)
#define frc(a, b, c) for (auto a = b; a != c; a++)
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define lb(v, x) lower_bound(v.begin(), v.end(), x)
#define ub(v, x) upper_bound(v.begin(), v.end(), x)
#define clr(x) memset(x, 0, sizeof(x))
#define GR (double)(1 + sqrt(5)) / 2
#define fib(n) (ll) round(pow(GR, n) / sqrt(5))
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define out1(x) cout << x << " " << endl
#define out2(x, y) cout << x << " " << y << endl
#define SORT(a) sort(all(a));
#define SORTR(a) sort(all(a), greater<ll>());
#define ARRAY(a, n)                                                            \\
  vll a;                                                                       \\
  fr(i, 0, n) {                                                                \\
    ll temp;                                                                   \\
    cin >> temp;                                                               \\
    a.pb(temp);                                                                \\
  }
#define PRINT(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define MAX 100050
#define pb push_back
#define mp make_pair
#define F first
#define S second

template <typename T> void output_vector(const vector<T>, bool, bool, int, int);

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v);

template <typename T> istream &operator>>(istream &in, vector<T> &v);

ll find(vll &a, vll &b, int i, int j, ll k) {
  ll n = a.size(), m = b.size();
  if (k <= 0 || (i == n && j == m))
    return 0;
  else if (i == n && k >= b[j]) {
    return 1 + find(a, b, i, j + 1, k - b[j]);
  } else if (j == n && k >= a[i]) {
    return 1 + find(a, b, i + 1, j, k - a[j]);
  } else if (k >= a[i] || k >= b[j]) {
    ll ans1 = 1 + find(a, b, i, j + 1, k - b[j]);
    ll ans2 = 1 + find(a, b, i + 1, j, k - a[i]);
    return max(ans1, ans2);
  } else {
    return 0;
  }
}

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  vll a, b;
  a.resize(n);
  b.resize(m);
  cin >> a >> b;
  cout << find(a, b, 0, 0, k) << endl;
}

int main() {

  FIO;
  // ll test;cin>>test;
  // while(test--)
  solve();
  return 0;
}

template <typename T>
void output_vector(const vector<T> &v, bool debug_print = false,
                   bool add_one = false, int start = -1, int end = -1) {
  if (start < 0)
    start = 0;
  if (end < 0)
    end = int(v.size());

  for (int i = start; i < end; i++)
    (debug_print)
        ? (cerr << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\\n'))
        : (cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\\n'));
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  int i = 0;
  for (T x : v) {
    out << x;
    (i++ != v.size()) ? out << " " : out << "\\n";
  }
  return out;
}

template <typename T> istream &operator>>(istream &in, vector<T> &v) {
  for (T &i : v)
    in >> i;
  return in;
}