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

// Author: Fuadul Hasan(fuadul202@gmail.com)
// BSMRSTU,Gopalganj
#include <bits/stdc++.h>
using namespace std;
int tc = 1;
#define happy ios::sync_with_stdio(false);
#define coding cin.tie(0);
#define F first
#define S second
#define mp make_pair
#define ll long long
#define Pi atan(1) * 4
#define pb push_back
#define vpr vector<pr>
#define pr pair<int, int>
#define vi std::vector<int>
#define vll std::vector<ll>
#define YES printf("YES\\n");
#define NO printf("NO\\n");
#define Yes printf("Yes\\n");
#define No printf("No\\n");
#define all(n) n.begin(), n.end()
#define point(x) setprecision(x)
#define Test printf("Case %d: ", tc++);
#define Unique(c) (c).resize(unique(all(c)) - (c).begin())
#define vout(v)                                                                \\
  for (auto z : v)                                                             \\
    cout << z << " ";                                                          \\
  cout << endl;

#define error(args...)                                                         \\
  {                                                                            \\
    vector<string> _v = split(#args, ',');                                     \\
    err(_v.begin(), args);                                                     \\
    cout << endl;                                                              \\
  }
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " ";
  err(++it, args...);
}

const int M = 1e9 + 7;
const ll Inf = (ll)2e18 + 5;
const int N = 2e5 + 5;
ll vis[N], res[N];

int solve() {
  // happy coding

  int n, m;
  ll rem;
  cin >> n >> m >> rem;

  std::vector<int> a(n), b(m);
  std::vector<ll> preb(m + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    preb[i + 1] = preb[i] + b[i];
  }

  int best = 0;
  ll sum = 0;
  for (int i = 0; i <= n && sum <= rem; i++) {
    ll target = rem - sum;
    int cnt = upper_bound(all(preb), target) - preb.begin();
    best = max(i + cnt - 1, best);
    sum += a[i];
  }

  cout << best << endl;

  return 0;
}
int main() {
  int test = 1;
  // scanf("%d", &test);
  while (test--)
    solve();
  return 0;
}

Error: 0

Fixed submission:

// Author: Fuadul Hasan(fuadul202@gmail.com)
// BSMRSTU,Gopalganj
#include <bits/stdc++.h>
using namespace std;
int tc = 1;
#define happy ios::sync_with_stdio(false);
#define coding cin.tie(0);
#define F first
#define S second
#define mp make_pair
#define ll long long
#define Pi atan(1) * 4
#define pb push_back
#define vpr vector<pr>
#define pr pair<int, int>
#define vi std::vector<int>
#define vll std::vector<ll>
#define YES printf("YES\\n");
#define NO printf("NO\\n");
#define Yes printf("Yes\\n");
#define No printf("No\\n");
#define all(n) n.begin(), n.end()
#define point(x) setprecision(x)
#define Test printf("Case %d: ", tc++);
#define Unique(c) (c).resize(unique(all(c)) - (c).begin())
#define vout(v)                                                                \\
  for (auto z : v)                                                             \\
    cout << z << " ";                                                          \\
  cout << endl;

#define error(args...)                                                         \\
  {                                                                            \\
    vector<string> _v = split(#args, ',');                                     \\
    err(_v.begin(), args);                                                     \\
    cout << endl;                                                              \\
  }
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " ";
  err(++it, args...);
}

const int M = 1e9 + 7;
const ll Inf = (ll)2e18 + 5;
const int N = 2e5 + 5;
ll vis[N], res[N];

int solve() {
  // happy coding

  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[n];
  ll sum = 0;
  ll idx = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (sum + a[i] <= k) {
      sum += a[i];
      idx = i;
    }
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  ll mx = 0;
  // error(mx);
  for (ll i = idx, j = 0; j < m && i >= 0; i--) {
    // error(i);
    while (sum + b[j] <= k) {
      // error(sum);
      sum += b[j];
      mx = max(mx, j + i + 2);
      j++;
    }

    if (sum >= a[i]) {
      mx = max(i + 1, mx);
      // error(sum , a[i]);
      sum -= a[i];
      if (i == 0) {
        while (sum + b[j] <= k) {
          // error(sum);
          sum += b[j];
          mx = max(mx, j + i + 1);
          j++;
        }
      }
    }
  }

  cout << mx << endl;

  return 0;
}
int main() {
  int test = 1;
  // scanf("%d", &test);
  while (test--)
    solve();
  return 0;
}