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

#include <bits/stdc++.h>
using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
using ll = long long;
using PAIR = pair<int, int>;
using Graph = vector<vector<int>>;
// const ll INF = (1LL << 60);
// const double pi=acos(-1.0);
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/**************************************/

/******** User-defined Function *******/

/**************************************/

/********** Main()  function **********/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  ll sum = 0;
  rep(i, m) sum += b[i];
  int last = m - 1;
  ll ans = 0;
  ll now = 0;
  while (sum > k) {
    sum -= b[last];
    last--;
  }

  ans = last + 1;
  rep(i, n) {
    now += a[i];
    while (now + sum > k) {
      if (last == -1)
        break;
      sum -= b[last];
      last--;
    }
    if (now + sum <= k) {
      chmax(ans, (ll)i + 1 + last + 1);
    }
  }

  cout << ans << endl;

  return 0;
}
/********  Main() Ends Here *************/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
using ll = long long;
using PAIR = pair<int, int>;
using Graph = vector<vector<int>>;
// const ll INF = (1LL << 60);
// const double pi=acos(-1.0);
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/**************************************/

/******** User-defined Function *******/
ll n, m;

vector<ll> a(n), b(m);

bool func(int i, int j) {
  if (i >= n && j >= m)
    return true;
  if (i >= n)
    return false;
  if (j >= m)
    return true;

  int num1 = i + 1, num2 = j + 1;
  while (num1 < n && num2 < m) {
    if (a[num1] < b[num2]) {
      return true;
    } else if (a[num1] > b[num2]) {
      return false;
    } else {
      func(num1, num2);
    }
  }
}

/**************************************/

/********** Main()  function **********/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  ll ans = 0;
  ll sum = 0;
  int i = 0, j = 0;
  while (sum <= k) {
    if (i < n && j < m) {
      if (a[i] < b[j]) {
        sum += a[i];
        if (sum <= k) {
          ans++;
          i++;
        } else {
          break;
        }
      } else if (a[i] > b[j]) {
        sum += b[j];
        if (sum <= k) {
          ans++;
          j++;
        } else {
          break;
        }
      } else {
        func(i, j);
      }
    } else if (j < m) {
      sum += b[j];
      if (sum <= k) {
        ans++;
        j++;
      } else {
        break;
      }
    } else if (i < n) {
      sum += a[i];
      if (sum <= k) {
        ans++;
        i++;
      } else {
        break;
      }
    } else {
      break;
    }
  }

  cout << ans << endl;

  return 0;
}
/********  Main() Ends Here *************/