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

/*  /$$                                     /$$
      | $$                                    | $$
  /$$$$$$$  /$$$$$$   /$$$$$$$  /$$$$$$   /$$$$$$$  /$$$$$$   /$$$$$$
 /$$__  $$ /$$__  $$ /$$_____/ /$$__  $$ /$$__  $$ /$$__  $$ /$$__  $$
| $$  | $$| $$$$$$$$| $$      | $$  \\ $$| $$  | $$| $$$$$$$$| $$  \\__/
| $$  | $$| $$_____/| $$      | $$  | $$| $$  | $$| $$_____/| $$
|  $$$$$$$|  $$$$$$$|  $$$$$$$|  $$$$$$/|  $$$$$$$|  $$$$$$$| $$
 \\_______/ \\_______/ \\_______/ \\______/  \\_______/ \\_______/|__//$$$$$$ /$$$$$$
                                                               |______/|______/

*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ln cout << endl;
#define vi vector<int>
#define vll vector<long long>
#define sortl(vec) sort(vec.begin(), vec.end());
#define sortr(vec) sort(vec.rbegin(), vec.rend());
#define forn(i, x, n) for (long long i = x; i < n; i++)
#define revn(i, n, x) for (long long i = n; i >= x; i--)
#define in(vec)                                                                \\
  for (auto &it : vec)                                                         \\
    cin >> it;
#define loop(vec) for (auto &it : vec)
#define out(vec)                                                               \\
  for (auto &it : vec)                                                         \\
    cout << it << " ";                                                         \\
  ln
#define ll long long
#define mod 1000000007
#define pb emplace_back
#define mp make_pair
#define um unordered_map
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define pi 3.14159265359
#define o(x) cout << x << endl;
#define CLOCK                                                                  \\
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
using namespace std;

ll bS(ll l, ll r, ll x, vll arr) {
  while (l <= r) {
    ll m = l + (r - l) / 2;

    if (arr[m] == x)
      return m;

    if (arr[m] < x)
      l = m + 1;

    else
      r = m - 1;
  }

  return -1;
}

void primeFactors(ll n) {
  while (n % 2 == 0) {
    // cout << 2 << " ";
    n = n / 2;
  }

  for (ll i = 3; i <= sqrt(n); i += 2) {
    while (n % i == 0) {
      // cout << i << " ";
      n = n / i;
    }
  }

  if (n > 2)
    cout << n << " ";
}

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;

    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

ll modulo(ll a, ll b) {
  ll c = a % b;
  return (c < 0) ? c + b : c;
}

int main() {
  // in(vec) works for size-defined type vectors

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input1.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output1.txt", "w", stdout);
#endif

  fastio ll t = 1;
  // cin >> t;
  while (t--) {
    ll n, m, K;
    cin >> n >> m >> K;

    vll a(n);
    vll b(m);
    in(a);
    in(b);

    forn(i, 1, n) { a[i] += a[i - 1]; }

    forn(i, 1, m) { b[i] += b[i - 1]; }

    ll maxa = 0, it2;

    for (ll i = 0; a[i] <= K && i < n; i++) {
      it2 = upper_bound(b.begin(), b.end(), K - a[i]) - b.begin() - 1;
      maxa = max(maxa, (it2 + i + 2));
    }

    for (ll i = 0; b[i] <= K && i < m; i++) {
      it2 = upper_bound(a.begin(), a.end(), K - b[i]) - a.begin() - 1;
      maxa = max(maxa, (it2 + i + 2));
    }

    o(maxa)
  }

  CLOCK
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

/*  /$$                                     /$$
      | $$                                    | $$
  /$$$$$$$  /$$$$$$   /$$$$$$$  /$$$$$$   /$$$$$$$  /$$$$$$   /$$$$$$
 /$$__  $$ /$$__  $$ /$$_____/ /$$__  $$ /$$__  $$ /$$__  $$ /$$__  $$
| $$  | $$| $$$$$$$$| $$      | $$  \\ $$| $$  | $$| $$$$$$$$| $$  \\__/
| $$  | $$| $$_____/| $$      | $$  | $$| $$  | $$| $$_____/| $$
|  $$$$$$$|  $$$$$$$|  $$$$$$$|  $$$$$$/|  $$$$$$$|  $$$$$$$| $$
 \\_______/ \\_______/ \\_______/ \\______/  \\_______/ \\_______/|__//$$$$$$ /$$$$$$
                                                               |______/|______/

*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ln cout << endl;
#define vi vector<int>
#define vll vector<long long>
#define sortl(vec) sort(vec.begin(), vec.end());
#define sortr(vec) sort(vec.rbegin(), vec.rend());
#define forn(i, x, n) for (long long i = x; i < n; i++)
#define revn(i, n, x) for (long long i = n; i >= x; i--)
#define in(vec)                                                                \\
  for (auto &it : vec)                                                         \\
    cin >> it;
#define loop(vec) for (auto &it : vec)
#define out(vec)                                                               \\
  for (auto &it : vec)                                                         \\
    cout << it << " ";                                                         \\
  ln
#define ll long long
#define mod 1000000007
#define pb emplace_back
#define mp make_pair
#define um unordered_map
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define pi 3.14159265359
#define o(x) cout << x << endl;
#define CLOCK                                                                  \\
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
using namespace std;

ll bS(ll l, ll r, ll x, vll arr) {
  while (l <= r) {
    ll m = l + (r - l) / 2;

    if (arr[m] == x)
      return m;

    if (arr[m] < x)
      l = m + 1;

    else
      r = m - 1;
  }

  return -1;
}

void primeFactors(ll n) {
  while (n % 2 == 0) {
    // cout << 2 << " ";
    n = n / 2;
  }

  for (ll i = 3; i <= sqrt(n); i += 2) {
    while (n % i == 0) {
      // cout << i << " ";
      n = n / i;
    }
  }

  if (n > 2)
    cout << n << " ";
}

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;

    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

ll modulo(ll a, ll b) {
  ll c = a % b;
  return (c < 0) ? c + b : c;
}

int main() {
  // in(vec) works for size-defined type vectors

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input1.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output1.txt", "w", stdout);
#endif

  fastio ll t = 1;
  // cin >> t;
  while (t--) {
    ll n, m, K;
    cin >> n >> m >> K;

    vll a(n);
    vll b(m);
    in(a);
    in(b);
    sortl(a);
    sortl(b);
    forn(i, 1, n) { a[i] += a[i - 1]; }

    forn(i, 1, m) { b[i] += b[i - 1]; }

    ll maxa;

    auto ita = upper_bound(a.begin(), a.end(), K) - a.begin();

    auto itb = upper_bound(b.begin(), b.end(), K) - b.begin();

    maxa = max(ita, itb);

    ll it1, it2;

    for (ll i = 0; a[i] < K; i++) {

      it1 = upper_bound(a.begin(), a.end(), a[i]) - a.begin();

      it2 = upper_bound(b.begin(), b.end(), K - a[it1 - 1]) - b.begin();

      maxa = max(maxa, it1 + it2);
    }

    o(maxa)
  }

  CLOCK
  return 0;
}