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

#include <bits/stdc++.h>

// Function Macros:
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define setpre(n) cout << fixed << setprecision(n)

// Type / Class / Struct Definitions:
using namespace std;

using ll = long long;
using ull = unsigned long long;

// Constants:
const int INF = (1ll << 30) - 1; // approx 1.07e9
const ll INFLL = 1ll << 60;      // approx 1.15e18
const int MOD = 1e9 + 7;         // 998244353
const long double EPS = 1.0e-14;

// Global Variables:

// Prototype Declarations:
int gcd(int, int);
template <class T> inline bool chmin(T &, T);
template <class T> inline bool chmax(T &, T);

// ================ Main Loop ================
int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1);
  int x, ans = 0;
  a[0] = 0;
  b[0] = 0;
  rep(i, n) {
    cin >> x;
    a[i + 1] = a[i] + x;
    if (a[i + 1] <= k)
      ans = i + 1;
  }
  rep(i, m) {
    cin >> x;
    b[i + 1] = b[i] + x;
  }

  int kari = 0;
  for (int i = ans; i >= 0; i--) {
    while (kari <= m && a[i] + b[kari] <= k) {
      kari++;
    }
    if (kari > 0)
      kari--;
    chmax(ans, i + kari);
  }
  cout << ans << endl;
  return 0;
}
// ===========================================

// Functions:
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

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;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

// Function Macros:
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define setpre(n) cout << fixed << setprecision(n)

// Type / Class / Struct Definitions:
using namespace std;

using ll = long long;
using ull = unsigned long long;

// Constants:
const int INF = (1ll << 30) - 1; // approx 1.07e9
const ll INFLL = 1ll << 60;      // approx 1.15e18
const int MOD = 1e9 + 7;         // 998244353
const long double EPS = 1.0e-14;

// Global Variables:

// Prototype Declarations:
int gcd(int, int);
template <class T> inline bool chmin(T &, T);
template <class T> inline bool chmax(T &, T);

// ================ Main Loop ================
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  queue<int> a, b;
  int x;
  rep(i, n) {
    cin >> x;
    a.emplace(x);
  }
  rep(i, m) {
    cin >> x;
    b.emplace(x);
  }
  int ans = 0;
  ll now = 0;
  while (now + min(a.front(), b.front()) <= k) {
    if (ans == n + m) {
      break;
    } else if (a.empty()) {
      now += b.front();
      a.pop();
    } else if (b.empty()) {
      now += a.front();
      b.pop();
    } else if (a.front() < b.front()) {
      now += a.front();
      a.pop();
    } else {
      now += b.front();
      b.pop();
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}
// ===========================================

// Functions:
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

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;
}