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

#include "bits/stdc++.h"

typedef long long ll;
typedef long double ld;

/*
int %d
long long %lld
double %lf %0.6lf
long double %0.6Lf
char %c
string %s
*/

int main() {
#if _MSC_VER
  freopen("input.txt", "r", stdin);
#endif
  ll k;
  int n, m;
  scanf("%d%d%lld", &n, &m, &k);
  std::vector<int> a(n);
  std::vector<ll> b(m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%lld", &b[i]);
    if (i) {
      b[i] += b[i - 1ll];
    }
  }
  int ans = std::upper_bound(b.begin(), b.end(), k) - b.begin();
  ll acum = 0;
  for (int i = 0; i < n; ++i) {
    acum += a[i];
    if (acum > k) {
      break;
    }
    int temp = std::upper_bound(b.begin(), b.end(), k - acum) - b.begin();
    ans = std::max(ans, i + 1 + temp);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 0, m = 0, k = 0, counter = 0;
  cin >> n >> m >> k;
  int arr1[n];
  for (int i = 0; i < n; i++) {
    cin >> arr1[i];
  }
  int arr2[n];
  for (int i = 0; i < m; i++) {
    cin >> arr2[i];
  }
  for (int i = 0; i < min(n, m); i++) {
    if (k - arr1[i] >= 0) {
      k = k - arr1[i];
      counter++;
    }
    if (k - arr2[i] >= 0) {
      k = k - arr2[i];
      counter++;
    }
  }
  if (n > m) {
    for (int i = m; i < n; i++)
      if (k - arr1[i] >= 0) {
        k = k - arr1[i];
        counter++;
      } else {
        break;
      }
  }
  if (n < m) {
    for (int i = n; i < m; i++)
      if (k - arr2[i] >= 0) {
        k = k - arr2[i];
        counter++;
      } else {
        break;
      }
  }
  cout << counter;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string>
#define ll long long

using namespace std;

const int N = 2e6 + 5;

long long a[N], b[N];

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

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

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

const int N = 4e6 + 5;

long long a[N];

int main() {
  long long m, n, k;
  cin >> n >> m >> k;
  for (int i = 0; i < n + m; ++i) {
    cin >> a[i];
  }
  int i = 0, ans = 0;
  sort(a, a + n + m);
  while (k - a[i] >= 0) {
    ++ans;
    k -= a[i];
    ++i;
  }
  cout << ans << 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: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long int n, m, k, temp, stepsback = 0;
  long long int ia = 0, ib = 0, books = 0, t = 0;
  cin >> n >> m >> k;

  vector<int> a;
  vector<int> b;

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

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

  while (t <= k && ia < n && ib < m) {
    while (a[ia] == b[ib] && t < k) {
      ia++;
      ib++;
      books++;
      stepsback++;
      t += a[ia];
    }
    if (a[ia] < b[ib] && t < k) {
      t += a[ia];
      ia++;
      ib -= stepsback;
      books++;

    } else if (a[ia] > b[ib] && t < k) {
      t += b[ib];
      ib++;
      ia -= stepsback;
      books++;
    }
    stepsback = 0;
  }

  while (t < k && ia < n) {
    t += a[ia];
    ia++;
    books++;
  }

  while (t < k && ib < m) {
    t += b[ib];
    ib++;
    books++;
  }

  if (t > k)
    books--;

  cout << books;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void solve(long long N, long long M, long long K, std::vector<long long> A,
           std::vector<long long> B) {

  vector<long long> Atab(N + 1, LLONG_MAX);
  vector<long long> Btab(M + 1, LLONG_MAX);

  Atab[0] = 0;
  for (long long i = 1; i <= N; i++) {
    Atab[i] = Atab[i - 1] + A[i - 1];
    if (Atab[i] > K)
      break;
  }

  Btab[0] = 0;
  for (long long i = 1; i <= M; i++) {
    Btab[i] = Btab[i - 1] + B[i - 1];
    if (Btab[i] > K)
      break;
  }

  long long mymax = 0;
  for (long long i = 0; i <= N; i++) {
    if (Atab[i] > K)
      break;

    auto it = upper_bound(Btab.begin(), Btab.end(), K - Atab[i]);
    mymax = max(mymax, i + distance(Btab.begin(), it - 1));
  }

  cout << mymax << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &B[i]);
  }
  solve(N, M, K, std::move(A), std::move(B));
  return 0;
}


Error: TLE

Fixed submission:

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

void solve(long long N, long long M, long long K, std::vector<long long> A,
           std::vector<long long> B) {

  vector<long long> Atab(N + 1);
  vector<long long> Btab(M + 1);

  Atab[0] = 0;
  for (long long i = 1; i <= N; i++) {
    Atab[i] = Atab[i - 1] + A[i - 1];
    if (Atab[i] > K)
      break;
  }

  Btab[0] = 0;
  for (long long i = 1; i <= M; i++) {
    Btab[i] = Btab[i - 1] + B[i - 1];
    if (Btab[i] > K)
      break;
  }

  long long mymax = 0;
  for (long long i = 0; i <= N; i++) {
    if (Atab[i] > K)
      break;
    for (long long j = 0; j <= M; j++) {
      if (Atab[i] + Btab[j] <= K)
        mymax = max(mymax, i + j);
      else {
        break;
      }
    }
  }

  cout << mymax << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &B[i]);
  }
  solve(N, M, K, std::move(A), std::move(B));
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define fout(x) cout << fixed << setprecision(20) << x << endl
const int MOD = 1e9 + 7;
const ll LINF = (ll)1e18 - 1;
const int INF = 1e9 - 1;
const double EPS = 1e-8;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

auto print = [](auto &v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  VL a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  VL sa(n + 1, 0), sb(m + 1, 0);
  rep(i, n) sa[i + 1] = sa[i] + a[i];
  rep(i, m) sb[i + 1] = sb[i] + b[i];
  rep(i, n) {
    ll atime = sa[i + 1];
    if (atime > k)
      break;
    else if (atime == k) {
      ans = max(ans, i + 1LL);
      continue;
    } else {
      ll it = upper_bound(all(sb), (k - atime)) - sb.begin();
      ans = max(ans, it - 1 + i + 1);
    }
  }
  ans = max(ans, (ll)(upper_bound(all(sb), k) - sb.begin() - 1LL));
  out(ans);

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define fout(x) cout << fixed << setprecision(20) << x << endl
const int MOD = 1e9 + 7;
const ll LINF = (ll)1e18 - 1;
const int INF = 1e9 - 1;
const double EPS = 1e-8;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

auto print = [](auto &v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  VL a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll ca, cb;
  VL sa(n + 1, 0), sb(n + 1, 0);
  rep(i, n) sa[i + 1] = sa[i] + a[i];
  rep(i, n) sb[i + 1] = sb[i] + b[i];
  rep(i, n) {
    ll atime = sa[i + 1];
    if (atime > k)
      break;
    else if (atime == k) {
      ans = max(ans, i + 1LL);
      continue;
    } else {
      ll it = upper_bound(all(sb), (k - atime)) - sb.begin();
      ans = max(ans, it + i + 1);
    }
  }
  ans = max(ans, (ll)(upper_bound(all(sb), k) - sb.begin()));
  out(ans);

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

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> b[i];
  REP(i, n) { a[i] += a[i - 1]; }
  REP(i, m) { b[i] += b[i - 1]; }
  ll ans = 0;
  rep(i, n + 1) {
    if (a[i] > k) {
      break;
    }
    ll r = m + 1;
    ll l = 0;
    while (r - l > 1) {
      ll t = (l + r) / 2;
      if (b[t] > k - a[i]) {
        r = t;
      } else {
        l = t;
      }
    }
    ans = max(ans, i + l);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

ll const INF = 1LL << 60;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll ans = 0;
  ll time = 0;
  ll ida = 0;
  ll idb = 0;
  while (time <= k) {
    ll p, q;
    if (ida < n)
      p = a[ida];
    if (idb < m)
      q = b[ida];
    if (ans >= n + m)
      break;
    if (p <= q) {
      time += a[ida];
      ida++;
      if (time <= k) {
        ans++;
      } else {
        break;
      }
    }
    if (q <= p) {
      time += b[idb];
      idb++;
      if (time <= k) {
        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() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long n, m, k;
  cin >> n >> m >> k;

  vector<long long> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 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] += a[i - 1];

  for (int i = 1; i <= m; i++)
    b[i] += b[i - 1];

  int cnt = 0;

  for (int i = 0; i <= n; i++) {
    long long rem = k - a[i];

    if (rem >= 0) {
      auto l = lower_bound(b.begin(), b.end(), rem);
      int idx = l - b.begin();
      if (idx > m || b[idx] > rem)
        idx--;

      cnt = max(cnt, i + idx);
    }
  }
  cout << cnt << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
unsigned long long int *readStack(int length) {
  unsigned long long int *stack = new unsigned long long int[length];
  for (int i = 0; i < length; i++) {
    cin >> stack[i];
    // cout << stack[i] << " ";
  }
  // cout << endl;
  return stack;
}
unsigned long long int getSum(unsigned long long int tree[], int index) {
  unsigned long long int sum = 0;
  index++;
  while (index > 0) {
    sum += tree[index];
    // cout << index << ' ';
    index -= index & (-index);
    // cout << index << endl;
  }
  return sum;
}
unsigned long long int *getTree(unsigned long long int *s, int l, int k) {
  unsigned long long int *tree = new unsigned long long int[l + 1];
  for (int i = 1; i <= l; i++)
    tree[i] = 0;
  for (int i = 0; i < l; i++) {
    int index = i + 1;
    while (index <= l) { // Tree creation problems
      // if(tree[index] + s[i] > k)
      // return tree;
      tree[index] += s[i];
      // cout << index << ' ';
      index += index & (-index);
      // cout << index << endl;
    }
  }
  // for (int i = 1; i <= l; i++)
  // cout << tree[i] << " ";
  // cout << endl;
  return tree;
}
int main() {
  int n, m;
  unsigned long long int k;
  cin >> n >> m >> k;
  unsigned long long int *aStack = readStack(n);
  unsigned long long int *bStack = readStack(m);
  unsigned long long int *aTree = getTree(aStack, n, k);
  unsigned long long int *bTree = getTree(bStack, m, k);
  // check if time sum of stack is <= k
  // if (getSum(aTree, n - 1) + getSum(bTree, m - 1) <= k)
  // cout << n + m;
  // else {
  bool zeroFlag = false;
  int numOfBooks = 0;
  for (int i = -1; i < n; i++) //-1 means no
    for (int j = -1; j < m; j++) {
      unsigned long long int time = 0;
      if (i == -1 && j == -1)
        continue;
      else {
        if (i >= 0)
          time += getSum(aTree, i);
        if (j >= 0)
          time += getSum(bTree, j);
        // cout << i << " " << j << " " << time << endl;
        if (time <= k && i + j + 2 > numOfBooks) {
          numOfBooks = i + j + 2;
        }
      }
    }

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

#include <iostream>
#include <vector>

// Bの方をX冊:YY分というように足したものを計算しておく
// Aの方を1冊づつ見ていき、残り時間で⇑がどこまで出来るか二分探索とかで探す
//
// 別海
// Aが0:BがWのときよりBが増えることはないので、
// Bをそこを基準に減らしていく
// 減るならAの計算量も底が見えるし、減らないならO(N+M)で済む

int main() {
  int N, M, K;
  std::cin >> N >> M >> K;
  std::vector<int> A(N);
  std::vector<int> B(M);

  for (int i = 0; i < N; i++)
    std::cin >> A[i];
  for (int i = 0; i < M; i++)
    std::cin >> B[i];

  // Bの足した時間を出す
  std::vector<long long> sigmaB(M + 1);
  sigmaB[0] = 0;
  //	printf( "%lld|", sigmaB[0]);
  for (int i = 1; i <= M; i++) {
    sigmaB[i] = sigmaB[i - 1] + B[i - 1];
    //		printf("%lld|", sigmaB[i]);
  }
  //	printf("\\n");
  //	// Aを1冊づつ見ていく
  long long sigmaA = 0;
  int ans = 0;
  for (int i = 0; i < N + 1; i++) {
    if (i != 0) {
      sigmaA += A[i - 1];
      //			printf("%lld\\n", sigmaA);
      if (sigmaA > K) {
        break;
      }
    }

    // sigmaBから2分探索でいい感じのを探す
    int nokori_time = K - sigmaA;
    int min = -1;
    int max = M + 1;
    while (max - min > 1) // maxとminが隣あっていない間
    {
      //			printf("max=%d|min=%d", max, min);
      int j = (max + min) / 2;
      if (sigmaB[j] <= nokori_time) // ⇑で端数は切り捨てられるから
      { // ピッタリのときは低い方に落ち着く
        min = j;
        //				printf("|j=%d\\n", j);
      } else if (sigmaB[j] > nokori_time) {
        max = j;
        //				printf("|j=%d\\n", j);
      }
    }

    //		printf( "%d:%d[%lld]\\n", i, min, sigmaA + sigmaB[min] );
    ans = std::max(ans, i + min);
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

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

  std::vector<int> A(N);
  std::vector<int> B(M);

  for (int i = 0; i < N; i++)
    std::cin >> A[i];
  for (int i = 0; i < M; i++)
    std::cin >> B[i];

  long long ans = 0;
  int timer = 0;
  int A_idx = 0;
  int B_idx = 0;
  while (true) {
    // 本を選ぶ
    // 全探索は時間が許さない?
    // 仮　貪欲法
    if ((A_idx < N) && (B_idx < N)) {
      if (A[A_idx] <= B[B_idx]) {
        // printf("A");
        timer += A[A_idx];
        if (timer >= K) {
          // //printf("\\n");
          break;
        }
        A_idx++;
      } else {
        // printf("B");
        timer += B[B_idx];
        if (timer >= K) {
          // //printf("\\n");
          break;
        }
        B_idx++;
      }
    } else if (A_idx < M) {
      // printf("A");
      timer += A[A_idx];
      if (timer >= K) {
        // //printf("\\n");
        break;
      }
      A_idx++;
    } else if (B_idx < N) {
      // printf("B");
      timer += B[B_idx];
      if (timer >= K) {
        // //printf("\\n");
        break;
      }
      B_idx++;
    } else {
      break;
    }

    // printf("%d\\n", timer);
    ans++;
  }

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

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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
using ULL = unsigned long long;
using LL = long long;
using pii = pair<int, int>;
const int MAX_M = 2e5;
int N, M, K;
vector<int> books_A, books_B;
LL pref_sum_B[MAX_M + 1];

int b_search(const LL &max_val, const int &M) {
  int left = 0, right = M - 1, mid;
  int nr_books = 0;
  while (left <= right) {
    mid = left + (right - left) / 2;

    if (pref_sum_B[mid] <= max_val) {
      assert(mid + 1 > nr_books);
      nr_books = mid + 1;
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }

  return nr_books;
}

int main() {
  cin.sync_with_stdio(0);

  cin >> N >> M >> K;

  for (int i = 0; i < N; ++i) {
    int book_A;
    cin >> book_A;

    books_A.emplace_back(book_A);
  }
  for (int i = 0; i < M; ++i) {
    int book_B;
    cin >> book_B;

    books_B.emplace_back(book_B);
    if (i == 0) {
      pref_sum_B[i] = book_B;
    } else {
      pref_sum_B[i] = pref_sum_B[i - 1] + book_B;
    }
  }

  LL cur_pref_sum_A = 0;
  int res_nr_books = 0;
  for (int i = 0;; ++i) {
    res_nr_books = max(res_nr_books, i + b_search(K - cur_pref_sum_A, M));
    if (i == N || cur_pref_sum_A + books_A[i] > K) {
      break;
    }
    cur_pref_sum_A += books_A[i];
  }

  cout << res_nr_books;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
using ULL = unsigned long long;
using LL = long long;
using pii = pair<int, int>;
int N, M, K;
vector<int> books_A, books_B;
map<pii, int> cache;

int solve(const int &i, const int &j) {
  if (i == N && j == M) {
    return 0;
  }

  int &res_books = cache[{i, j}];
  if (res_books != 0) {
    return res_books;
  }

  static int sum_so_far = 0;
  if (i != N && sum_so_far + books_A[i] <= K) {
    sum_so_far += books_A[i];
    res_books = max(res_books, 1 + solve(i + 1, j));
    sum_so_far -= books_A[i];
  }
  if (j != M && sum_so_far + books_B[j] <= K) {
    sum_so_far += books_B[j];
    res_books = max(res_books, 1 + solve(i, j + 1));
    sum_so_far -= books_B[j];
  }

  return res_books;
}

int main() {
  cin.sync_with_stdio(0);

  cin >> N >> M >> K;

  for (int i = 0; i < N; ++i) {
    int book_A;
    cin >> book_A;

    books_A.emplace_back(book_A);
  }
  for (int i = 0; i < M; ++i) {
    int book_B;
    cin >> book_B;

    books_B.emplace_back(book_B);
  }

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

#include <bits/stdc++.h>

#define pp push_back
#define pf push_front
#define mp make_pair
#define fs first
#define sc second
#define sf scanf

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

const int N = (int)1e5;
const int INF = (int)1e9 + 7;

int main(int argc, char *argv[]) {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  int n, m, k;
  sf("%d %d %d", &n, &m, &k);
  int a[n];
  int b[m];
  for (int i = 0; i < n; ++i) {
    sf("%d", &a[i]);
  }
  for (int i = 0; i < m; ++i) {
    sf("%d", &b[i]);
  }

  ll t = 0;
  int x = -1;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (t + a[i] > k)
      break;
    t += a[i];
    ++x;
    ++cnt;
  }
  int j;
  for (j = 0; j < m; ++j) {
    if (t + b[j] > k)
      break;
    t += b[j];
    ++cnt;
  }
  int ans = cnt;
  while (j < m && x >= 0) {
    t -= a[x];
    --x;
    --cnt;

    while (j < m && t + b[j] <= k) {
      t += b[j];
      ++j;
      ++cnt;
    }
    ans = max(ans, cnt);
  }

  printf("%d\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define pp push_back
#define pf push_front
#define mp make_pair
#define fs first
#define sc second
#define sf scanf

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

const int N = (int)2e5;
const int M = (int)1e7 + 7;
const int INF = (int)1e9 + 7;

int n, m, k;
int a[N + 7];
int b[N + 7];

int dp[M];

int f(int i, int j, int t) {
  if (t < 0)
    return -1;
  if (t < M && dp[t] != -1)
    return dp[t];
  if (i >= n && j >= m)
    return 0;

  int x = 0;
  int y = 0;
  if (i < n)
    x = f(i + 1, j, t - a[i]) + 1;
  if (j < m)
    y = f(i, j + 1, t - b[j]) + 1;

  if (t < M)
    dp[t] = max(x, y);
  return max(x, y);
}

int main(int argc, char *argv[]) {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  memset(dp, -1, sizeof(dp));
  sf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < n; ++i) {
    sf("%d", &a[i]);
  }
  for (int i = 0; i < m; ++i) {
    sf("%d", &b[i]);
  }

  printf("%d\\n", f(0, 0, k));

  return 0;
}