Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define pf push_front
#define fi first
#define se second
#define INF 1e10
const double pi = 3.14159265358979323;
const int mod = 998244353;
const int MAX = 1e7 + 5;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
int modinv(int y) { return binpow(y, mod - 2); }

int fact[500000 + 5];
void precal() {
  fact[0] = 1;
  for (int i = 1; i < 500000 + 5; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
  }
}
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif
  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    int a[n], b[n];
    f(i, 0, n) cin >> a[i];
    f(i, 0, m) cin >> b[i];
    vector<int> p1, p2;
    p1.pb(0);
    p1.pb(a[0]);
    f(i, 1, n) {
      int x = p1.back();
      p1.pb(x + a[i]);
    }
    p2.pb(0);
    p2.pb(b[0]);
    f(i, 1, m) {
      int x = p2.back();
      p2.pb(x + b[i]);
    }
    int mx = 0;
    int j = p2.size() - 1;
    f(i, 0, p1.size()) {
      if (p1[i] > k)
        break;
      while (p2[j] > k - p1[i]) {
        j -= 1;
      }
      mx = max(mx, i + j);
    }
    cout << mx << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define pf push_front
#define fi first
#define se second
#define INF 1e10
const double pi = 3.14159265358979323;
const int mod = 998244353;
const int MAX = 1e7 + 5;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
int modinv(int y) { return binpow(y, mod - 2); }

int fact[500000 + 5];
void precal() {
  fact[0] = 1;
  for (int i = 1; i < 500000 + 5; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
  }
}
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif
  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    int a[n + 1], b[m + 1];
    f(i, 0, n) cin >> a[i];
    f(i, 0, m) cin >> b[i];
    vector<int> p1, p2;
    p1.pb(0);
    p1.pb(a[0]);
    f(i, 1, n) {
      int x = p1.back();
      p1.pb(x + a[i]);
    }
    p2.pb(0);
    p2.pb(b[0]);
    f(i, 1, m) {
      int x = p2.back();
      p2.pb(x + b[i]);
    }
    int mx = 0;
    int j = p2.size() - 1;
    f(i, 0, p1.size()) {
      if (p1[i] > k)
        break;
      while (p2[j] > k - p1[i]) {
        j -= 1;
      }
      mx = max(mx, i + j);
    }
    cout << mx << endl;
  }
}