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

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

#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front
#define pob pop_back
#define mp make_pair
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pdd pair<double, double>
#define all(s) s.begin(), s.end()
#define vll vector<ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vull vector<ull>
#define vvi vector<vector<int>>
#define vvll vector<vector<ll>>
#define fi first
#define se second
#define sz(x) x.size()
#define cinarr(n, a)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define coutarr(n, a)                                                          \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << (i + 1 == n ? '\\n' : ' ');
#define fori(i, a, b, x) for (int i = a; i <= b; i += x)
#define ford(i, a, b, x) for (int i = a; i >= b; i -= x)
#define tc                                                                     \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  for (int cs = 1; cs <= tt; cs++)
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
#define reset(x, y) memset(x, y, sizeof x);
#define EPS 1e-10
#define INF 2000000000
#define mod 1000000007
#define debug(val) cerr << "The value of " << #val << " is = " << val << endl;
#define testing cerr << "--------------------------\\n";

int main() {
  IOS ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 5], b[m + 5], prea[n + 5], preb[m + 5];
  prea[0] = preb[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    prea[i] = prea[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    preb[i] = preb[i - 1] + b[i];
  }
  ll res = 0;
  for (int i = 0; i <= n; i++) {
    ll tmp = 0;
    if (prea[i] <= k) {
      ll x = upper_bound(preb + 1, preb + 1 + m, k - prea[i]) - (preb + 1);
      //			debug(k-prea[i])
      //			debug(x)
      tmp = i + x;
    }
    res = max(res, tmp);
  }
  ////	testing
  //	for(int i = 0; i <= m; i++) {
  //		ll tmp=0;
  //		if(preb[i]<=k) {
  //			ll x=upper_bound(prea+1,prea+1+m,k-preb[i])-(prea+1);
  ////			debug(k-preb[i])
  ////			debug(x)
  //			tmp=i+x;
  //		}
  //		res=max(res,tmp);
  //	}
  cout << res << endl;
}

Error: -11

Fixed submission:

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

int main() {
  int N, M;
  unsigned long long K;
  cin >> N >> M >> K;

  unsigned long long A[N], B[M];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }
  int tot = 0;
  int x = 0, y = 0;
  while (K >= 0) {
    if (A[x] <= B[y]) {
      if (K - A[x] >= 0) {
        K -= A[x];
        A[x++];
        tot++;
      } else {
        cout << tot << endl;
        return 0;
      }
    } else {
      if (K - B[y] >= 0) {
        K -= B[y];
        B[y++];
        tot++;
      } else {
        cout << tot << endl;
        return 0;
      }
    }
  }
  cout << tot << endl;
  return 0;
}