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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

/*
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/

#define fast_io                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define md(l, r) l + (r - l) / 2
#define ll long long
#define btw(a, l, r) a.begin() + l, a.begin() + r + 1
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int(x.size()))
#define sqr(x) (x) * (x)
#define endl '\\n'

#define getfiles                                                               \\
  ifstream cin("input.txt");                                                   \\
  ofstream cout("output.txt");
#define clr(x, y) memset((x), (y), sizeof(x))
#define ld long double

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<pair<int, int>>> vvpii;
typedef pair<int, pair<int, int>> piii;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ll, pair<ll, ll>>> vplll;

const ld eps = -1e9;
const long long N = 200001, P = 1e9 + 7, P1 = 1e9 + 13, iINF = INT_MAX,
                INF = 2e9, K = 73, K1 = 79;

ll dstF(ll x, ll y, ll xx, ll yy) { return (sqr(x - xx) + sqr(y - yy)); }

ld dstSqrtF(ll x, ll y, ll xx, ll yy) {
  return (sqrt(sqr(x - xx) + sqr(y - yy)));
}

ll _bp(ll val, ll st, ll p) {
  if (st == 0) {
    return 1;
  }
  ll temp = _bp(val, st / 2, p);
  temp = sqr(temp);
  temp %= p;
  if (st % 2 == 1) {
    temp *= val;
    temp %= p;
  }
  return temp;
}

ll _bp(ll val, ll st) {
  if (st == 0) {
    return 1;
  }
  ll temp = _bp(val, st / 2);
  temp = sqr(temp);
  if (st % 2 == 1) {
    temp *= val;
  }
  return temp;
}

ll nok(ll a, ll b) { return (a / __gcd(a, b) * b); }

/*
ll fact[N];

void buildFacts(ll n, ll p) {
    fact[0] = 1;
    for (ll i = 1; i <= n; i++) {
        fact[i] = (fact[i - 1] * i) % p;
    }
}

ll C(ll n, ll k, ll p = -1) {
    if (p == -1) {
        ll x = fact[n];
        ll y = (fact[k] * fact[n - k]);
        return (x / y);
    } else {
        ll x = fact[n];
        ll y = (fact[k] * fact[n - k]) % p;
        x = (x * _bp(y, p - 2, p)) % p;
        return x;
    }
}
*/

long long k, n, m, a[200100], pr[200100], pr1[200100], b[200100], ans;

void solve() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pr[i] = pr[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    pr1[i] = pr1[i - 1] + b[i];
  }
  ll p = 0;
  while (p <= m && pr1[p] <= k) {
    p++;
  }
  p--;
  // cout << p << endl;
  for (int i = 0; i <= n; i++) {
    if (pr[i] > k) {
      break;
    }
    while (p > 0 && pr[i] + pr1[p] > k) {
      p--;
    }
    ans = max(ans, i + p);
  }
  cout << ans;
}

signed main() {
  fast_io;
  int q;
  q = 1;
  // cin >> q;
  while (q != 0) {
    solve();
    --q;
  }
}

/*
adsdads
*/


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

/*
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/

#define fast_io                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define md(l, r) l + (r - l) / 2
#define ll long long
#define btw(a, l, r) a.begin() + l, a.begin() + r + 1
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int(x.size()))
#define sqr(x) (x) * (x)
#define endl '\\n'

#define getfiles                                                               \\
  ifstream cin("input.txt");                                                   \\
  ofstream cout("output.txt");
#define clr(x, y) memset((x), (y), sizeof(x))
#define ld long double

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<pair<int, int>>> vvpii;
typedef pair<int, pair<int, int>> piii;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ll, pair<ll, ll>>> vplll;

const ld eps = -1e9;
const long long N = 200001, P = 1e9 + 7, P1 = 1e9 + 13, iINF = INT_MAX,
                INF = 2e9, K = 73, K1 = 79;

ll dstF(ll x, ll y, ll xx, ll yy) { return (sqr(x - xx) + sqr(y - yy)); }

ld dstSqrtF(ll x, ll y, ll xx, ll yy) {
  return (sqrt(sqr(x - xx) + sqr(y - yy)));
}

ll _bp(ll val, ll st, ll p) {
  if (st == 0) {
    return 1;
  }
  ll temp = _bp(val, st / 2, p);
  temp = sqr(temp);
  temp %= p;
  if (st % 2 == 1) {
    temp *= val;
    temp %= p;
  }
  return temp;
}

ll _bp(ll val, ll st) {
  if (st == 0) {
    return 1;
  }
  ll temp = _bp(val, st / 2);
  temp = sqr(temp);
  if (st % 2 == 1) {
    temp *= val;
  }
  return temp;
}

ll nok(ll a, ll b) { return (a / __gcd(a, b) * b); }

/*
ll fact[N];

void buildFacts(ll n, ll p) {
    fact[0] = 1;
    for (ll i = 1; i <= n; i++) {
        fact[i] = (fact[i - 1] * i) % p;
    }
}

ll C(ll n, ll k, ll p = -1) {
    if (p == -1) {
        ll x = fact[n];
        ll y = (fact[k] * fact[n - k]);
        return (x / y);
    } else {
        ll x = fact[n];
        ll y = (fact[k] * fact[n - k]) % p;
        x = (x * _bp(y, p - 2, p)) % p;
        return x;
    }
}
*/

long long k, n, m, a[100100], pr[100100], pr1[100100], b[100100], ans;

void solve() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pr[i] = pr[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    pr1[i] = pr1[i - 1] + b[i];
  }
  ll p = 0;
  while (p <= m && pr1[p] <= k) {
    p++;
  }
  p--;
  // cout << p << endl;
  for (int i = 0; i <= n; i++) {
    if (pr[i] > k) {
      break;
    }
    ans = max(ans, i + p);
    while (p >= 0 && pr[i] + pr1[p] > k) {
      p--;
    }
  }
  cout << ans;
}

signed main() {
  fast_io;
  int q;
  q = 1;
  // cin >> q;
  while (q != 0) {
    solve();
    --q;
  }
}

/*
adsdads
*/