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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define maxn 2000006
#define gcd __gcd
#define debug(x...)
#endif

#define ff first
#define endl '\\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950L
#define f(i, x, n) for (int i = x; i <= n; i++)
#define fr(i, x, n) for (int i = x; i >= n; i--)
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;

int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};

int main() {

  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> va(n + 1, 0), vb(m + 1, 0), pa(n + 1, 0), pb(m + 1, 0);

  for (long long i = 1; i <= n; i++)
    cin >> va[i], pa[i] = pa[i - 1] + va[i];
  for (long long i = 1; i <= m; i++)
    cin >> vb[i], pb[i] = pb[i - 1] + vb[i];

  long long ans = 0;
  for (long long i = 0; i <= n; i++) {
    if (pa[i] > k)
      break;
    long long j =
        prev(upper_bound(pb.begin(), pb.end(), k - pa[i])) - pb.begin();
    ans = max(ans, i + j);
  }

  cout << ans;

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define maxn 2000006
#define gcd __gcd
#define debug(x...)
#endif

#define ff first
#define endl '\\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950L
#define f(i, x, n) for (int i = x; i <= n; i++)
#define fr(i, x, n) for (int i = x; i >= n; i--)
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;

int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};

int main() {

  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> va(n), vb(n);
  stack<long long> a, b;

  for (long long i = 0; i <= n - 1; i++)
    cin >> va[i];

  for (long long i = n - 1; i >= 0; i--)
    a.push(va[i]);

  for (long long i = 0; i <= m - 1; i++)
    cin >> vb[i];

  for (long long i = m - 1; i >= 0; i--)
    b.push(vb[i]);

  long long ans = 0;
  while (true) {
    if (a.empty() == true and b.empty() == true)
      break;

    else if (a.empty() == true and b.empty() == false) {
      if (k - b.top() < 0)
        break;
      ans++;
      k -= b.top();
      b.pop();
    }

    else if (a.empty() == false and b.empty() == true) {
      if (k - a.top() < 0)
        break;
      ans++;
      k -= a.top();
      a.pop();
    }

    else {
      long long qa = a.top();
      a.pop();
      long long qb = b.top();
      b.pop();

      if (min(qa, qb) == qa) {
        if (k - qa < 0)
          break;
        ans++;
        k -= qa;
        b.push(qb);
      } else {
        if (k - qb < 0)
          break;
        ans++;
        k -= qb;
        a.push(qa);
      }
    }
  }

  cout << ans;

  return 0;
}