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

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define vec_input(v)                                                           \\
  for (auto it = v.begin(); it != v.end(); it++) {                             \\
    cin >> *it;                                                                \\
  }
#define vec_output(v)                                                          \\
  for (auto it = v.begin(); it != v.end(); it++) {                             \\
    if (it != v.begin())                                                       \\
      cout << " ";                                                             \\
    cout << *it;                                                               \\
  }                                                                            \\
  cout << endl;
#define vec_debug(v)                                                           \\
  for (auto it = v.begin(); it != v.end(); it++) {                             \\
    cerr << *it << " ";                                                        \\
  }                                                                            \\
  cerr << endl;
#define yn(ans) cout << (ans ? "Yes" : "No") << endl
#define YN(ans) cout << (ans ? "YES" : "NO") << endl
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
template <typename T> T digitsum(T n);
template <typename T> bool isPrime(T n);
template <typename T> vector<pair<T, T>> prime_factor(T n);
long long int intpow(long long int, long long int);
template <typename T> T intlog(T);
long long int combination(long long int, long long int);
long long int arith_sum1(long long int, long long int, long long int);
long long int arith_sum2(long long int, long long int, long long int);
long long int series_sum(long long int);

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  ll x, sum = 0;
  vec_input(a);
  rep(i, m) {
    cin >> x;
    sum += x;
    b[i] = sum;
  }
  sum = 0;
  ll ans = (ll)(upper_bound(all(b), k) - b.begin());
  ll hikaku;
  rep(i, n) {
    sum += a[i];
    if (sum <= k) {
      hikaku = i + 1 + (ll)(upper_bound(all(b), k - sum) - b.begin());
      ans = max(ans, hikaku);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define vec_input(v)                                                           \\
  for (auto it = v.begin(); it != v.end(); it++) {                             \\
    cin >> *it;                                                                \\
  }
#define vec_output(v)                                                          \\
  for (auto it = v.begin(); it != v.end(); it++) {                             \\
    if (it != v.begin())                                                       \\
      cout << " ";                                                             \\
    cout << *it;                                                               \\
  }                                                                            \\
  cout << endl;
#define vec_debug(v)                                                           \\
  for (auto it = v.begin(); it != v.end(); it++) {                             \\
    cerr << *it << " ";                                                        \\
  }                                                                            \\
  cerr << endl;
#define yn(ans) cout << (ans ? "Yes" : "No") << endl
#define YN(ans) cout << (ans ? "YES" : "NO") << endl
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;

// 等差数列の和を求める(初項,項数,公差)
long long int arith_sum1(long long int a, long long int n, long long int d) {
  return n * (2 * a + (n - 1) * d) / 2;
}
// 等差数列の和を求める(初項,末項,項数)
long long int arith_sum2(long long int a, long long int l, long long int n) {
  return n * (a + l) / 2;
}
// 初項1，末項n，項数1の数列の和を求める
long long int series_sum(long long int n) { return n * (n + 1) / 2; }
// 各桁の数の和を求める
template <typename T> T digitsum(T n) {
  string s = to_string(n);
  T sum = 0;
  T d = 1;
  for (T e = 0; e < s.length(); e++) {
    sum += (n / d) % 10;
    d *= 10;
  }
  return sum;
}
template <typename T> bool isPrime(T n) {
  if (n <= 1)
    return false;
  if (n == 2 || n == 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (T q = 5; q * q <= n; q += 4) {
    if (n % q == 0)
      return false;
    q += 2;
    if (n % q == 0)
      return false;
  }
  return true;
}
template <typename T> vector<pair<T, T>> prime_factor(T n) {
  vector<pair<T, T>> ret;
  T tmp = 0;
  if (n % 2 == 0) {
    tmp = 0;
    while (n % 2 == 0) {
      tmp++;
      n /= 2;
    }
    ret.push_back(make_pair(2, tmp));
  }
  if (n % 3 == 0) {
    tmp = 0;
    while (n % 3 == 0) {
      tmp++;
      n /= 3;
    }
    ret.push_back(make_pair(3, tmp));
  }
  for (T i = 5; i * i <= n; i += 4) {
    if (n % i == 0) {
      tmp = 0;
      while (n % i == 0) {
        tmp++;
        n /= i;
      }
      ret.push_back(make_pair(i, tmp));
    }
    i += 2;
    if (n % i == 0) {
      tmp = 0;
      while (n % i == 0) {
        tmp++;
        n /= i;
      }
      ret.push_back(make_pair(i, tmp));
    }
  }
  if (n != 1)
    ret.push_back(make_pair(n, 1));
  return ret;
}
long long int intpow(long long int x, long long int n) {
  long long int ans = 1;
  for (int i = 0; i < n; i++) {
    ans *= x;
  }
  return ans;
}
template <typename T> T intlog(T x) {
  string a = to_string(x);
  return a.length() - 1;
}
long long int combination(long long int a, long long int b) {
  queue<long long int> bunsi, bunbo;
  long long int ans = 1;
  b = min(b, a - b);
  for (int i = 0; i < b; i++) {
    bunsi.push(a - i);
    if (b - i >= 2)
      bunbo.push(b - i);
  }
  while (bunsi.size() != 0 || bunbo.size() != 0) {
    ans *= bunsi.front();
    bunsi.pop();
    if (ans % bunbo.front() == 0) {
      ans /= bunbo.front();
      bunbo.pop();
    }
  }
  return ans;
}

int main() {
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  queue<ll> a;
  queue<ll> b;
  ll x;
  rep(i, n) {
    cin >> x;
    a.push(x);
  }
  rep(i, m) {
    cin >> x;
    b.push(x);
  }
  while (k >= 0) {
    if (!(a.empty()) && a.front() < b.front()) {
      k -= a.front();
      a.pop();
      if (k >= 0)
        ans++;
    } else {
      if (a.empty() && b.empty())
        break;
      k -= b.front();
      b.pop();
      if (k >= 0)
        ans++;
    }
    if (a.empty() && b.empty())
      break;
  }
  cout << ans << endl;
}