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

#include <bits/stdc++.h>
#define ld long double
#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define ms(v, x) memset(v, x, sizeof(v))
#define all(v) v.begin(), v.end()
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define td(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
#define right sefudermano
typedef long long ll;
typedef pair<int, int> pii;
inline ll mod(ll n, ll m) {
  ll ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}
ll gcd(ll a, ll b) { return (b == 0LL ? a : gcd(b, a % b)); }
ll exp(ll a, ll b, ll m) {
  if (b == 0LL)
    return 1LL;
  if (b == 1LL)
    return mod(a, m);
  ll k = mod(exp(a, b / 2, m), m);
  if (b & 1LL) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}

const int N = 200100;
int a[N], b[N];
const int inf = 1e9 + 1e8;
int pre[N];
int n;
int get(int x) {
  int l = 1, r = n;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (pre[mid] <= x) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  return ans;
}

int32_t main() {
  fastio;
  int m, k;
  cin >> n >> m >> k;
  int id1 = 0, id2 = 0;
  for (int i = 0; i < N; i++) {
    a[i] = b[i] = inf;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    pre[i] = pre[i - 1] + a[i];
  }

  int r = 0;
  for (int i = 1; i <= m; i++) {
    if (k - r < 0)
      break;
    res = max(res, +i - 1 + get(k - r));
    r += b[i];
    if (k - r < 0)
      break;
    res = max(res, i + get(k - r));
  }
  cout << res << endl;

  // Math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ld long double
#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define ms(v, x) memset(v, x, sizeof(v))
#define all(v) v.begin(), v.end()
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define td(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
#define right sefudermano
typedef long long ll;
typedef pair<int, int> pii;
inline ll mod(ll n, ll m) {
  ll ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}
ll gcd(ll a, ll b) { return (b == 0LL ? a : gcd(b, a % b)); }
ll exp(ll a, ll b, ll m) {
  if (b == 0LL)
    return 1LL;
  if (b == 1LL)
    return mod(a, m);
  ll k = mod(exp(a, b / 2, m), m);
  if (b & 1LL) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}

const int inf = 1e9 + 100;

int32_t main() {
  fastio;
  int n, m, k;
  cin >> n >> m >> k;
  int id1 = 0, id2 = 0;
  vi a(n), b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int res = 0;
  while (true) {
    int op1 = (id1 == n ? inf : a[id1]);
    int op2 = (id2 == m ? inf : a[id2]);
    if (min(op1, op2) > k)
      break;
    if (op1 <= op2) {
      id1++;
      k -= op1;
    } else {
      id2++;
      k -= op2;
    }
    res++;
  }
  cout << res << endl;
  // Math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}