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

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define ll long long
#define f(i, n) for (int i = 0; i < n; i++)
#define ff(i, n) for (int i = 1; i <= n; i++)
#define F first
#define S second
#define vpii vector<pair<int, int>>
#define sz size()
#define all(v) v.begin(), v.end()
#define pb push_back
#define PB pop_back
#define vl vector<int>
#define mp make_pair
#define mod 1000000007
typedef unsigned long long ull;
const int N = 200005;
const int MOD = 998244353;

void solve() {
  int n, m, k, ans = 0, cnt = 0, mx = LLONG_MIN;
  cin >> n >> m >> k;
  int a[n + 5], b[m + 5];
  f(i, n) cin >> a[i];
  f(i, m) cin >> b[i];
  ff(i, n - 1) a[i] += a[i - 1];
  ff(i, m - 1) b[i] += b[i - 1];
  int p = lower_bound(b, b + m, k) - b, q;
  if (b[p] == k)
    mx = (p + 1);
  else
    mx = p;
  // cout<<p<<" "<<mx<<" here\\n";
  f(i, n) {
    if (a[i] > k)
      break;
    cnt = 0;
    cnt += i + 1;
    q = k - a[i];
    p = lower_bound(b, b + m, q) - b;
    // cout<<b[p]<<" "<<q<<" ";
    if (q > 0 && p >= 0 && p < m) {
      if (b[p] == q)
        cnt += (p + 1);
      else
        cnt += p;
      // cout<<q<<" "<<p+1<<" "<<cnt<<" hi\\n";
    }
    mx = max(mx, cnt);
  }
  // cout<<endl;

  cout << max(mx, 0ll);
}

int32_t main() {
  fast int t;
  t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define ll long long
#define f(i, n) for (int i = 0; i < n; i++)
#define ff(i, n) for (int i = 1; i <= n; i++)
#define F first
#define S second
#define vpii vector<pair<int, int>>
#define sz size()
#define all(v) v.begin(), v.end()
#define pb push_back
#define PB pop_back
#define vl vector<int>
#define mp make_pair
#define mod 1000000007
typedef unsigned long long ull;
const int N = 200005;
const int MOD = 998244353;

void solve() {
  int n, m, k, ans = 0, cnt = 0, mx = LLONG_MIN;
  cin >> n >> m >> k;
  int a[n + 5], b[m + 5];
  f(i, n) cin >> a[i];
  f(i, m) cin >> b[i];
  ff(i, n - 1) a[i] += a[i - 1];
  ff(i, m - 1) b[i] += b[i - 1];
  int p = lower_bound(b, b + m, k) - b, q;
  if (a[p] == k)
    mx = (p + 1);
  else
    mx = p;
  // cout<<p<<" "<<mx<<" here\\n";
  f(i, n) {
    if (a[i] > k)
      break;
    cnt = 0;
    cnt += i + 1;
    q = k - a[i];
    p = lower_bound(b, b + m, q) - b;
    // cout<<b[p]<<" "<<q<<" ";
    if (q > 0) {
      if (b[p] == q)
        cnt += (p + 1);
      else
        cnt += p;
      // cout<<q<<" "<<p+1<<" "<<cnt<<" hi\\n";
    }
    mx = max(mx, cnt);
  }
  // cout<<endl;

  cout << max(mx, 0ll);
}

int32_t main() {
  fast int t;
  t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}