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

#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define rep(i, j, n) for (int i = (j); i <= ((int)n); ++i)
#define rev(i, n, j) for (int i = (n); i >= ((int)j); --i)
typedef long long int ll;
#define int long long int
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
#define endl "\\n"
#define mem(a, val) memset(a, val, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define tr(container, it)                                                      \\
  for (__typeof(container.begin()) it = container.begin();                     \\
       it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define pb push_back
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define readmat(a, n, m)                                                       \\
  int a[n + 5][m + 5] = {};                                                    \\
  rep(i, 1, n) { rep(j, 1, m) cin >> a[i][j]; }
#define printmat(a, n, m)                                                      \\
  rep(i, 1, n) {                                                               \\
    rep(j, 1, m) cout << a[i][j] << " ";                                       \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
#define printarr(a, n)                                                         \\
  rep(i, 1, n) cout << a[i] << " ";                                            \\
  cout << endl;
typedef std::map<int, int> mii;
typedef std::vector<int> vi;
typedef std::vector<vi> vvi;
typedef std::pair<int, int> ii;
using namespace std;
#define cerr cout
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}
const int N = 1e6 + 5, M = N;
vector<int> g[N];

void solve() {
  int n, m;
  cin >> n >> m;
  int a[n + 5];
  int b[m + 5];
  int k;
  cin >> k;
  int dp1[n + 5] = {0};
  int dp2[m + 5] = {0};
  int z = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    dp1[i] = dp1[i - 1] + a[i];
  }

  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
    dp2[i] = dp2[i - 1] + b[i];
  }

  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    int z = dp1[i];
    int l = k - z;
    int pos = upper_bound(dp2, dp2 + m + 1, l) - dp2;
    pos--;
    if (pos >= 0)
      ans = max(ans, i + pos);
  }

  cout << ans << endl;
}

signed main() {
  FAST;
#ifdef LOCAL
  freopen("C:\\\\Users\\\\hp\\\\Documents\\\\input.txt", "r", stdin);
  freopen("C:\\\\Users\\\\hp\\\\Documents\\\\output.txt", "w", stdout);
  std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
  start = std::chrono::high_resolution_clock::now();
#endif

  solve();

#ifdef LOCAL
  end = std::chrono::high_resolution_clock::now();
  ll elapsed_time =
      std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
          .count();
  cout << endl;
  cout << "\\nElapsed Time: " << elapsed_time << "ms\\n";
#endif

  return 0;
}

// vector string set map first second continue break return upper_bound
// lower_bound length void sort stack queue pop size erase empty insert #Hala
// BBCF


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define rep(i, j, n) for (int i = (j); i <= ((int)n); ++i)
#define rev(i, n, j) for (int i = (n); i >= ((int)j); --i)
typedef long long int ll;
#define int long long int
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
#define endl "\\n"
#define mem(a, val) memset(a, val, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define tr(container, it)                                                      \\
  for (__typeof(container.begin()) it = container.begin();                     \\
       it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define pb push_back
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define readmat(a, n, m)                                                       \\
  int a[n + 5][m + 5] = {};                                                    \\
  rep(i, 1, n) { rep(j, 1, m) cin >> a[i][j]; }
#define printmat(a, n, m)                                                      \\
  rep(i, 1, n) {                                                               \\
    rep(j, 1, m) cout << a[i][j] << " ";                                       \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
#define printarr(a, n)                                                         \\
  rep(i, 1, n) cout << a[i] << " ";                                            \\
  cout << endl;
typedef std::map<int, int> mii;
typedef std::vector<int> vi;
typedef std::vector<vi> vvi;
typedef std::pair<int, int> ii;
using namespace std;
#define cerr cout
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}
const int N = 1e6 + 5, M = N;
vector<int> g[N];

void solve() {
  int n, m;
  cin >> n >> m;
  int a[n + 5];
  int b[n + 5];
  int k;
  cin >> k;
  int z = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }

  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
  }

  int ans = 0;
  int i = 1;
  int j = 1;
  while (1) {
    if (ans >= (n + m))
      break;
    int u = INFL;
    int v = INFL;

    if (i <= n)
      u = a[i];
    if (j <= m)
      v = b[j];
    if (u < v) {
      if (z + u <= k) {
        z = z + u;
        ans++;
        i++;
      } else
        break;
    } else {
      if (z + v <= k) {
        z = z + v;
        ans++;
        j++;
      } else
        break;
    }
  }

  cout << ans << endl;
}

signed main() {
  FAST;
#ifdef LOCAL
  freopen("C:\\\\Users\\\\hp\\\\Documents\\\\input.txt", "r", stdin);
  freopen("C:\\\\Users\\\\hp\\\\Documents\\\\output.txt", "w", stdout);
  std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
  start = std::chrono::high_resolution_clock::now();
#endif

  solve();

#ifdef LOCAL
  end = std::chrono::high_resolution_clock::now();
  ll elapsed_time =
      std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
          .count();
  cout << endl;
  cout << "\\nElapsed Time: " << elapsed_time << "ms\\n";
#endif

  return 0;
}

// vector string set map first second continue break return upper_bound
// lower_bound length void sort stack queue pop size erase empty insert #Hala
// BBCF