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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pll pair<long long int, long long int>
#define great (long long int)(1e9 + 7)
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fst first
#define scd second

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n], B[m];
  for (ll i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> B[i];
  }
  vector<ll> a, b;
  ll s = 0;
  for (ll i = 0; i < n; i++) {
    s = s + A[i];
    a.pb(s);
  }
  s = 0;
  for (ll i = 0; i < m; i++) {
    s = s + B[i];
    b.pb(s);
  }
  if (k < a[0] && k < b[0]) {
    cout << 0 << endl;
    return;
  }
  ll ans = 0;
  for (ll i = 0; i < n && a[i] <= k; i++) {
    ll ind1, ind2;

    if (*(lower_bound(a.begin(), a.end(), a[i])) == a[i]) {
      auto it1 = lower_bound(a.begin(), a.end(), a[i]);
      ind1 = it1 - a.begin() + 1;
      ll n1 = k - a[i];
      // if(n1<0) break;
      if (*(lower_bound(b.begin(), b.end(), n1)) == n1) {
        auto it2 = lower_bound(b.begin(), b.end(), n1);
        ind2 = it2 - b.begin() + 1;

      } else {
        auto it2 = lower_bound(b.begin(), b.end(), n1);
        ind2 = it2 - b.begin();
      }

    } else {
      auto it1 = lower_bound(a.begin(), a.end(), a[i]);
      ind1 = it1 - a.begin();
      ll n1 = k - a[i];
      // if(n1<0) break;
      if (*(lower_bound(b.begin(), b.end(), n1)) == n1) {
        auto it2 = lower_bound(b.begin(), b.end(), n1);
        ind2 = it2 - b.begin() + 1;

      } else {
        auto it2 = lower_bound(b.begin(), b.end(), n1);
        ind2 = it2 - b.begin();
      }
    }
    ans = max(ans, ind1 + ind2);
  }
  for (ll i = 0; i < m && b[i] <= k; i++) {
    ll ind1, ind2;

    if (*(lower_bound(b.begin(), b.end(), b[i])) == b[i]) {
      auto it1 = lower_bound(b.begin(), b.end(), b[i]);
      ind1 = it1 - b.begin() + 1;
      ll n1 = k - b[i];
      // if(n1<0) break;
      if (*(lower_bound(a.begin(), a.end(), n1)) == n1) {
        auto it2 = lower_bound(a.begin(), a.end(), n1);
        ind2 = it2 - a.begin() + 1;

      } else {
        auto it2 = lower_bound(a.begin(), a.end(), n1);
        ind2 = it2 - a.begin();
      }

    } else {
      auto it1 = lower_bound(b.begin(), b.end(), b[i]);
      ind1 = it1 - b.begin();
      ll n1 = k - b[i];
      // if(n1<0) break;
      if (*(lower_bound(a.begin(), a.end(), n1)) == n1) {
        auto it2 = lower_bound(a.begin(), a.end(), n1);
        ind2 = it2 - a.begin() + 1;

      } else {
        auto it2 = lower_bound(a.begin(), a.end(), n1);
        ind2 = it2 - a.begin();
      }
    }
    ans = max(ans, ind1 + ind2);
  }
  cout << ans << endl;
}
int main() {
  // fastio;
  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pll pair<long long int, long long int>
#define great (long long int)(1e9 + 7)
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fst first
#define scd second

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n], B[m];
  for (ll i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> B[i];
  }
  vector<ll> a, b;
  ll s = 0;
  for (ll i = 0; i < n; i++) {
    s = s + A[i];
    a.pb(s);
  }
  s = 0;
  for (ll i = 0; i < m; i++) {
    s = s + B[i];
    b.pb(s);
  }
  if (k < a[0] && k < b[0]) {
    cout << 0 << endl;
    return;
  }
  ll ans = 0;
  for (ll i = 0; i < n && a[i] <= k; i++) {
    ll ind1, ind2;

    if (*(lower_bound(a.begin(), a.end(), a[i])) == a[i]) {
      auto it1 = lower_bound(a.begin(), a.end(), a[i]);
      ind1 = it1 - a.begin() + 1;
      ll n1 = k - a[i];
      // if(n1<0) break;
      if (*(lower_bound(b.begin(), b.end(), n1)) == n1) {
        auto it2 = lower_bound(b.begin(), b.end(), n1);
        ind2 = it2 - b.begin() + 1;

      } else {
        auto it2 = lower_bound(b.begin(), b.end(), n1);
        ind2 = it2 - b.begin();
      }

    } else {
      auto it1 = lower_bound(a.begin(), a.end(), a[i]);
      ind1 = it1 - a.begin();
      ll n1 = k - a[i];
      // if(n1<0) break;
      if (*(lower_bound(b.begin(), b.end(), n1)) == n1) {
        auto it2 = lower_bound(b.begin(), b.end(), n1);
        ind2 = it2 - b.begin() + 1;

      } else {
        auto it2 = lower_bound(b.begin(), b.end(), n1);
        ind2 = it2 - b.begin();
      }
    }
  }
  for (ll i = 0; i < m && b[i] <= k; i++) {
    ll ind1, ind2;

    if (*(lower_bound(b.begin(), b.end(), b[i])) == b[i]) {
      auto it1 = lower_bound(b.begin(), b.end(), a[i]);
      ind1 = it1 - b.begin() + 1;
      ll n1 = k - b[i];
      // if(n1<0) break;
      if (*(lower_bound(a.begin(), a.end(), n1)) == n1) {
        auto it2 = lower_bound(a.begin(), a.end(), n1);
        ind2 = it2 - a.begin() + 1;

      } else {
        auto it2 = lower_bound(a.begin(), a.end(), n1);
        ind2 = it2 - a.begin();
      }

    } else {
      auto it1 = lower_bound(b.begin(), b.end(), b[i]);
      ind1 = it1 - b.begin();
      ll n1 = k - b[i];
      // if(n1<0) break;
      if (*(lower_bound(a.begin(), a.end(), n1)) == n1) {
        auto it2 = lower_bound(a.begin(), a.end(), n1);
        ind2 = it2 - a.begin() + 1;

      } else {
        auto it2 = lower_bound(a.begin(), a.end(), n1);
        ind2 = it2 - a.begin();
      }
    }
    ans = max(ans, ind1 + ind2);
  }
  cout << ans << endl;
}
int main() {
  // fastio;
  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}