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

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define qrep(que, ite) for (auto ite = begin(que); ite != end(que); ite++)
const int max_n = 1e5;
const ll mod = 1e9 + 7;
const ll INF = 1e17;
const int inf = 1e5;
typedef long double ld;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
ll mo = 998244353;
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
struct SegmentTree {
  int N;
  vector<int> node;

public:
  void intit(vector<int> v) {
    int sz = v.size();
    N = 1;
    while (N < sz)
      N *= 2;
    node.resize(N);
    for (int i = 0; i < sz; i++)
      node[i + N - 1] = v[i];
    for (int i = N - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void update(int x, int val) {
    x += N - 1;
    node[x + N - 1] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = min(node[x * 2 + 1], node[x * 2 + 2]);
    }
  }
  int getmin(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return inf;
    else if (a <= l && r <= b)
      return node[k];
    else {
      int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  ll K;
  cin >> K;
  vector<ll> a(N), b(M);
  rep(i, 0, N) cin >> a[i];
  rep(i, 0, M) cin >> b[i];
  int cnt1 = 0, cnt2 = 0;
  ll K2 = K;
  bool ok1 = true, ok2 = true;
  vector<ll> v1(N + 1, 0), v2(M + 1, 0);
  rep(i, 0, N) v1[i + 1] = a[i] + v1[i];
  rep(i, 0, M) v2[i + 1] = b[i] + v2[i];
  int i1 = 0, i2 = 0;
  ll cnt = 0;
  /*while((i1!=N)||(i2!=M)){
    if(i1==N){
      while(i2!=M){
        if(K>=b[i2]){
          K -= b[i2++];
          cnt++;
        }
      }
      break;
    }
    if(i2==M){
      while(i1!=N){
        if(K>=b[i1]){
          K -= b[i1++];
          cnt++;
        }
      }
      break;
    }
    if(a[i1]<b[i2]){
      if(K>=a[i1]){
        K -= a[i1++];
        cnt++;
      }else{
        break;
      }
    }else{
      if(K>=b[i2]){
        K -= b[i2++];
        cnt++;
      }else{
        break;
      }
    }
  }*/
  ll ma = -5;
  rep(i, 0, N + 1) {
    if (K < v1[i])
      break;
    auto st = upper_bound(all(v2), K - v1[i]) - v2.begin();

    chmax(cnt, i + st - 1);
  }
  /*rep(i,0,M){
    if(K<v2[i]) break;
    ll KK = K - v2[i];
    auto st = lower_bound(all(v1), KK)-v1.begin();
    if(ma<=v2[i]+v1[st]){
      ma = v2[i]+v1[st];
      cnt = i+1 + st+1;
    }
  }*/
  cout << cnt << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define qrep(que, ite) for (auto ite = begin(que); ite != end(que); ite++)
const int max_n = 1e5;
const ll mod = 1e9 + 7;
const ll INF = 1e17;
const int inf = 1e5;
typedef long double ld;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
ll mo = 998244353;
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
struct SegmentTree {
  int N;
  vector<int> node;

public:
  void intit(vector<int> v) {
    int sz = v.size();
    N = 1;
    while (N < sz)
      N *= 2;
    node.resize(N);
    for (int i = 0; i < sz; i++)
      node[i + N - 1] = v[i];
    for (int i = N - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void update(int x, int val) {
    x += N - 1;
    node[x + N - 1] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = min(node[x * 2 + 1], node[x * 2 + 2]);
    }
  }
  int getmin(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return inf;
    else if (a <= l && r <= b)
      return node[k];
    else {
      int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  ll K;
  cin >> K;
  vector<ll> a(N), b(M);
  rep(i, 0, N) cin >> a[i];
  rep(i, 0, M) cin >> b[i];
  int cnt1 = 0, cnt2 = 0;
  ll K2 = K;
  bool ok1 = true, ok2 = true;
  vector<ll> v1(N + 1, 0), v2(M + 1, 0);
  rep(i, 0, N) v1[i + 1] += v1[i];
  rep(i, 0, M) v2[i + 1] += v2[i];
  int i1 = 0, i2 = 0;
  int cnt = 0;
  while ((i1 != N) || (i2 != M)) {
    if (i1 == N) {
      while (i2 != M) {
        if (K >= b[i2]) {
          K -= b[i2++];
          cnt++;
        }
      }
      break;
    }
    if (i2 == M) {
      while (i1 != N) {
        if (K >= b[i1]) {
          K -= b[i1++];
          cnt++;
        }
      }
      break;
    }
    if (a[i1] < b[i2]) {
      if (K >= a[i1]) {
        K -= a[i1++];
        cnt++;
      } else {
        break;
      }
    } else {
      if (K >= b[i2]) {
        K -= b[i2++];
        cnt++;
      } else {
        break;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}