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

/******************************************
 * AUTHOR : Lokesh goel*
 * NICK : InsaneNerd *
 * INSTITUTION : NIT Kurukshetra *
 ******************************************/
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 1000000000
#define N 100005
#define pii pair<int, int>
#define vp vector<pair<int, int>>
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define gsz(x) ((int)(x).size())
#define F first
#define S second
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cerr.tie(NULL);
#define endl "\\n"
#define ms(x, v) memset(x, v, sizeof(x))

int main() {
  fastio;
  ll n, m, k, i, j;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < m; i++) {
    cin >> b[i];
  }
  i = 0;
  j = 0;
  ll cnt = 0;
  // while(i<n&&j<m){
  // 	//cout<<cnt<<" "<<k<<endl;
  // 	if(a[i]<b[j]&&k>=a[i]){
  // 		k-=a[i];
  // 		cnt++;
  // 		i++;
  // 	}
  // 	else if(a[i]>=b[j]&&k>=b[j]){
  // 		k-=b[j];
  // 		cnt++;
  // 		j++;
  // 	}
  // 	else{
  // 		break;
  // 	}
  // }
  while (i < n) {
    // cout<<cnt<<" "<<k<<endl;
    if (k >= a[i]) {
      k -= a[i];
      i++;
      cnt++;
    } else
      break;
  }
  while (j < m) {
    // cout<<cnt<<" "<<k<<" "<<b[j]<<endl;
    if (k >= b[j]) {
      k -= b[j];
      j++;
      cnt++;
    } else
      break;
  }
  if (i == n && j == m) {
    cout << cnt << endl;
    return 0;
  }
  ll maxcnt = cnt;
  i--;
  while (j < m) {
    k -= b[j];
    cnt++;
    while (i >= 0 && k < 0) {
      k += a[i];
      i--;
      cnt--;
    }
    if (k < 0) {
      break;
    }
    maxcnt = max(cnt, maxcnt);
    j++;
  }
  cout << maxcnt << endl;
  return 0;
}

Error: 0

Fixed submission:

/******************************************
 * AUTHOR : Lokesh goel*
 * NICK : InsaneNerd *
 * INSTITUTION : NIT Kurukshetra *
 ******************************************/
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 1000000000
#define N 100005
#define pii pair<int, int>
#define vp vector<pair<int, int>>
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define gsz(x) ((int)(x).size())
#define F first
#define S second
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cerr.tie(NULL);
#define endl "\\n"
#define ms(x, v) memset(x, v, sizeof(x))

int main() {
  fastio;
  ll n, m, k, i, j;
  cin >> n >> m >> k;
  ll a[n + 1], b[n + 1];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < m; i++) {
    cin >> b[i];
  }
  i = 0;
  j = 0;
  ll cnt = 0;
  while (i < n && j < m) {
    // cout<<cnt<<" "<<k<<endl;
    if (a[i] < b[j] && k >= a[i]) {
      k -= a[i];
      cnt++;
      i++;
    } else if (a[i] >= b[j] && k >= b[j]) {
      k -= b[j];
      cnt++;
      j++;
    } else {
      break;
    }
  }
  while (i < n) {
    // cout<<cnt<<" "<<k<<endl;
    if (k >= a[i]) {
      k -= a[i];
      i++;
      cnt++;
    } else
      break;
  }
  while (j < m) {
    // cout<<cnt<<" "<<k<<" "<<b[j]<<endl;
    if (k >= b[j]) {
      k -= b[j];
      j++;
      cnt++;
    } else
      break;
  }
  cout << cnt << endl;

  return 0;
}