alexjercan/bugnet huggingface dataset:
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using ll = int64_t;
constexpr ll mod = ll(1e9 + 7);

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> A(n);
  for (ll i = 0; i < n; ++i) {
    cin >> A[i];
  }
  vector<ll> B(m);
  for (ll i = 0; i < m; ++i) {
    cin >> B[i];
  }
  ll t = 0, na = 0, nb = 0;
  while (na < n && t + A[na] <= k) {
    t += A[na++];
  }
  while (nb < m && t + B[nb] <= k) {
    t += B[nb++];
  }

  ll ans = na + nb;
  while (na) {
    t -= A[--na];
    while (nb < m && t + B[nb] <= k) {
      t += B[nb++];
    }
    ans = max(ans, na + nb);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = int64_t;
constexpr ll mod = ll(1e9 + 7);

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> A(n);
  for (ll i = 0; i < n; ++i) {
    cin >> A[i];
  }
  vector<ll> B(m);
  for (ll i = 0; i < m; ++i) {
    cin >> B[i];
  }
  using tup = tuple<ll, int, int>;
  priority_queue<tup, vector<tup>, greater<tup>> pq;
  pq.emplace(0, 0, 0);
  ll ans = 0;
  while (pq.size()) {
    auto [t, ia, ib] = pq.top();
    pq.pop();
    if (t > k) {
      break;
    }
    ans = max(ans, ll(ia + ib));
    if (ia < n)
      pq.emplace(t + A[ia], ia + 1, ib);
    if (ib < m)
      pq.emplace(t + B[ib], ia, ib + 1);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main(void) {
  int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<long long int> A(n), B(m);
  std::vector<long long int> At, Bt;

  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    std::cin >> B[i];
  }

  At.push_back(0);
  int i = 1;
  while (true) {
    // std::cout << "i:" << i << std::endl;
    long long int tmp = At[i - 1] + A[i - 1];
    if (tmp > k || i > n) {
      break;
    } else {
      At.push_back(tmp);
      // std::cout << At[i] << " ";
    }
    i++;
  }
  // std::cout << std::endl;

  Bt.push_back(0);
  i = 1;
  while (Bt[i] <= k && i <= m) {
    long long int tmp = Bt[i - 1] + B[i - 1];
    if (tmp > k || i > m) {
      break;
    } else {
      Bt.push_back(tmp);
    }
    i++;
  }

  long long int sum = 0;
  int bcnt = Bt.size() - 1;
  int ans = 0;
  for (int i = 0; i < At.size(); i++) {
    sum = At[i];
    int tmp = bcnt;
    for (int j = tmp; j >= 0; j--) {
      if (Bt[j] + sum <= k) {
        bcnt = j;
        break;
      }
    }
    ans = std::max(i + bcnt, ans);
    // std::cout << "i:" << i << "bcnt: " << bcnt << "ans: " << ans <<
    // std::endl;
  }

  std::cout << ans << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main(void) {
  int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<int> A(n), B(m);
  std::vector<int> At, Bt;

  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    std::cin >> B[i];
  }

  At.push_back(0);
  int i = 1;
  while (true) {
    // std::cout << "i:" << i << std::endl;
    int tmp = At[i - 1] + A[i - 1];
    if (tmp > k || i > n) {
      break;
    } else {
      At.push_back(tmp);
      // std::cout << At[i] << " ";
    }
    i++;
  }
  // std::cout << std::endl;

  Bt.push_back(0);
  i = 1;
  while (Bt[i] <= k && i <= m) {
    int tmp = Bt[i - 1] + B[i - 1];
    if (tmp > k || i > m) {
      break;
    } else {
      Bt.push_back(tmp);
    }
    i++;
  }

  int sum = 0;
  int bcnt = 0;
  int ans = 0;
  for (int i = 0; i < At.size(); i++) {
    sum = At[i];
    for (int j = 0;; j++) {
      if (j == Bt.size())
        break;
      if (Bt[j] + sum <= k) {
        bcnt = j;
      } else {
        break;
      }
    }
    ans = std::max(i + bcnt, ans);
    // std::cout << "i:" << i << "bcnt: " << bcnt << "ans: " << ans <<
    // std::endl;
  }

  std::cout << ans << std::endl;

  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ll long long int
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n];
  ll B[m];
  ll preA[n + 1];
  ll preB[m + 1];
  preA[0] = 0;
  preB[0] = 0;
  for (ll i = 0; i < n; ++i) {
    cin >> A[i];
    preA[i + 1] = preA[i] + A[i];
  }
  for (ll i = 0; i < m; ++i) {
    cin >> B[i];
    preB[i + 1] = preB[i] + B[i];
  }
  ll ans = 0;
  ll index = m;
  for (ll i = 0; i <= n; ++i) {
    ll Left = k - preA[i];
    if (Left < 0) {
      break;
    }
    while (preB[index] > Left) {
      index--;
    }
    ans = max(ans, index + i);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  stack<ll> a1;
  stack<ll> b1;
  ll a[n];
  ll b[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < m; i++)
    cin >> b[i];
  for (ll i = n - 1; i >= 0; i--)
    a1.push(a[i]);
  for (ll i = m - 1; i >= 0; i--)
    b1.push(b[i]);
  if (m > n)
    swap(a1, b1);
  /*	cout<<a1.size()<<endl;
          cout<<b1.size()<<endl;
          cout<<a1.top()<<endl;
          cout<<b1.top()<<endl;*/
  ll count = 0;
  while ((!a1.empty() && !b1.empty()) && (k != 0)) {
    if (a1.top() <= b1.top() && a1.top() <= k) {
      count++;
      k = k - a1.top();
      a1.pop();
      // cout<<"salo"<<count<<endl;
    } else if (a1.top() > b1.top() && b1.top() <= k) {
      count++;
      k = k - b1.top();
      b1.pop();
      // cout<<"saloni"<<count<<endl;
    } else {
      // cout<<"pouytr"<<count<<endl;
      break;
    }
  }
  //	cout<<"k"<<k<<endl;
  while (!a1.empty() && k != 0) {
    if (a1.top() <= k) {
      count++;
      k = k - a1.top();
      a1.pop();
    } else
      break;
  }
  while (!b1.empty() && k != 0) {
    if (b1.top() <= k) {
      count++;
      k = k - b1.top();
      b1.pop();
    } else
      break;
  }
  cout << count << endl;

  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define mem(x, n) memset(x, n, sizeof(x))
#define all(x) x.begin(), x.end()
#define endl "\\n"

int main() {
  faster;

  ll n, m, t, in, tot = 0, time = 0;
  cin >> n >> m >> t;

  vector<ll> a(n + 1), b(m + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int i, j;
  for (i = 0; i < n; i++) {
    if (a[i] + time <= t) {
      time += a[i];
      tot++;
    } else {
      break;
    }
  }
  i--;

  ll temp = tot;

  for (j = 0; j < m; j++) {
    time += b[j];
    temp++;
    while (i >= 0 && time > t) {
      time -= a[i];
      i--;
      temp--;
    }

    if (time <= t) {
      tot = max(tot, temp);
    } else {
      break;
    }
  }

  cout << tot << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

int main() {
  int n, m, k, time = 0, count = 0;
  cin >> n >> m >> k;

  queue<int> a, b;

  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    a.push(num);
  }

  for (int i = 0; i < m; i++) {
    int num;
    cin >> num;
    b.push(num);
  }

  while (!a.empty() || !b.empty()) {
    if (a.empty() || b.front() <= a.front()) {
      if (b.front() + time <= k) {
        time += b.front();
        b.pop();
        count++;
      } else {
        break;
      }
    }
    if (b.empty() || b.front() > a.front()) {
      if (a.front() + time <= k) {
        time += a.front();
        a.pop();
        count++;
      } else {
        break;
      }
    }
  }

  cout << count << endl;

  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 1023456789
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define trav(it, v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long long ll;

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int a[n], b[m];
  rep(i, 0, n) { scanf("%d", &a[i]); }
  rep(i, 0, m) { scanf("%d", &b[i]); }
  int max_c = 0, c, t = k, s = 0;
  ;
  int tmp = k, j = 0, bc = 0, used = 0;

  while (j < m && tmp >= b[j]) {
    tmp -= b[j++];
    used += b[j - 1];
    bc++;
  }
  max_c = bc;
  rep(i, 0, n) {
    t -= a[i];
    s++;
    c = s;
    if (t < 0)
      break;
    while (bc > 0 && used > t) {
      used -= b[--bc];
    }
    if (used > t)
      break;
    max_c = max(max_c, s + bc);
  }
  cout << max_c;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1023456789
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define trav(it, v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long long ll;

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int a[n], b[m];
  rep(i, 0, n) { scanf("%d", &a[i]); }
  rep(i, 0, m) { scanf("%d", &b[i]); }
  int max_c = 0, c, t = k, s = 0;
  ;
  rep(i, 0, n + 1) {
    if (i != 0) {
      t -= a[i - 1];
      s++;
    }
    c = s;
    if (t < 0)
      break;
    int tmp = t;
    int j = 0;
    while (j < m && tmp >= b[j]) {
      tmp -= b[j++];
      c++;
    }
    max_c = max(c, max_c);
  }
  cout << max_c;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using namespace std;

int main() {
  int n, m, k, now = 0;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0), b(m + 1, 0);
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
  }
  int mx = 0;
  for (int i = n; i >= 0; i--) {
    while (now <= m && sum + b[now] <= k) {
      sum += b[now];
      now++;
    }
    if (sum <= k)
      mx = max(mx, i + now - 1);
    sum -= a[i];
  }
  cout << mx;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using namespace std;

int main() {
  int n, m, k, now = 0;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), asum(n), bsum(n);
  rep(i, n) {
    cin >> a[i];
    if (i == 0)
      asum[i] = a[i];
    else
      asum[i] = asum[i - 1] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    if (i == 0)
      bsum[i] = b[i];
    else
      bsum[i] = bsum[i - 1] + b[i];
  }
  ll mx = 0;
  for (ll i = n - 1; i >= 0; i--) {
    if (asum[i] > k)
      continue;
    for (ll j = now; j < m; j++) {
      if (asum[i] + bsum[j] > k)
        break;
      cout << asum[i] + bsum[j] << " " << i << " " << j << endl;
      now = j;
    }
    if (mx < i + 1 + now + 1)
      mx = i + 1 + now + 1;
  }
  cout << mx;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  long long K;
  cin >> K;
  long long a;
  vector<long long> A(N + 1);
  vector<long long> B(M + 1);
  A[0] = 0;
  B[0] = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i < M + 1; i++) {
    cin >> a;
    B[i] = B[i - 1] + a;
  }
  int ans = 0;
  int J = M;
  for (int i = 0; i < N + 1; i++) {
    if (A[i] > K)
      break;
    auto it = upper_bound(B.begin(), B.end(), K - A[i]);
    int j = it - B.begin();
    ans = max(ans, i + j - 1);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  long long K;
  cin >> K;
  long long a;
  vector<long long> A(N + 1);
  vector<long long> B(M + 1);
  A[0] = 0;
  B[0] = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i < M + 1; i++) {
    cin >> a;
    B[i] = B[i - 1] + a;
  }
  int ans = 0;
  int J = M;
  for (int i = 0; i < N + 1; i++) {
    if (A[i] > K)
      break;
    for (int j = 0; j < J + 1; j++) {
      if ((A[i] + B[j]) > K) {
        J = j;
        break;
      }
      ans = max(ans, i + j);
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> a(n + 1, 0), b(m + 1, 0), sa(n + 1, 0), sb(m + 1, 0);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sa[i] = sa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
    sb[i] = sb[i - 1] + b[i];
  }

  int ans = 0;
  for (int i = 0; sa[i] <= k && i <= n; ++i) {
    auto itr = upper_bound(sb.begin(), sb.end(), k - sa[i]);
    --itr;
    int numb = distance(sb.begin(), itr);
    ans = max(ans, i + numb);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> a(n + 1), b(m + 1), sa(n + 1, 0), sb(m + 1, 0);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sa[i] = sa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
    sb[i] = sb[i - 1] + b[i];
  }

  int ans = 0;
  for (int i = 0; sa[i] <= k && i <= m; ++i) {
    auto itr = upper_bound(sb.begin(), sb.end(), k - sa[i]);
    --itr;
    int numb = distance(sb.begin(), itr);
    ans = max(ans, i + numb);
  }

  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

int main() {
  int N, M;
  unsigned long long K;
  std::vector<unsigned long long> A;
  std::vector<unsigned long long> B;
  std::cin >> N >> M >> K;
  A.resize(N + 1);
  B.resize(M + 1);
  for (int i = 1; i < N + 1; ++i) {
    unsigned long long a;
    std::cin >> a;
    A.at(i) = a + A.at(i - 1);
  }

  for (int i = 1; i < M + 1; ++i) {
    unsigned long long b;
    std::cin >> b;
    B.at(i) = b + B.at(i - 1);
  }

  int max = 0;
  int startM = 0;
  for (int i = 0; i < N + 1; ++i) {
    for (int j = startM; j < M + 1; ++j) {
      unsigned long long cost = A.at(i) + B.at(j);
      if (cost > K) {
        break;
      }
      int newMax = i + j;
      if (max < newMax)
        max = newMax;
    }
    startM = max - i;
    if (startM < 0)
      break;
  }

  std::cout << max << std::endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int N, M;
  unsigned long long K;
  std::vector<unsigned long long> A;
  std::vector<unsigned long long> B;
  std::cin >> N >> M >> K;
  A.resize(N + 1);
  B.resize(M + 1);
  for (int i = 1; i < N + 1; ++i) {
    unsigned long long a;
    std::cin >> a;
    A.at(i) = a + A.at(i - 1);
  }

  for (int i = 1; i < M + 1; ++i) {
    unsigned long long b;
    std::cin >> b;
    B.at(i) = b + B.at(i - 1);
  }

  int max = 0;
  int startM = 0;
  for (int i = 0; i < N + 1; ++i) {
    for (int j = startM; j < M + 1; ++j) {
      unsigned long long cost = A.at(i) + B.at(j);
      if (cost > K) {
        startM = j - 1;
        break;
      }
      int newMax = i + j;
      if (max < newMax)
        max = newMax;
    }
  }

  std::cout << max << std::endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, M;
  long long K;
  cin >> N >> M >> K;
  vector<long long> A(N), B(M);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  for (int i = 0; i < M; ++i)
    cin >> B[i];

  vector<long long> a, b;
  a.push_back(0);
  b.push_back(0);
  for (int i = 0; i < N; ++i)
    a.push_back(a[i] + A[i]);
  for (int i = 0; i < M; ++i)
    b.push_back(b[i] + B[i]);

  int maxNum = 0, num = M;
  for (int i = 0; i <= N; ++i) {
    if (a[i] > K)
      break;

    while (b[num] > K - a[i]) {
      --num;
    }
    maxNum = max(maxNum, i + num);
  }

  cout << maxNum << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, M;
  long long K;
  cin >> N >> M >> K;
  vector<long long> A(N), B(M);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  for (int i = 0; i < M; ++i)
    cin >> B[i];

  vector<long long> a, b;
  a.push_back(0);
  b.push_back(0);
  for (int i = 0; i < N; ++i)
    a.push_back(a[i] + A[i]);
  for (int i = 0; i < M; ++i)
    b.push_back(b[i] + B[i]);

  int maxNum = 0;
  for (int i = 0; i < N; ++i) {
    if (a[i] > K)
      break;

    int num = M;
    while (b[num] >= K - a[i]) {
      --num;
    }
    maxNum = max(maxNum, i + num);
  }

  cout << maxNum << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

// #define LOCAL = 0

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << "\\t|";
  __f(comma + 1, args...);
}

#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, k;
  cin >> n >> m >> k;
  vector<ll> a = {0}, b = {0};
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.push_back(x);
  }
  for (int i = 0; i < m; i++) {
    ll x;
    cin >> x;
    b.push_back(x);
  }
  for (int i = 1; i < n + 1; i++)
    a[i] = a[i] + a[i - 1];
  for (int i = 1; i < m + 1; i++)
    b[i] = b[i] + b[i - 1];

  long long ans = 0;
  for (int i = 0; i < n + 1; i++) {
    ll search = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin();
    if (search > 0)
      search--;
    trace(i, search, a[i], b[search]);
    if (a[i] + b[search] <= k) {
      ans = max(ans, i + search);
    }
    if (a[i] > k)
      break;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    a.push_back(num);
  }
  for (int i = 0; i < m; i++) {
    int num;
    cin >> num;
    b.push_back(num);
  }
  int i = 0, j = 0;
  int ans = 0, pages = 0;
  while (i < m || j < n) {
    int page = 0;
    if (i < m && j < n) {
      if (a[i] < b[j]) {
        page = a[i];
        i++;
      } else {
        page = b[j];
        j++;
      }
    } else if (i < m) {
      page = a[i];
      i++;
    } else {
      page = b[j];
      j++;
    }
    if (pages + page <= k) {
      pages += page;
      ans++;
    } else {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N), B(M);
  vector<long long> d_A(N + 1), d_B(M + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    d_A[i + 1] += d_A[i] + A[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i];
    d_B[i + 1] += d_B[i] + B[i];
  }

  long long max_count = 0;
  long long book_count = 0;
  long long time_count = 0;
  long long a_book_count = 0;
  long long a_time_count = 0;

  long long i = 0, j = M;
  for (; i <= N; i++) {
    for (; j >= 0; j--) {
      if (d_A[i] + d_B[j] > K) {
        continue;
      } else {
        max_count = max(max_count, i + j);
        break;
      }
    }
  }
  cout << max_count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> 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 max_count = 0;
  int book_count = 0;
  int time_count = 0;
  int a_book_count = 0;
  int a_time_count = 0;
  for (int i = -1; i < N; i++) {
    if (i >= 0) {
      a_book_count++;
      a_time_count += A[i];
    }
    int b_book_count = 0;
    int b_time_count = 0;
    for (int j = -1; j < M; j++) {
      if (j >= 0) {
        b_book_count++;
        b_time_count += B[j];
      }
      time_count = a_time_count + b_time_count;
      book_count = a_book_count + b_book_count;
      if (time_count <= K) {
        // cerr << time_count << ' ' << book_count << endl;
        max_count = max(max_count, book_count);
      } else {
        break;
      }
    }
    if (a_time_count > K) {
      break;
    }
  }

  cout << max_count << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int N, M;
  ll K;
  cin >> N >> M >> K;
  // cout << N << M << K << endl;

  ll A[N + 1];
  A[0] = 0;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }

  ll B[M + 1];
  B[0] = 0;

  for (int i = 1; i <= M; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }

  // for (const auto& value: A) {
  //     cout << value << ' ';
  // }

  int cur_max = 0;

  int books_stack_b = M;

  ll diff = -1;
  for (int books_stack_a = 0; books_stack_a <= N; books_stack_a++) {
    if (A[books_stack_a] > K) {
      break;
    }
    diff = K - A[books_stack_a];
    while (diff < B[books_stack_b]) { // guaranteed to terminate at zero
      books_stack_b--;
    }
    // cout << books_stack_b << ' ' << B[books_stack_b] << '\\n';
    cur_max = max(cur_max, books_stack_a + books_stack_b);
  }

  cout << cur_max;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll N, M, K;
  cin >> N >> M >> K;
  // cout << N << M << K << endl;

  ll A[N + 1];
  A[0] = 0;

  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }

  ll B[M + 1];
  B[0] = 0;

  for (ll i = 1; i <= M; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }

  // for (const auto& value: A) {
  //     cout << value << ' ';
  // }

  ll cur_max = 0;

  ll books_stack_b = M;

  ll diff = -1;
  for (ll books_stack_a = 0; books_stack_a <= N; books_stack_a++) {
    diff = K - A[books_stack_a];
    while (diff < B[books_stack_b]) { // guaranteed to terminate at zero
      books_stack_b--;
    }
    // cout << books_stack_b << ' ' << B[books_stack_b] << '\\n';
    cur_max = max(cur_max, books_stack_a + books_stack_b);
  }

  cout << cur_max;
}