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

#include <bits/stdc++.h>
#define ll long long
#define INF 1e9
#define pb push_back
#define mp make_pair
#define loop(a, n) for (long long i = a; i < n; i++)
#define vil vector<long long int>
#define vi vector<int>
#define sz(v) v.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
using namespace std;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
bool isperfect(ll n) {
  ll y = sqrt(n);
  if (n % y == 0 && y * y == n)
    return true;
  return false;
}
bool comp(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first)
    return a.second > b.second;
  return a.first < b.first;
}
ll powi(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a);
    b = b >> 1;
    a = (a * a);
  }
  return ans;
}
bool isprime(ll n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
bool prime[10000000];
void seive() {

  prime[1] = true;
  for (int i = 2; i * i < 10000000; i++) {

    if (prime[i])
      continue;
    for (int j = i * i; j < 10000000; j += i) {
      prime[j] = true;
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll m, k;
  cin >> m >> k;
  ll 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 i = 0, j = 0;
  ll cnt = 0, ans = 0;
  ll s = 0, e = n + m;
  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int j = 1; j < m; j++)
    b[j] += b[j - 1];
  for (int i = 0; i < n; i++) {
    if (a[i] <= k) {
      ll up = upper_bound(b, b + m, k - a[i]) - b;
      ans = max(ans, i + 1 + up);
    }
  }
  for (int i = 0; i < m; i++) {
    if (b[i] <= k) {
      ll up = upper_bound(a, a + n, k - b[i]) - a;
      ans = max(ans, i + 1 + up);
    }
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1e9
#define pb push_back
#define mp make_pair
#define loop(a, n) for (long long i = a; i < n; i++)
#define vil vector<long long int>
#define vi vector<int>
#define sz(v) v.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
using namespace std;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
bool isperfect(ll n) {
  ll y = sqrt(n);
  if (n % y == 0 && y * y == n)
    return true;
  return false;
}
bool comp(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first)
    return a.second > b.second;
  return a.first < b.first;
}
ll powi(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a);
    b = b >> 1;
    a = (a * a);
  }
  return ans;
}
bool isprime(ll n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
bool prime[10000000];
void seive() {

  prime[1] = true;
  for (int i = 2; i * i < 10000000; i++) {

    if (prime[i])
      continue;
    for (int j = i * i; j < 10000000; j += i) {
      prime[j] = true;
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll m, k;
  cin >> m >> k;
  vil a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int i = 0, j = 0;
  ll cnt = 0, ans = 0;
  while (cnt < k && (i < n || j < m)) {
    if (i < n && j < n) {
      // cnt+=min(a[i],b[j]);

      if (a[i] < b[j])
        cnt += a[i++];
      else if (a[i] > b[j])
        cnt += b[j++];
      else {
        if (i + 1 < n && j + 1 < m) {
          if (a[i + 1] + a[i] < b[i + 1] + b[i]) {
            cnt += a[i++];
          } else
            cnt += b[j++];
        }
      }
      if (cnt > k)
        break;
      ans++;
    } else {
      if (i < n)
        cnt += a[i++];
      if (j < m)
        cnt += b[j++];
      if (cnt > k)
        break;
      ans++;
    }
  }
  cout << ans;
}