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

// I have to work harder than anyone else to make it! I’ll never catch up
// otherwise… Don’t worry about what other people think. Hold your head up high
// and plunge forward.

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

#define P pair<int, int>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define str std::string
#define pb push_back
#define itrt(yo)                                                               \\
  for (auto x : yo) {                                                          \\
    std::cout << x << nl;                                                      \\
  }
#define vec(x) std::vector<x>
#define nl '\\n'
#define F first
#define S second
#define ll long long int
#define ALL(x) x.begin(), x.end()
#define BN(x) x.begin()
#define ED(x) x.end()
#define MAP_traverse(it, x) for (auto it = BN(x); it != ED(x); it++)
#define PI 3.14159265358979323846264338327950L
const unsigned int M = 1000000007;

const int mxN = 2e5;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;

  ll a[mxN + 1], b[mxN + 1];
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i + 1];
    b[i + 1] += b[i];
  }
  int ans = 0;

  for (int i = 0, j = m; i <= n; i++) {
    if (a[i] > k) {
      break;
    }

    while (a[i] + b[j] > k) {
      j--;
    }
    ans = max(i + j, ans);
  }
  cout << ans;
}

Error: WA

Fixed submission:

// I have to work harder than anyone else to make it! I’ll never catch up
// otherwise… Don’t worry about what other people think. Hold your head up high
// and plunge forward.

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

#define P pair<int, int>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define str std::string
#define pb push_back
#define itrt(yo)                                                               \\
  for (auto x : yo) {                                                          \\
    std::cout << x << nl;                                                      \\
  }
#define vec(x) std::vector<x>
#define nl '\\n'
#define F first
#define S second
#define ll long long int
#define ALL(x) x.begin(), x.end()
#define BN(x) x.begin()
#define ED(x) x.end()
#define MAP_traverse(it, x) for (auto it = BN(x); it != ED(x); it++)
#define PI 3.14159265358979323846264338327950L
const unsigned int M = 1000000007;

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  ll a[100000], b[100000];
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i + 1];
    b[i + 1] += b[i];
  }
  int ans = 0;

  for (int i = 0, j = m; i <= n; i++) {
    if (a[i] > k) {
      break;
    }

    while (a[i] + b[j] > k) {
      j--;
    }
    ans = max(i + j, ans);
  }
  cout << ans;
}