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

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define lld long long int
#define rep(i, start, end) for (lld i = start; i < end; i++)
#define newline cout << endl
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define in insert
#define mp make_pair
#define se second
#define fi first
#define starttime                                                              \\
  chrono::time_point<chrono::system_clock> start, end;                         \\
  start = chrono::system_clock::now()
#define showtime                                                               \\
  end = chrono::system_clock::now();                                           \\
  chrono::duration<double> elapsed_seconds = end - start;                      \\
  time_t end_time = chrono::system_clock::to_time_t(end);                      \\
  cout << "elapsed time: " << elapsed_seconds.count() << "s\\n"
#define INF 1e17
lld MOD = (lld)(1e19 + 7);
#define mh make heap
#define ph push_heap
#define poph pop_heap
#define openf                                                                  \\
  freopen("input.txt", "rt", stdin);                                           \\
  freopen("output.txt", "wt", stdout);
using namespace std;
lld add(long long a, long long b) { return ((a % MOD) + (b % MOD)) % MOD; }
lld mul(long long a, long long b) { return ((a % MOD) * (b % MOD)) % MOD; }
lld sub(long long a, long long b) {
  lld ans = ((a % MOD - b % MOD)) % MOD;
  if (ans < 0)
    ans += MOD;
  return ans;
}
lld modInverse(lld a) {
  lld m = MOD, m0 = m, y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    lld q = a / m, t = m;
    m = a % m, a = t, t = y;
    y = x - q * y, x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
lld gcd(lld a, lld b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void MAIN(lld i, lld t);
int get() {
  lld t;
  cin >> t;
  ;
  return t;
}
template <class T> int logd(T name) {
  cout << name << endl;
  return 0;
}
void test(int t) {
  for (lld i = 0; i < t; i++) {
    MAIN(i, t);
    cout << endl;
  }
}
lld power(lld x, lld y) {
  lld res = 1;
  x = x % MOD;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}
#define all(v) (v).begin(), (v).end()
lld popcount(lld x) { return __builtin_popcountll(x); }
lld lcm(lld a, lld b) { return (a * b) / gcd(a, b); }
vector<lld>::iterator minmax(vector<lld> &a, lld i, lld x) {
  auto it = upper_bound(a.begin() + i, a.end(), x);
  return it;
}
void MAIN(lld current_test_case, lld total_test_cases) {
  lld n, m, k;
  cin >> n >> m >> k;
  vector<lld> a(n), b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  vector<lld> prefix_a, prefix_b;
  prefix_a = a;
  prefix_b = b;
  for (int i = 1; i < n; i++)
    prefix_a[i] += prefix_a[i - 1];
  for (int i = 1; i < m; i++)
    prefix_b[i] += prefix_b[i - 1];
  lld sum = 0;
  lld ans = minmax(prefix_b, 0, k) - prefix_b.begin();
  for (int i = 0; i < n; i++) {
    sum += a[i];
    lld temp = 0;
    if (k - sum >= 0)
      temp = i + 1;
    ans =
        max(ans, (lld)(minmax(prefix_b, 0, k - sum) - prefix_b.begin() + temp));
  }
  cout << ans << endl;
}
int main() {
  fastio;
#ifndef ONLINE_JUDGE
  openf
#endif
      test(1);
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define lld long long int
#define rep(i, start, end) for (lld i = start; i < end; i++)
#define newline cout << endl
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define in insert
#define mp make_pair
#define se second
#define fi first
#define starttime                                                              \\
  chrono::time_point<chrono::system_clock> start, end;                         \\
  start = chrono::system_clock::now()
#define showtime                                                               \\
  end = chrono::system_clock::now();                                           \\
  chrono::duration<double> elapsed_seconds = end - start;                      \\
  time_t end_time = chrono::system_clock::to_time_t(end);                      \\
  cout << "elapsed time: " << elapsed_seconds.count() << "s\\n"
#define INF 1e17
lld MOD = (lld)(1e19 + 7);
#define mh make heap
#define ph push_heap
#define poph pop_heap
#define openf                                                                  \\
  freopen("input.txt", "rt", stdin);                                           \\
  freopen("output.txt", "wt", stdout);
using namespace std;
lld add(long long a, long long b) { return ((a % MOD) + (b % MOD)) % MOD; }
lld mul(long long a, long long b) { return ((a % MOD) * (b % MOD)) % MOD; }
lld sub(long long a, long long b) {
  lld ans = ((a % MOD - b % MOD)) % MOD;
  if (ans < 0)
    ans += MOD;
  return ans;
}
lld modInverse(lld a) {
  lld m = MOD, m0 = m, y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    lld q = a / m, t = m;
    m = a % m, a = t, t = y;
    y = x - q * y, x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
lld gcd(lld a, lld b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void MAIN(lld i, lld t);
int get() {
  lld t;
  cin >> t;
  ;
  return t;
}
template <class T> int logd(T name) {
  cout << name << endl;
  return 0;
}
void test(int t) {
  for (lld i = 0; i < t; i++) {
    MAIN(i, t);
    cout << endl;
  }
}
lld power(lld x, lld y) {
  lld res = 1;
  x = x % MOD;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}
#define all(v) (v).begin(), (v).end()
lld popcount(lld x) { return __builtin_popcountll(x); }
lld lcm(lld a, lld b) { return (a * b) / gcd(a, b); }
vector<lld>::iterator minmax(vector<lld> &a, lld i, lld x) {
  auto it = upper_bound(a.begin() + i, a.end(), x);
  return it;
}
void MAIN(lld current_test_case, lld total_test_cases) {
  lld n, m, k;
  cin >> n >> m >> k;
  vector<lld> a(n), b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  lld i = 0, j = 0;
  lld count = 0;
  vector<lld> prefix_a, prefix_b;
  prefix_a = a;
  prefix_b = b;
  for (int i = 1; i < n; i++)
    prefix_a[i] += prefix_a[i - 1];
  for (int i = 1; i < m; i++)
    prefix_b[i] += prefix_b[i - 1];
  lld sum = 0;
  lld ans = minmax(prefix_b, 0, k) - prefix_b.begin();
  for (int i = 0; i < n; i++) {
    sum += a[i];
    lld temp = 0;
    if (k - sum >= 0)
      temp = i + 1;
    //      cout << minmax(prefix_b,0,k-sum) - prefix_b.begin() << " " << temp
    //      <<  " " << k-sum << endl;
    ans =
        max(ans, (lld)(minmax(prefix_b, 0, k - sum) - prefix_b.begin() + temp));
  }
  cout << ans << endl;
}
int main() {
  fastio;
#ifndef ONLINE_JUDGEr
  openf
#endif
      test(1);
  return 0;
}