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

#include <bits/stdc++.h>

//      DEEP

using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define fwd(i, a, n) for (int i = a; i <= n; i++)
#define bak(i, a, n) for (int i = a; i >= n; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define lop '\\n'
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
typedef set<int> si;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
// power
ull power(ull x, ull y) {
  if (y == 0)
    return 1;
  else {
    if (y % 2 == 0)
      return power(x * x, y / 2);
    else
      return x * power(x * x, (y - 1) / 2);
  }
}

//                    Modular Exponentiation

ll mod_power(ll x, ll y, ll m) {
  ll r = 1; // Initialize result

  x = x % m; // Update x if it is more than or equal to m

  if (x == 0)
    return 0; // In case x is divisible by m;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      r = (r * x) % m;

    // y must be even now
    y = y / 2;
    x = (x * x) % m;
  }
  return r;
}

// GCD
ull gcd(ull x, ull y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
//                                         EXTENDED_EUCLID
vector<ll> extended_Euclid(ll a, ll b) {
  vector<ll> v(3);
  if (b == 0) {
    v[0] = a;
    v[1] = 1;
    v[2] = 0;
    return v;
  } else {
    vector<ll> v1 = extended_Euclid(b, a % b); // recursive call
    v[0] = v1[0];
    v[1] = v1[2];
    v[2] = v1[1] - (a / b) * v1[2];
    return v;
  }
}
// DFS ( number of connected components  )
// vector<vector<ll> >adj(200001);
// bool visited[200001];
/*ll dfs(int x,int c){                         // x - > node      c - > number
of edges in the component if(visited[x])return c; else{ visited[x]=true; int
y=c; for(int i=0;i<adj[x].size();i++){ if(!visited[adj[x][i]]){ c+=1; int
d=dfs(adj[x][i],y); c+=d;
            }
        }
        return c;
    }
}
*/
// SIEVE

/*
bool isPrime[100001];
void sieve(int N) {

        for(int i = 0; i <= N;++i) {
            isPrime[i]=true;
        }
        isPrime[0]=false;
        isPrime[1]=false;

        for(int i = 2; i * i <= N; ++i) {
             if(isPrime[i] == true) {
                 for(int j = i * i; j <= N ;j += i)
                     isPrime[j] = false;
            }
        }
    }
*/
// Binary Search
/*
ll bin_search(ll y){
  while(lo<hi){
    x=lo+(hi-lo)/2;
    if(condition){
            .....
    }
    else{
                  // increase lo or decrease hi suitably
            }
   }
}
*/

int main() {
  boost;
  int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    int n, m, k;
    cin >> n >> m >> k;
    vector<ull> a(n);
    vector<ull> b(m);
    for (int i = 0; i < n; i++) {
      ull x;
      cin >> x;
      if (i == 0)
        a[0] = x;
      else
        a[i] = x + a[i - 1];
    }
    for (int i = 0; i < m; i++) {
      ull x;
      cin >> x;
      if (i == 0)
        b[0] = x;
      else
        b[i] = x + b[i - 1];
    }
    auto ip = upper_bound(all(a), k);
    ll num = distance(a.begin(), ip);
    num -= 1;
    if (num >= 0) {
      ll ans = num + 1;
      ll i = 0;
      for (i = 0; i < m; i++) {
        if (num >= 0) {
          if (a[num] + b[i] <= k)
            ans = max(ans, num + 1 + i + 1);
          else {
            while (a[num] + b[i] > k) {
              if (num > 0)
                num--;
              else {
                num = -1;
                break;
              }
              if (num < 0)
                break;
            }
            if (num >= 0)
              ans = max(ans, num + 1 + i + 1);
          }
        } else if (b[i] <= k)
          ans = max(ans, i + 1);
        else
          break;
      }
      cout << ans << endl;
    } else {
      auto it = upper_bound(all(b), k);
      int nu = distance(b.begin(), it);
      nu -= 1;
      if (nu >= 0)
        cout << nu + 1 << endl;
      else
        cout << 0 << endl;
    }
  }
}
/* Be careful about integer ranges;
   Check the corner cases before submitting.
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>

//      DEEP

using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define fwd(i, a, n) for (int i = a; i <= n; i++)
#define bak(i, a, n) for (int i = a; i >= n; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define lop '\\n'
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
typedef set<int> si;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
// power
ull power(ull x, ull y) {
  if (y == 0)
    return 1;
  else {
    if (y % 2 == 0)
      return power(x * x, y / 2);
    else
      return x * power(x * x, (y - 1) / 2);
  }
}

//                    Modular Exponentiation

ll mod_power(ll x, ll y, ll m) {
  ll r = 1; // Initialize result

  x = x % m; // Update x if it is more than or equal to m

  if (x == 0)
    return 0; // In case x is divisible by m;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      r = (r * x) % m;

    // y must be even now
    y = y / 2;
    x = (x * x) % m;
  }
  return r;
}

// GCD
ull gcd(ull x, ull y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
//                                         EXTENDED_EUCLID
vector<ll> extended_Euclid(ll a, ll b) {
  vector<ll> v(3);
  if (b == 0) {
    v[0] = a;
    v[1] = 1;
    v[2] = 0;
    return v;
  } else {
    vector<ll> v1 = extended_Euclid(b, a % b); // recursive call
    v[0] = v1[0];
    v[1] = v1[2];
    v[2] = v1[1] - (a / b) * v1[2];
    return v;
  }
}
// DFS ( number of connected components  )
// vector<vector<ll> >adj(200001);
// bool visited[200001];
/*ll dfs(int x,int c){                         // x - > node      c - > number
of edges in the component if(visited[x])return c; else{ visited[x]=true; int
y=c; for(int i=0;i<adj[x].size();i++){ if(!visited[adj[x][i]]){ c+=1; int
d=dfs(adj[x][i],y); c+=d;
            }
        }
        return c;
    }
}
*/
// SIEVE

/*
bool isPrime[100001];
void sieve(int N) {

        for(int i = 0; i <= N;++i) {
            isPrime[i]=true;
        }
        isPrime[0]=false;
        isPrime[1]=false;

        for(int i = 2; i * i <= N; ++i) {
             if(isPrime[i] == true) {
                 for(int j = i * i; j <= N ;j += i)
                     isPrime[j] = false;
            }
        }
    }
*/
// Binary Search
/*
ll bin_search(ll y){
  while(lo<hi){
    x=lo+(hi-lo)/2;
    if(condition){
            .....
    }
    else{
                  // increase lo or decrease hi suitably
            }
   }
}
*/

int main() {
  boost;
  int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    int n, m, k;
    cin >> n >> m >> k;
    vector<ll> a(n);
    vector<ll> b(m);
    for (int i = 0; i < n; i++) {
      ll x;
      cin >> x;
      if (i == 0)
        a[0] = x;
      else
        a[i] = x + a[i - 1];
    }
    for (int i = 0; i < m; i++) {
      ll x;
      cin >> x;
      if (i == 0)
        b[0] = x;
      else
        b[i] = x + b[i - 1];
    }
    auto ip = upper_bound(all(a), k);
    ll num = distance(a.begin(), ip);
    num -= 1;
    if (num >= 0) {
      ll ans = num + 1;
      ll i = 0;
      for (i = 0; i < m; i++) {
        if (num >= 0) {
          if (a[num] + b[i] <= k)
            ans = max(ans, num + 1 + i + 1);
          else {
            while (a[num] + b[i] > k) {
              if (num > 0)
                num--;
              else {
                num = -1;
                break;
              }
            }
            if (num >= 0)
              ans = max(ans, num + 1 + i + 1);
          }
        } else if (a[i] <= k)
          ans = max(ans, i + 1);
        else
          break;
      }
      cout << ans << endl;
    } else {
      auto it = upper_bound(all(b), k);
      int nu = distance(b.begin(), it);
      nu -= 1;
      if (nu >= 0)
        cout << nu + 1 << endl;
      else
        cout << 0 << endl;
    }
  }
}
/* Be careful about integer ranges;
   Check the corner cases before submitting.
*/