alexjercan/bugnet huggingface dataset:
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1000000007;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  ll total = 0;
  ll cnt = m;
  ll now = m - 1;
  rep(i, n) cin >> a[i];
  rep(i, m) {
    cin >> b[i];
    total += b[i];
  }
  ll ma = 0;
  for (int i = -1; i < n; i++) {
    bool ok = true;
    if (i != -1) {
      total += a[i];
      cnt++;
    }
    while (total > k) {
      if (now < 0) {
        ok = false;
        break;
      }
      total -= b[now];
      now--;
      cnt--;
      if (now < 0) {
        if (total > k)
          ok = false;
        break;
      }
    }
    if (ok) {
      ma = max(cnt, ma);
    } else
      break;
  }
  cout << ma << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1000000007;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  ll total = 0;
  ll cnt = 0;
  rep(i, n) cin >> a[i];
  rep(i, m) {
    cin >> b[i];
    total += b[i];
    cnt++;
  }
  ll ma = 0;
  ll now = m - 1;
  for (int i = -1; i < n; i++) {
    bool ok = true;
    if (i != -1) {
      total += a[i];
      cnt++;
    }
    while (total > k) {
      total -= b[now];
      now--;
      cnt--;
      if (cnt <= -1) {
        ok = false;
        break;
      }
    }
    if (ok)
      ma = max(cnt, ma);
    else
      break;
  }
  cout << ma << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using PP = pair<int, pair<int, int>>;
using T = tuple<string, int, int>;
const string EMP = " ";
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

int main() {
  int n, m;
  ull k;
  cin >> n >> m >> k;

  vector<ull> a(n + 1, 0), b(m + 1, 0);
  for (int i = 0; i < n; i++) {
    ull aa;
    cin >> aa;
    a[i + 1] = aa + a[i];
  }
  for (int i = 0; i < m; i++) {
    ull bb;
    cin >> bb;
    b[i + 1] = bb + b[i];
  }
  int ans = 0, j = m;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    while (a[i] + b[j] > k)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using PP = pair<int, pair<int, int>>;
using T = tuple<string, int, int>;
const string EMP = " ";
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), at(n + 1, 0), bt(m + 1, 0);
  int ae = 0, be = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    at[i + 1] = a[i] + at[i];
    if (at[i + 1] <= k)
      ae = i + 1;
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    bt[i + 1] = b[i] + bt[i];
    if (bt[i + 1] <= k)
      be = i + 1;
  }
  ll ans = 0;
  if (ae < be) {
    for (ll i = 0; i <= ae; i++) {
      if (at[i] > k)
        break;
      for (ll j = 0; j <= be; j++) {
        ll time = at[i] + bt[j];
        if (time > k)
          break;
        ans = max(ans, i + j);
      }
    }
  } else {
    for (ll i = 0; i <= be; i++) {
      if (bt[i] > k)
        break;
      for (ll j = 0; j <= ae; j++) {
        ll time = bt[i] + at[j];
        if (time > k)
          break;
        ans = max(ans, i + j);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int max(int a, int b) { return a > b ? a : b; }
int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  long A[200001];
  long B[200001];
  A[0] = B[0] = 0;
  int ans = 0;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  for (int i = 1; i <= M; i++)
    cin >> B[i];

  for (int i = 1; i < N; i++) {
    A[i + 1] += A[i];
  }
  for (int i = 1; i < M; i++) {
    B[i + 1] += B[i];
  }

  int j = M;
  for (int i = 0; i <= N; i++) {
    if (A[i] > K)
      break;
    while (A[i] + B[j] > K) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int max(int a, int b) { return a > b ? a : b; }
int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  long A[200001];
  long B[200001];
  A[0] = B[0] = 0;
  int ans = 0;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  for (int i = 1; i <= M; i++)
    cin >> B[i];

  for (int i = 1; i < N; i++) {
    A[i + 1] += A[i];
  }
  for (int i = 1; i < M; i++) {
    B[i + 1] += B[i];
  }

  for (int i = 0; i <= N; i++) {
    if (A[i] > K)
      break;
    for (int j = 0; j <= M; j++) {
      if (A[i] + B[j] <= K)
        ans = max(ans, i + j);
      else
        break;
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
int main() {
  FASTINOUT;
  ll x, m, k;
  cin >> x >> m >> k;
  ll a[x + 1], b[m];
  for (int i = 0; i < x; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  a[x] = 0;
  ll sum = 0, ans = 0, cnt = 0;
  int j = m - 1;
  for (int i = 1; i < m; i++)
    b[i] += b[i - 1];
  for (int i = 0; i <= x; i++) {
    ll cur = sum;
    cnt = i;
    for (; j >= 0; j--) {
      if (cur + b[j] <= k) {
        cnt += j + 1;
        break;
      }
    }
    ans = max(ans, cnt);
    sum += a[i];
    if (sum > k)
      break;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define ll long long
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
map<pair<int, pair<int, int>>, int> dp;
const int N = 200009;
int n, m, k, a[N], b[N];
int f(int i, int j, int val) {
  if (i == n && j == m)
    return 0;
  if (val == 0)
    return 0;
  int ret = 0;
  if (dp.find({i, {j, val}}) != dp.end())
    return dp[{i, {j, val}}];
  if (i < n && a[i] <= val)
    ret = max(ret, 1 + f(i + 1, j, val - a[i]));
  if (j < m && b[j] <= val)
    ret = max(ret, 1 + f(i, j + 1, val - b[j]));
  return dp[{i, {j, val}}] = ret;
}
int main() {
  FASTINOUT;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int j = 0; j < m; j++)
    scanf("%d", &b[j]);
  printf("%d", f(0, 0, k));
  // cout<<f(0,0,k);
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, M;
  long long int K;
  cin >> N >> M >> K;

  vector<long long int> A(N);
  vector<long long int> B(M);
  vector<long long int> a(N + 1);
  vector<long long int> b(M + 1);
  a.at(0) = 0;
  b.at(0) = 0;
  int i;

  for (i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (i = 0; i < M; i++) {
    cin >> B.at(i);
  }

  int ans = 0;
  for (i = 0; i < N; i++) {
    a.at(i + 1) = a.at(i) + A.at(i);
  }

  for (i = 0; i < M; i++) {
    b.at(i + 1) = b.at(i) + B.at(i);
  }

  int j = M;
  for (i = 0; i < N + 1; i++) {
    if (a.at(i) > K) {
      break;
    }
    while (a.at(i) + b.at(j) > K) {
      j--;
    }
    ans = max(i + j, ans);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int M, N;
  long long int K;
  int r = 0;
  cin >> N >> M >> K;
  // vector<long long> A(N);
  long long int A, B;
  vector<long long int> SA(N + 1, 0);
  // vector<long long> B(M);
  vector<long long int> SB(M + 1, 0);
  // int p = 0;
  // int q = 0;

  for (int i = 0; i < N; i++) {
    cin >> A;
    SA[i + 1] = SA[i] + A;
  }
  for (int j = 0; j < M; j++) {
    cin >> B;
    SB[j + 1] = SB[j] + B;
  }
  for (int i = 0; i < N + 1; i++) {
    if (SA[i] > K) {
      break;
    }
    int j = M;
    // int j = q;
    while (K - SA[i] < SB[j]) {
      j -= 1;
    }
    if (r < i + j) {
      r = max(r, i + j);
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  a[0] = 0;
  vector<ll> b(m + 1);
  b[0] = 0;
  ll ai = 0, bj = 0;
  rep(i, n) {
    ll tmp;
    cin >> tmp;
    ai += tmp;
    a[i + 1] = ai;
  }
  rep(j, m) {
    ll tmp;
    cin >> tmp;
    bj += tmp;
    b[j + 1] = bj;
  }
  ll ans = 0, j = m;
  rep(i, n + 1) {
    if (a[i] > k)
      break;
    while (b[j] > k - a[i])
      j -= 1;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  a[0] = 0;
  vector<ll> b(m + 1);
  b[0] = 0;
  ll ai = 0, bj = 0;
  rep(i, n) {
    ll tmp;
    cin >> tmp;
    ai += tmp;
    a[i + 1] = ai;
  }
  rep(j, m) {
    ll tmp;
    cin >> tmp;
    bj += tmp;
    b[j + 1] = bj;
  }
  ll ans = 0;
  rep(i, n + 1) {
    if (a[i] > k)
      break;
    rep(j, m + 1) {
      if (a[i] + b[j] <= k)
        ans = max(ans, i + j);
      else
        continue;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  vector<int> A;
  vector<ll> B;

  rep(i, n) {
    int a;
    cin >> a;

    A.push_back(a);
  }

  ll b_total = 0;

  rep(i, m) {
    int b;
    cin >> b;

    b_total += b;
    B.push_back(b_total);
  }

  ll total_a = 0;
  int max_books = 0;
  int bi = B.size() - 1;

  while (bi >= 0 && B[bi] > k - total_a) {
    bi--;
  }
  max_books = max(max_books, bi + 1);

  rep(ai, n) {
    total_a += A[ai];
    if (total_a > k)
      break;

    while (bi >= 0 && B[bi] > k - total_a) {
      bi--;
    }

    max_books = max(max_books, ai + 1 + bi + 1);
  }

  cout << max_books << endl;

  return 0;
}


Error: 0

Fixed submission:

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

bool a_is_better(vector<pair<int, int>> A, int ai, vector<pair<int, int>> B,
                 int bi) {
  if (ai >= A.size() && bi >= B.size())
    return false;
  if (ai >= A.size() && bi < B.size())
    return false;
  if (bi >= B.size() && ai < A.size())
    return true;
  if (A[ai].first == B[bi].first) {
    if (A[ai].second < B[bi].second)
      return true;
    if (A[ai].second > B[bi].second)
      return false;
    return a_is_better(A, ai + 1, B, bi + 1);
  }
  if (A[ai].first < B[bi].first)
    return true;
  return false;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  vector<pair<int, int>> A;
  vector<pair<int, int>> B;

  rep(i, n) {
    int a;
    cin >> a;

    if (i > 0 && A[i - 1].first == a) {
      A[i - 1].second++;
    } else {
      A.push_back(make_pair(a, 1));
    }
  }
  rep(i, m) {
    int b;
    cin >> b;

    if (i > 0 && B[i - 1].first == b) {
      B[i - 1].second++;
    } else {
      B.push_back(make_pair(b, 1));
    }
  }

  int total = 0;
  int ai, bi;
  ai = bi = 0;

  while (k > 0) {
    bool a_first = a_is_better(A, ai, B, bi);

    if (a_first) {
      if (A[ai].first > k)
        break;
      int books = min(A[ai].second, k / A[ai].first);
      k -= books * A[ai].first;
      total += books;
      ai++;
    } else {
      if (B[bi].first > k)
        break;
      int books = min(B[bi].second, k / B[bi].first);
      k -= books * B[bi].first;
      total += books;
      bi++;
    }
  }

  cout << total << endl;

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

#include <bits/stdc++.h>
#define lli long long int
using namespace std;
int main() {
  lli n, m, k, i, j, MAX = 0;
  cin >> n >> m >> k;
  lli a[n + 1], b[m + 1];
  a[0] = b[0] = 0;
  for (i = 1; i < n + 1; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (j = 1; j < m + 1; j++) {
    cin >> b[j];
    b[j] += b[j - 1];
  }
  j = m;
  for (i = 0; i < n + 1; i++) {
    if (a[i] > k)
      break;
    while (b[j] > k - a[i])
      j--;
    if ((i + j) > MAX)
      MAX = i + j;
  }
  cout << MAX << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lli long long int
using namespace std;
int main() {
  lli n, m, k, i, j, MAX = 0;
  cin >> n >> m >> k;
  lli a[n + 1], b[m + 1];
  a[0] = b[0] = 0;
  for (i = 1; i < n + 1; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (j = 1; j < n + 1; j++) {
    cin >> b[j];
    b[j] += b[j - 1];
  }
  j = m;
  for (i = 0; i < n + 1; i++) {
    while (b[j] > k - a[i])
      j--;
    if ((i + j) > MAX)
      MAX = i + j;
  }
  cout << MAX << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);

  for (ll i = 1; i <= n; i++) {
    ll tmp;
    cin >> tmp;
    a[i] = tmp + a[i - 1];
  }

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

  ll res = 0;
  for (ll i = 0; i <= n; i++) {
    ll tmp = a[i];
    ll nokori = k - tmp;
    if (nokori < 0)
      break;
    if (nokori == 0) {
      res = max(res, i);
      continue;
    }

    auto it = lower_bound(b.begin(), b.end(), nokori);
    ll index;
    if (*it == nokori) {
      index = distance(b.begin(), it);
    } else {
      index = distance(b.begin(), --it);
    }

    res = max(res, index + i);
  }

  cout << res << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);

  for (ll i = 1; i <= n; i++) {
    ll tmp;
    cin >> tmp;
    a[i] = tmp + a[i - 1];
  }

  for (ll i = 1; i <= n; i++) {
    ll tmp;
    cin >> tmp;
    b[i] = tmp + b[i - 1];
  }

  ll res = 0;
  for (ll i = 0; i <= n; i++) {
    ll tmp = a[i];
    ll nokori = k - tmp;
    if (nokori < 0)
      break;

    auto it = lower_bound(b.begin(), b.end(), nokori);
    ll index = distance(b.begin(), --it);

    res = max(res, index + i);
  }

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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int N, M;
  ll K;
  cin >> N >> 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];
  }

  ll a[N + 1], b[M + 1];
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < N; i++) {
    a[i + 1] = a[i] + A[i];
  }
  for (int i = 0; i < M; i++) {
    b[i + 1] = b[i] + B[i];
  }
  int ans = 0;
  int j = M;
  for (int i = 0; i < N + 1; i++) {
    if (a[i] > K) {
      break;
    }
    while (b[j] > K - a[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }

  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define rep(i, n) for (int i = 0; i < (n); i++)

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

  ll a[N + 1], b[M + 1];
  for (int i = 0; i < N; i++) {
    a[i + 1] = a[i] + A[i];
  }
  for (int i = 0; i < M; i++) {
    b[i + 1] = b[i] + B[i];
  }
  int ans = 0;
  int j = M;
  for (int i = 0; i < N; i++) {
    if (a[i] > K) {
      break;
    }
    while (b[j] > K - a[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n1, n2, k;
  cin >> n1 >> n2 >> k;
  ll a[n1], b[n2];
  vector<ll> sum1(1 + n1);
  vector<ll> sum2(1 + n2);
  for (ll i = 0; i < n1; i++) {
    cin >> a[i];
    sum1[i + 1] = sum1[i] + a[i];
  }
  for (ll i = 0; i < n2; i++) {
    cin >> b[i];
    sum2[i + 1] = sum2[i] + b[i];
  }
  ll ans = 0;
  ll j = n2;
  for (ll i = 0; i <= n1; i++) {
    if (sum1[i] > k) {
      break;
    }
    while (sum2[j] > k - sum1[i]) {
      j -= 1;
    }
    ans = max(ans, i + j);
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll b_search(vector<ll> sum2, ll left) {
  ll ans = 0;
  ll s = 0;
  ll e = sum2.size() - 1;
  while (s <= e) {
    ll m = (s + e) / 2;
    if (sum2[m] <= left) {
      s = m + 1;
      ans = m;
    } else {
      e = m - 1;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n1, n2, time;
  cin >> n1 >> n2 >> time;
  ll a[n1], b[n2];
  vector<ll> sum1(n1 + 1);
  vector<ll> sum2(n2 + 1);
  sum1[0] = 0;
  sum2[0] = 0;
  for (ll i = 0; i < n1; i++) {
    cin >> a[i];
    sum1[i + 1] = sum1[i] + a[i];
  }
  ll max_books = 0;
  for (ll i = 0; i < n2; i++) {
    cin >> b[i];
    sum2[i + 1] = sum2[i] + b[i];
    if (sum2[i + 1] <= time) {
      ll j = b_search(sum1, time - sum2[i + 1]);
      max_books = max(max_books, i + 1 + j);
    }
  }
  ll j = b_search(sum1, time);
  max_books = max(max_books, j);
  cout << max_books;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int main() {
  long long minutes = 0;
  int ans = 0, j;
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) {
    cin >> b[i];
    minutes += b[i];
  }

  j = m;
  rep(i, n + 1) {
    while (k < minutes && j > 0) {
      --j;
      minutes -= b[j];
    }
    if (k >= minutes)
      ans = max(ans, i + j);
    if (i == n)
      break;
    minutes += a[i];
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int main() {
  long minutes = 0;
  int ans, cnt = 0, numa, numb;
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) {
    cin >> b[i];
    if (minutes + b[i] <= k) {
      minutes += b[i];
      cnt = i + 1;
    }
  }
  ans = cnt;
  numb = cnt - 1;
  numa = 0;
  rep(i, cnt) {
    if (i != 0) {
      minutes -= b[numb - i];
      cnt--;
    }
    while (k >= minutes + a[numa]) {
      minutes += a[numa];
      cnt++;
      numa++;
      if (numa == n)
        break;
    }
    ans = max(ans, cnt);
    if (numa == n)
      break;
  }
  cout << ans << endl;

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

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

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

  int n;
  cin >> n;
  int m;
  cin >> m;
  ll k;
  cin >> k;
  vector<ll> a = vector<ll>(n);
  vector<ll> b = vector<ll>(m);
  vector<ll> asum = vector<ll>(n + 1);
  vector<ll> bsum = vector<ll>(m + 1);
  asum[0] = 0;
  bsum[0] = 0;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    asum[i + 1] = asum[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    bsum[i + 1] = bsum[i] + b[i];
  }

  int ans = 0;
  int bestj = m;
  for (int i = 0; i <= n; i++) {
    for (int j = bestj; j > -1; j--) {
      if (asum[i] + bsum[j] <= k) {
        ans = max(ans, i + j);
        bestj = j;
        break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int n;
  cin >> n;
  int m;
  cin >> m;
  ll k;
  cin >> k;
  vector<ll> a = vector<ll>(n);
  vector<ll> b = vector<ll>(m);
  vector<ll> asum = vector<ll>(n + 1);
  vector<ll> bsum = vector<ll>(m + 1);
  asum[0] = 0;
  bsum[0] = 0;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    asum[i + 1] = asum[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    bsum[i + 1] = bsum[i] + b[i];
  }

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = m; j > -1; j--) {
      if (asum[i] + bsum[j] <= k) {
        ans = max(ans, i + j);
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define int long long int
using namespace std;
signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n], b[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int j = 0; j < m; j++) {
    cin >> b[j];
  }
  int x = 0, y = 0, sum1 = 0, sum2 = 0;
  while (y < m && (sum2 + b[y]) <= k) {
    sum2 += b[y];
    y++;
  }
  y--;
  int ans = y + 1;

  for (int i = 0; i < n; i++) {
    sum1 += a[i];
    while (sum2 > (k - sum1) && y >= 0) {
      sum2 -= b[y];
      y--;
    }
    if (sum1 <= k) {
      ans = max(i + y + 2, ans);
    }
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
using namespace std;
signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int j = 0; j < m; j++) {
    cin >> b[j];
  }
  int x = 0, y = 0, sum1 = 0, sum2 = 0;
  while (y < m && (sum2 + b[y]) <= k) {
    sum2 += b[y];
    y++;
  }
  y--;
  int ans = y + 1;

  for (int i = 0; i < n; i++) {
    sum1 += a[i];
    while (sum2 > (k - sum1) && y >= 0) {
      sum2 -= b[y];
      y--;
    }
    if (sum1 <= k) {
      ans = max(i + y + 2, ans);
    }
  }
  cout << ans;
}