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

#pragma comment(linker, "/stack:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
// #include"functions.h"
// #include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pff pair<float, float>
#define pdd pair<double, double>
#define psi pair<string, int>
#define pis pair<int, string>
#define ppi pair<pii, int>
#define pdi pair<pii, pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-5
#define DEPS 1e-9
#define LDEPS 1e-13
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a, b) for (int a = 0; a < b; a++)
#define rep(a, b) for (int a = 0; a < b; a++)
#define forn(a, n) for (int a = 1; a <= n; a++)
#define forr(a, n) for (int a = n; a >= 1; a--)
#define repn(a, n, p) for (int a = 0; a < n; a += p)
#define repr(a, n) for (int a = n - 1; a >= 0; a--)
#define foreach(a, b) for (__typeof(b.begin()) a = b.begin(); a != b.end(); a++)
#define fillemp(a) memset(a, 0, sizeof(a))
#define fillinf(a) memset(a, 0x3f, sizeof(a))
#define frestream                                                              \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define stdf(a, b)                                                             \\
  freopen(a, "r", stdin);                                                      \\
  freopen(b, "w", stdout);
#define inputf(a) freopen(a, "r", stdin);
#define outputf(a) freopen(a, "w", stdout);
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

vector<ll> a, b, p, q;
int main(int argc, char **argv) {
  ll n, m, k;
  cin >> n >> m >> k;
  p.pb(0ll), q.pb(0ll);
  rep(i, n) {
    ll x;
    cin >> x;
    a.pb(x);
    p.pb(p[i] + a[i]);
  }
  rep(i, m) {
    ll x;
    cin >> x;
    b.pb(x);
    q.pb(q[i] + b[i]);
  }
  int mx = 0;
  rep(j, n + 1) {
    int i = j - 1;
    if (p[i + 1] > k)
      break;
    int l = 0, r = m, mid, cur = 0;
    while (l <= r) {
      mid = (l + r) / 2;
      if (p[i + 1] + q[mid] <= k) {
        l = mid + 1;
        cur = mid + i + 1;
      } else {
        r = mid - 1;
      }
    }
    mx = max(mx, cur);
  }
  cout << mx;
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#pragma comment(linker, "/stack:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
// #include"functions.h"
// #include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pff pair<float, float>
#define pdd pair<double, double>
#define psi pair<string, int>
#define pis pair<int, string>
#define ppi pair<pii, int>
#define pdi pair<pii, pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-5
#define DEPS 1e-9
#define LDEPS 1e-13
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a, b) for (int a = 0; a < b; a++)
#define rep(a, b) for (int a = 0; a < b; a++)
#define forn(a, n) for (int a = 1; a <= n; a++)
#define forr(a, n) for (int a = n; a >= 1; a--)
#define repn(a, n, p) for (int a = 0; a < n; a += p)
#define repr(a, n) for (int a = n - 1; a >= 0; a--)
#define foreach(a, b) for (__typeof(b.begin()) a = b.begin(); a != b.end(); a++)
#define fillemp(a) memset(a, 0, sizeof(a))
#define fillinf(a) memset(a, 0x3f, sizeof(a))
#define frestream                                                              \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define stdf(a, b)                                                             \\
  freopen(a, "r", stdin);                                                      \\
  freopen(b, "w", stdout);
#define inputf(a) freopen(a, "r", stdin);
#define outputf(a) freopen(a, "w", stdout);
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

int main(int argc, char **argv) {
  ll n, m, k;
  cin >> n >> m >> k;
  vll a(n), b(m), p(n), q(m);
  rep(i, n) {
    cin >> a[i];
    p[i + 1] = p[i] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    q[i + 1] = q[i] + b[i];
  }
  int mx = 0;
  rep(i, n) {
    if (p[i + 1] > k)
      break;
    int pos = lower_bound(all(q), k - p[i + 1]) - q.begin();
    if (p[i + 1] + q[pos] > k)
      pos--;
    //	cout<<q[pos]<<" "<<k-p[i+1]<<endl;
    //	cout<<i<<" "<<pos<<" "<<mx<<endl;
    mx = max(mx, pos + i + 1);
  }
  cout << mx;
  return 0;
}