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

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
const ll Mod = 1000000007;
const ll INF = 999999999999999999;
const ll NN = (ll)(1e6 + 5);
int mod(string s, int a) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++)
    ans = (ans * 10 + (int)s[i] - '0') % a;
  return ans;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}
ll power(ll x, unsigned ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
ll hcf(ll a, ll b) {
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
int main() {
  fio;
  /*#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
       freopen("error.txt", "w", stderr);
  #endif*/
  ll TT = 1;
  // cin>>TT;
  while (TT--) {
    ll n, ans = 0, m, k, i, j = 1, sum = 0;
    cin >> n >> m >> k;
    vector<ll> a(n + 1), b(m + 1);
    for (i = 1; i <= n; i++)
      cin >> a[i], a[i] += a[i - 1];
    for (i = 1; i <= m; i++)
      cin >> b[i], b[i] += b[i - 1];
    j = m;
    for (i = 0; i <= n; i++) {
      if (a[i] > k)
        break;
      while (a[i] + b[j] > k)
        j--;
      ans = max(ans, i + j);
    }
    cout << ans;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
const ll Mod = 1000000007;
const ll INF = 999999999999999999;
const ll NN = (ll)(1e6 + 5);
int mod(string s, int a) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++)
    ans = (ans * 10 + (int)s[i] - '0') % a;
  return ans;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}
ll power(ll x, unsigned ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
ll hcf(ll a, ll b) {
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
int main() {
  fio;
  /*#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
       freopen("error.txt", "w", stderr);
  #endif*/
  ll TT = 1;
  // cin>>TT;
  while (TT--) {
    ll n, ans = 0, m, k, i, j = 1, sum = 0;
    cin >> n >> m >> k;
    vector<ll> a(n + 1), b(m + 1);
    for (i = 1; i <= n; i++)
      cin >> a[i];
    for (i = 1; i <= m; i++)
      cin >> b[i];
    for (i = 1; i <= n; i++) {
      if (sum + a[i] > k)
        break;
      sum += a[i];
    }
    if (sum == 0) {
      for (i = 1; i <= m; i++) {
        if (sum + b[i] > k)
          break;
        sum += b[i];
      }
      cout << i - 1;
      return 0;
    }
    for (i = i - 1; i >= 1; i--) {
      for (j = j; j <= m; j++) {
        if (sum + b[j] > k) {
          j--;
          break;
        }
        sum += a[j];
      }
      ans = max(ans, i + j);
      sum -= a[i];
    }
    cout << ans;
  }
}