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

#include <bits/stdc++.h>
using namespace std;
#define M_PI 3.1415926535
#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define Summon_Tourist                                                         \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll inf = 1e9 + 7;
ll modexp(ll base, ll power) {
  if (power == 0)
    return 1;
  if (power & 1)
    return base * modexp(base, power - 1) % inf;
  return modexp(base * base % inf, power / 2);
}

int main() {
  Summon_Tourist
      // freopen("input.txt" , "r" , stdin );
      ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    vector<ll> a(n), b(m);
    vector<ll> prefa(n, 0), prefb(m, 0);
    ll ma = -1;
    for (ll i = 0; i < n; i++) {
      cin >> a[i];
      if (i == 0)
        prefa[i] = a[i];
      else
        prefa[i] = a[i] + prefa[i - 1];
    }
    for (ll i = 0; i < m; i++) {
      cin >> b[i];
      if (i == 0)
        prefb[i] = b[i];
      else
        prefb[i] = b[i] + prefb[i - 1];
      if (prefb[i] <= k)
        ma = i + 1;
    }
    for (ll i = 0; i < n; i++) {
      ll temp = 0;
      ll tempmax = k;
      if (prefa[i] <= k) {
        temp = i + 1;
        tempmax -= prefa[i];
      }
      ll y = lower_bound(prefb.begin(), prefb.end(), tempmax) - prefb.begin();
      if ((y < m && tempmax < prefb[y]) || y == m) {
        --y;
      }
      temp += (y + 1);
      ma = max(ma, temp);
    }
    cout << ma;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define M_PI 3.1415926535
#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define Summon_Tourist                                                         \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll inf = 1e9 + 7;
ll modexp(ll base, ll power) {
  if (power == 0)
    return 1;
  if (power & 1)
    return base * modexp(base, power - 1) % inf;
  return modexp(base * base % inf, power / 2);
}

int main() {
  Summon_Tourist freopen("input.txt", "r", stdin);
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    vector<ll> a(n), b(m);
    vector<ll> prefa(n, 0), prefb(m, 0);
    ll ma = -1;
    for (ll i = 0; i < n; i++) {
      cin >> a[i];
      if (i == 0)
        prefa[i] = a[i];
      else
        prefa[i] = a[i] + prefa[i - 1];
    }
    for (ll i = 0; i < m; i++) {
      cin >> b[i];
      if (i == 0)
        prefb[i] = b[i];
      else
        prefb[i] = b[i] + prefb[i - 1];
      if (prefb[i] <= k)
        ma = i + 1;
    }
    for (ll i = 0; i < n; i++) {
      ll temp = 0;
      ll tempmax = k;
      if (prefa[i] <= k) {
        temp = i + 1;
        tempmax -= prefa[i];
      }
      ll y = lower_bound(prefb.begin(), prefb.end(), tempmax) - prefb.begin();
      if ((y < m && tempmax < prefb[y]) || y == m) {
        --y;
      }
      temp += (y + 1);
      ma = max(ma, temp);
    }
    cout << ma;
  }
  return 0;
}