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

/*
 ▓█████▄  ▄████▄  ▓██   ██▓ ██▓███   ██░ ██ ▓█████  ██▀███
▒██▀ ██▌▒██▀ ▀█   ▒██  ██▒▓██░  ██▒▓██░ ██▒▓█   ▀ ▓██ ▒ ██▒
 ░██   █▌▒▓█    ▄   ▒██ ██░▓██░ ██▓▒▒██▀▀██░▒███   ▓██ ░▄█ ▒
 ░▓█▄   ▌▒▓▓▄ ▄██▒  ░ ▐██▓░▒██▄█▓▒ ▒░▓█ ░██ ▒▓█  ▄ ▒██▀▀█▄
 ░▒████▓ ▒ ▓███▀ ░  ░ ██▒▓░▒██▒ ░  ░░▓█▒░██▓░▒████▒░██▓ ▒██▒
  ▒▒▓  ▒ ░ ░▒ ▒  ░   ██▒▒▒ ▒▓▒░ ░  ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░
  ░ ▒  ▒   ░  ▒    ▓██ ░▒░ ░▒ ░      ▒ ░▒░ ░ ░ ░  ░  ░▒ ░ ▒░
  ░ ░  ░ ░         ▒ ▒ ░░  ░░        ░  ░░ ░   ░     ░░   ░
    ░    ░ ░       ░ ░               ░  ░  ░   ░  ░   ░
  ░      ░         ░ ░
*/
// focus less on how much problem you should solve within a fixed time and focus
// more on getting the problem solved and learning something from it
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define sedes set<ll, greater<ll>>
#define seasc set<ll>
#define pb push_back
#define mkp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define umap unordered_map<ll, ll>
#define map map<ll, ll>
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define mems(a, i) memset(a, i, sizeof(a))
#define endl '\\n'
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define printx(x) printf("%lld\\n", x);
#define printxy(x, y) cout << x << y << endl;
#define printxyz(x, y, z) cout << x << y << z << endl;
#define deba(a) cout << #a << a << endl;
#define debab(a, b) cout << #a << a << #b << b << endl;
int main() {
  fast;
  ll n;
  ll m;
  ll k;
  cin >> n >> m >> k;

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

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

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

  while (i < n && sum + a[i] <= k) {
    sum += a[i];
    i++;
  }
  count = i;

  while (j < m && i >= 0) {
    sum += b[j];
    j++;
    while (sum > k && i > 0) {
      i--;
      sum -= a[i];
    }
    if (sum <= k && i + j > count)
      count = i + j;
  }
  cout << count << endl;
  return 0;
}

Error: 0

Fixed submission:

/*
 ▓█████▄  ▄████▄  ▓██   ██▓ ██▓███   ██░ ██ ▓█████  ██▀███
▒██▀ ██▌▒██▀ ▀█   ▒██  ██▒▓██░  ██▒▓██░ ██▒▓█   ▀ ▓██ ▒ ██▒
 ░██   █▌▒▓█    ▄   ▒██ ██░▓██░ ██▓▒▒██▀▀██░▒███   ▓██ ░▄█ ▒
 ░▓█▄   ▌▒▓▓▄ ▄██▒  ░ ▐██▓░▒██▄█▓▒ ▒░▓█ ░██ ▒▓█  ▄ ▒██▀▀█▄
 ░▒████▓ ▒ ▓███▀ ░  ░ ██▒▓░▒██▒ ░  ░░▓█▒░██▓░▒████▒░██▓ ▒██▒
  ▒▒▓  ▒ ░ ░▒ ▒  ░   ██▒▒▒ ▒▓▒░ ░  ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░
  ░ ▒  ▒   ░  ▒    ▓██ ░▒░ ░▒ ░      ▒ ░▒░ ░ ░ ░  ░  ░▒ ░ ▒░
  ░ ░  ░ ░         ▒ ▒ ░░  ░░        ░  ░░ ░   ░     ░░   ░
    ░    ░ ░       ░ ░               ░  ░  ░   ░  ░   ░
  ░      ░         ░ ░
*/
// focus less on how much problem you should solve within a fixed time and focus
// more on getting the problem solved and learning something from it
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define sedes set<ll, greater<ll>>
#define seasc set<ll>
#define pb push_back
#define mkp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define umap unordered_map<ll, ll>
#define map map<ll, ll>
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define mems(a, i) memset(a, i, sizeof(a))
#define endl '\\n'
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define printx(x) printf("%lld\\n", x);
#define printxy(x, y) cout << x << y << endl;
#define printxyz(x, y, z) cout << x << y << z << endl;
#define deba(a) cout << #a << a << endl;
#define debab(a, b) cout << #a << a << #b << b << endl;
int main() {
  fast;
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 1], b[n + 1];
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];
  ll sum = 0, i = 0, j = 0, cnt = 0;
  while (i < n or j < m) {
    if (a[i] < b[j] and i < n)
      sum += a[i], i++;
    else if (b[j] < a[i] and j < m)
      sum += b[j], j++;
    cnt++;
    if (sum == k)
      break;
    if (sum > k) {
      cnt--;
      break;
    }
  }
  cout << cnt << endl;
  return 0;
}