Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define bfr(i, a, b) for (lli i = a; i >= b; i--)
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL), cout.tie(NULL);
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define mod 1000000007LL
#define mod2 998244353LL
#define all(vec) vec.begin(), vec.end()
#define ld long double
#define pb push_back
#define ordered_set                                                            \
  tree<lli, null_type, less<lli>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

lli power(lli x, lli y, lli m) {
  lli res = 1;
  while (y > 0) {
    if (y & 1)
      res = ((res % m) * (x % m)) % m;
    y >>= 2;
    x = ((x % m) * (x % m)) % m;
  }
  return res % m;
}

int main() {

  fio

      lli n,
      m, k;
  cin >> n >> m >> k;
  vector<lli> a(n), b(m);
  fr(i, 0, n) cin >> a[i];
  fr(j, 0, n) {
    cin >> b[j];
    if (j > 0)
      b[j] += b[j - 1];
  }

  lli ans = 0, sum = 0;

  vector<lli>::iterator itr;
  itr = lower_bound(all(b), k);
  ans = itr - b.begin();
  if ((*itr) == k)
    ans++;

  fr(i, 0, n) {
    sum += a[i];
    if (k - sum < 0)
      break;
    itr = lower_bound(all(b), k - sum);
    lli temp = itr - b.begin();
    if ((*itr) == k - sum)
      temp++;
    ans = max(i + 1 + temp, ans);
  }

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define bfr(i, a, b) for (lli i = a; i >= b; i--)
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL), cout.tie(NULL);
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define mod 1000000007LL
#define mod2 998244353LL
#define all(vec) vec.begin(), vec.end()
#define ld long double
#define pb push_back
#define ordered_set                                                            \
  tree<lli, null_type, less<lli>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

lli power(lli x, lli y, lli m) {
  lli res = 1;
  while (y > 0) {
    if (y & 1)
      res = ((res % m) * (x % m)) % m;
    y >>= 2;
    x = ((x % m) * (x % m)) % m;
  }
  return res % m;
}

int main() {

  fio

      lli n,
      m, k;
  cin >> n >> m >> k;
  vector<lli> a(n), b(m);
  fr(i, 0, n) cin >> a[i];
  fr(j, 0, m) {
    cin >> b[j];
    if (j > 0)
      b[j] += b[j - 1];
  }

  lli ans = 0, sum = 0;

  vector<lli>::iterator itr;
  itr = lower_bound(all(b), k);
  ans = itr - b.begin();
  if ((*itr) == k)
    ans++;

  fr(i, 0, n) {
    sum += a[i];
    if (k - sum < 0)
      break;
    itr = lower_bound(all(b), k - sum);
    lli temp = itr - b.begin();
    if ((*itr) == k - sum)
      temp++;
    ans = max(i + 1 + temp, ans);
  }

  cout << ans << '\n';

  return 0;
}