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

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i, a, b) for (lli i = a; i < b; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define mod2 1000000006
#define inf 1000000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<lli>
#define vii vector<int>
#define is indexed_set
#define eps 0.000001
#define endl '\\n'
#define mo 998244353
#define pi 3.141592653589793
#define FF first
#define SS second
using namespace std;

const lli dx4[] = {-1, 0, 1, 0};
const lli dy4[] = {0, 1, 0, -1};
const lli dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const lli dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};

typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 mt(chrono::steady_clock::now()
                  .time_since_epoch()
                  .count()); // can give negative values too

#define N 2005

void solve() {
  lli n, m, k;
  cin >> n >> m >> k;

  vll a, b;

  lli sum = 0;
  loopl(i, 0, n) {
    lli x;
    cin >> x;

    sum += x;
    a.pb(sum);
  }

  sum = 0;
  loopl(j, 0, m) {
    lli x;
    cin >> x;
    sum += x;
    b.pb(sum);
  }

  lli p1 = 0, p2 = m - 1;

  lli ans = 0;

  loopl(i, 0, n) {
    if (a[i] <= k)
      ans = max(ans, i + 1);
  }

  loopl(i, 0, m) {
    if (b[i] <= k)
      ans = max(ans, i + 1);
  }

  while (p1 < n && p2 >= 0) {
    while (p2 >= 0 && a[p1] + b[p2] > k)
      p2--;

    lli cnt = 0;
    if (p1 >= 0 && p1 < n)
      cnt += a[p1];
    if (p2 >= 0 && p2 < m)
      cnt += b[p2];

    if (cnt <= k)
      ans = max(ans, p1 + p2 + 2);

    p1++;
  }

  cout << ans;
}

int main() {
  ////////////////////////////////////////////
  fastio;
  ////////////////////////////////////////////
  lli t;
  t = 1;
  while (t--) {
    solve();
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i, a, b) for (lli i = a; i < b; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define mod2 1000000006
#define inf 1000000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<lli>
#define vii vector<int>
#define is indexed_set
#define eps 0.000001
#define endl '\\n'
#define mo 998244353
#define pi 3.141592653589793
#define FF first
#define SS second
using namespace std;

const lli dx4[] = {-1, 0, 1, 0};
const lli dy4[] = {0, 1, 0, -1};
const lli dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const lli dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};

typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 mt(chrono::steady_clock::now()
                  .time_since_epoch()
                  .count()); // can give negative values too

#define N 2005

void solve() {
  lli n, m, k;
  cin >> n >> m >> k;

  vll a, b;

  lli sum = 0;
  loopl(i, 0, n) {
    lli x;
    cin >> x;

    sum += x;
    a.pb(sum);
  }

  sum = 0;
  loopl(j, 0, m) {
    lli x;
    cin >> x;
    sum += x;
    b.pb(sum);
  }

  lli p1 = 0, p2 = m - 1;

  lli ans = 0;

  loopl(i, 0, n) {
    if (a[i] <= k)
      ans = max(ans, i + 1);
  }

  loopl(i, 0, m) {
    if (b[i] <= k)
      ans = max(ans, i + 1);
  }

  while (p1 < n && p2 >= 0) {
    while (p2 >= 0 && a[p1] + b[p2] > k)
      p2--;

    lli cnt = 0;
    if (p1 >= 0 && p1 < n)
      cnt += a[p1];
    if (p2 >= 0 && p2 < m)
      cnt += a[p2];

    if (cnt <= k)
      ans = max(ans, p1 + p2 + 2);

    p1++;
  }

  cout << ans;
}

int main() {
  ////////////////////////////////////////////
  fastio;

  ////////////////////////////////////////////
  lli t;
  t = 1;
  while (t--) {
    solve();
  }
}