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

//                   ********** YATIN KWATRA **********

// AC BAKSHI RABBA

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rfo(i, a, b) for (int i = a; i >= b; i--)
#define vii vector<int>
#define vll vector<ll>
#define pq priority_queue
#define uom unordered_map
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<long, long>
#define mll map<ll, ll>
#define mii map<int, int>
#define vvii vector<vii>
#define vvll vector<vll>
#define mod 1000000007
#define pi 3.1415926535897932384626433832795
// #define node                    Node

using namespace std;

void INPUT() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

/*

   -> Check For Overflows
   -> Out of Bounds
   -> Initialisations of global arrays and variables
   -> Its not Always Complex, Try to simplify
   -> write it down and try to solve
   -> Have Faith in urself and in ur approach
   -> If it seems totally out of reach, switch to next :)
   -> And Come Back later, if possible
*/

void solve() {
  int n, m;
  ll t;
  cin >> n >> m >> t;

  vii v1(n), v2(m);

  fo(i, 0, n - 1) cin >> v1[i];
  fo(i, 0, m - 1) cin >> v2[i];

  ll sum = 0;
  int best = 0;

  int i = -1;

  while (i < n) {
    i++;
    if ((t - sum) >= v1[i])
      sum += v1[i];
    else
      break;
  }

  i--;

  int j = -1;

  while (j < m) {
    //  cout << " sum : " << sum << " i: " << i << " j: " << j << endl;
    while (j < m) {
      j++;
      if ((t - sum) >= v2[j])
        sum += v2[j];
      else
        break;
    }
    j--;

    //   cout << i << ' ' << j << endl;

    best = max(best, (i + j + 2));

    if (i >= 0) {
      i--;
      sum -= v1[i + 1];
    } else
      break;
  }

  cout << best << endl;
}
int main() {

  FIO
      // INPUT();

      int t;
  t = 1;
  //    cin >> t;

  while (t--) {
    solve();
  }
  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:

//                   ********** YATIN KWATRA **********

// AC BAKSHI RABBA

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rfo(i, a, b) for (int i = a; i >= b; i--)
#define vii vector<int>
#define vll vector<ll>
#define pq priority_queue
#define uom unordered_map
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<long, long>
#define mll map<ll, ll>
#define mii map<int, int>
#define vvii vector<vii>
#define vvll vector<vll>
#define mod 1000000007
#define pi 3.1415926535897932384626433832795
// #define node                    Node

using namespace std;

void INPUT() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

/*

   -> Check For Overflows
   -> Out of Bounds
   -> Initialisations of global arrays and variables
   -> Its not Always Complex, Try to simplify
   -> write it down and try to solve
   -> Have Faith in urself and in ur approach
   -> If seems totally out of reach, switch to next :)
   -> And Come Back later, if possible
*/

bool cmp(ll a, ll b) { return a <= b; }

void solve() {
  int n, m, t;
  cin >> n >> m >> t;

  vll v1(n), v2(m);

  fo(i, 0, n - 1) cin >> v1[i];
  fo(i, 0, m - 1) cin >> v2[i];

  int idx;

  fo(i, 1, n - 1) v1[i] += v1[i - 1];
  fo(i, 1, m - 1) v2[i] += v2[i - 1];
  /*
          fo(i,0,n-1) cout << v1[i] << " ";
          cout << endl;
          fo(i,0,m-1) cout << v2[i] << " ";
          cout << endl;
  */
  int i = 0, j = 0;
  ll cnt = 0;
  ll op1, op2;

  while (i < n or j < m and t > 0) {
    op1 = op2 = 0;

    if (i < n and (t >= v1[i])) {
      idx = lower_bound(v1.begin() + i, v1.end(), t, cmp) - v1.begin() - 1;
      op1 = idx - i + 1;
      if (idx == n)
        op1--;
    }
    if (j < m and t >= v2[i]) {
      idx = lower_bound(v2.begin() + j, v2.end(), t, cmp) - v2.begin() - 1;
      op2 = idx - j + 1;
      if (idx == m)
        op2--;
    }

    //  cout << op1 << " " << op2 << endl;

    if (op1 and op2) {
      if (op1 > op2)
        i++;
      else if (op2 > op1)
        j++;
      else {
        if (i == 0) {
          if (v1[i] <= v2[j])
            i++;
          else
            j++;
        } else {
          if ((v1[i] - v1[i - 1]) <= (v2[j] - v2[j - 1]))
            i++;
          else
            j++;
        }
      }
      cnt++;
    } else if (op1) {
      cnt++;
      i++;
    } else if (op2) {
      cnt++;
      j++;
    } else
      break;
  }

  cout << cnt << endl;
}
int main() {

  FIO INPUT();

  int t;
  t = 1;
  // cin >> t;

  while (t--) {
    solve();
  }
  return 0;
}