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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

void print(vector<ll> X) {
  for (int i = 0; i < X.size(); i++) {
    cout << X[i] << " ";
  }
  cout << endl;
  return;
}

bool pos(vector<ll> &A1, vector<ll> &B1, int n, int m, ll c, ll k) {
  if (c == 0) {
    return true;
  }
  if (c <= n && A1[c - 1] <= k) {
    return true;
  }
  if (c <= m && B1[c - 1] <= k) {
    return true;
  }
  for (int i = 1; i < c; i++) {
    if (i <= n && c - i <= m && A1[i - 1] + B1[c - 1 - i] <= k) {
      // cout<<c<<" "<<i<<" "<<c-i<<" "<<A1[i-1]<<" "<<B1[c-1-i]<<endl;
      return true;
    }
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  ll k;
  cin >> k;
  vector<ll> A(n, 0);
  vector<ll> B(m, 0);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> B[i];
  }
  vector<ll> A1(n, 0);
  A1[0] = A[0];
  vector<ll> B1(m, 0);
  B1[0] = B[0];
  for (int i = 1; i < n; i++) {
    A1[i] = A1[i - 1] + A[i];
  }
  for (int i = 1; i < m; i++) {
    B1[i] = B1[i - 1] + B[i];
  }
  ll a = 0;
  ll b = n + m;
  while (b - a > 3) {
    ll c = (a + b) / 2;
    if (pos(A1, B1, n, m, c, k)) {
      a = c;
    } else {
      b = c;
    }
  }
  while (!pos(A1, B1, n, m, b, k)) {
    b--;
  }
  cout << b << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

void print(vector<ll> X) {
  for (int i = 0; i < X.size(); i++) {
    cout << X[i] << " ";
  }
  cout << endl;
  return;
}

bool pos(vector<ll> &A1, vector<ll> &B1, int n, int m, ll c, ll k) {
  if (c == 0) {
    return true;
  }
  for (int i = 0; i <= c; i++) {
    if (i <= n && c - i <= m && A1[i - 1] + B1[c - 1 - i] <= k) {
      // cout<<c<<" "<<i<<" "<<c-i<<" "<<A1[i-1]<<" "<<B1[c-1-i]<<endl;
      return true;
    }
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  ll k;
  cin >> k;
  vector<ll> A(n, 0);
  vector<ll> B(m, 0);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> B[i];
  }
  vector<ll> A1(n, 0);
  A1[0] = A[0];
  vector<ll> B1(n, 0);
  B1[0] = B[0];
  for (int i = 1; i < n; i++) {
    A1[i] = A1[i - 1] + A[i];
  }
  for (int i = 1; i < m; i++) {
    B1[i] = B1[i - 1] + B[i];
  }
  ll a = 0;
  ll b = n + m;
  while (b - a > 3) {
    ll c = (a + b) / 2;
    if (pos(A1, B1, n, m, c, k)) {
      a = c;
    } else {
      b = c;
    }
  }
  while (!pos(A1, B1, n, m, b, k)) {
    b--;
  }
  cout << b << endl;
  return 0;
}