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

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

// だめだったけど、まだ諦めない
// 一旦バグ取りしたけど、これだとTLEになるから次で改善する
int main() {
  int N, M;
  long long K;
  cin >> N >> M >> K;

  int aCount = 0;
  map<int, long long> A, B;
  long long sum = 0;
  bool isMax = false;
  long long num;
  for (int i = 0; i < N; i++) {
    cin >> num;
    if (!isMax) {
      sum += num;
      if (sum >= K) {
        isMax = true;
      }
      if (sum <= K) {
        if (sum > K)
          break;
        A[i] = K - sum;
        aCount++;
      }
    }
  }
  sum = 0;
  isMax = false;
  int maxReadCount = aCount;
  int aLastNo = aCount;
  for (int i = 0; i < M; i++) {
    cin >> num;
    if (isMax) {
      continue;
    }
    sum += num;
    if (sum >= K) {
      isMax = true;
    }
    if (sum <= K) {
      if (sum > K)
        break;
      int a;
      for (int j = aLastNo; j >= 0; j--) {
        if (j == 0) {
          a = K;
        } else {
          a = A[j - 1];
        }
        if (sum > a)
          continue;
        // cout << "i:" << i << ", j:" << j;  // debug
        // cout << ", a:" << a << ", sum:" << sum << endl;  // debug
        maxReadCount = max((i + 1) + j, maxReadCount);
        aLastNo = j;
        break;
      }
    }
  }

  cout << maxReadCount;
}
/*
3 5 20
5 5 5
10 1 1 7 1
*/

Error: TLE

Fixed submission:

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

// だめだったけど、まだ諦めない
// 一旦バグ取りしたけど、これだとTLEになるから次で改善する
int main() {
  int N, M;
  long long K;
  cin >> N >> M >> K;

  int aCount = 0;
  map<int, long long> A, B;
  long long sum = 0;
  bool isMax = false;
  long long num;
  for (int i = 0; i < N; i++) {
    cin >> num;
    if (!isMax) {
      sum += num;
      if (sum >= K) {
        isMax = true;
      }
      if (sum <= K) {
        if (sum > K)
          break;
        A[i] = K - sum;
        aCount++;
      }
    }
  }
  sum = 0;
  isMax = false;
  int maxReadCount = aCount;
  for (int i = 0; i < M; i++) {
    cin >> num;
    if (isMax) {
      continue;
    }
    sum += num;
    if (sum >= K) {
      isMax = true;
    }
    if (sum <= K) {
      if (sum > K)
        break;
      int a;
      for (int j = aCount; j >= 0; j--) {
        if (j == 0) {
          a = K;
        } else {
          a = A[j - 1];
        }
        if (sum > a)
          continue;
        maxReadCount = max(i + (j + 1), maxReadCount);
        break;
      }
    }
  }

  cout << maxReadCount;
}
/*
3 5 20
5 5 5
10 1 1 7 1
*/
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 200123
#define ll long long
#define endl '\\n'

using namespace std;

int n, m, ans, curr;
ll k, a[N], b[N];

void print_vetor(ll v[], int size) {
  for (int i = 0; i < size; ++i) {
    cout << v[i] << ' ';
  }
  cout << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> k;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

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

  partial_sum(a, a + n, a);
  partial_sum(b, b + m, b);

  for (int i = 0; i < n; ++i) {
    curr = 0;
    if (k >= a[i])
      curr = i + 1;
    if (k > a[i]) {
      curr += upper_bound(b, b + m, k - a[i]) - b;
    }
    ans = max(ans, curr);
  }

  for (int i = 0; i < m; ++i) {
    curr = 0;
    if (k >= b[i])
      curr = i + 1;
    if (k > b[i]) {
      curr += upper_bound(a, a + n, k - b[i]) - a;
    }
    ans = max(ans, curr);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 100123
#define ll long long
#define endl '\\n'

using namespace std;

int n, m, ans, curr;
ll k, a[N], b[N];

void print_vetor(ll v[], int size) {
  for (int i = 0; i < size; ++i) {
    cout << v[i] << ' ';
  }
  cout << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> k;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

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

  partial_sum(a, a + n, a);
  partial_sum(b, b + m, b);

  for (int i = 0; i < n; ++i) {
    curr = 0;
    if (k >= a[i])
      curr = i + 1;
    if (k > a[i]) {
      curr += upper_bound(b, b + m, k - a[i]) - b;
    }
    ans = max(ans, curr);
  }

  for (int i = 0; i < m; ++i) {
    curr = 0;
    if (k >= b[i])
      curr = i + 1;
    if (k > b[i]) {
      curr += upper_bound(a, a + n, k - b[i]) - a;
    }
    ans = max(ans, curr);
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 1e9
#define All(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  vector<ll> ra(n + 1), rb(m + 1);
  ra[0] = 0, rb[0] = 0;
  rep(i, n) {
    cin >> a[i];
    ra[i + 1] = ra[i] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    rb[i + 1] = rb[i] + b[i];
  }
  ll ans = 0;
  auto idx = upper_bound(All(ra), k) - ra.begin();
  for (int i = idx - 1; i >= 0; i--) {
    auto idxb = upper_bound(All(rb), k - ra[i]) - rb.begin();
    idxb--;
    // cout<<i<<" "<<idxb<<endl;
    ans = max(ans, (ll)i + idxb);
  }
  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 1e9
#define All(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  int ia = 0, ib = 0;
  ll cnt = 0;
  while (1) {
    if (ia < n && a[ia] <= b[ib] && k >= a[ia]) {
      cnt++;
      k -= a[ia];
      ia++;
    } else if (ib < m && k >= b[ia]) {
      cnt++;
      k -= a[ib];
      ib++;
    } else {
      break;
    }
  }
  cout << cnt << "\\n";

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

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

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define setbits(x) __builtin_popcount(x)
#define zerobits(x) __builtin_ctz(x)
#define setbitsll(x) __builtin_popcountll(x)
#define zerobitsll(x) __builtin_ctzll(x)
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef map<int, int> mii;

const int MOD = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  vl arr1(n + 1), arr2(m + 1);
  for (ll i = 1; i <= n; ++i) {
    cin >> arr1[i];
  }
  for (ll i = 1; i <= m; ++i) {
    cin >> arr2[i];
  }
  for (int i = 1; i <= n; ++i) {
    if (i > 1) {
      arr1[i] += arr1[i - 1];
    }
  }
  for (int i = 1; i <= m; ++i) {
    if (i > 1) {
      arr2[i] += arr2[i - 1];
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (arr1[i] > k)
      break;
    ll curr = i;
    ans = max(ans, curr);
    ll idx = upper_bound(all(arr2), k - arr1[i]) - arr2.begin();
    // cout << idx << '\\n';
    --idx;
    if (idx < 0) {
      break;
    } else {
      curr += idx;
    }
    ans = max(ans, curr);
  }
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

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

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define setbits(x) __builtin_popcount(x)
#define zerobits(x) __builtin_ctz(x)
#define setbitsll(x) __builtin_popcountll(x)
#define zerobitsll(x) __builtin_ctzll(x)
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef map<int, int> mii;

const int MOD = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  vl arr1(n), arr2(m);
  for (ll i = 0; i < n; ++i) {
    cin >> arr1[i];
  }
  for (ll i = 0; i < m; ++i) {
    cin >> arr2[i];
  }
  for (int i = 0; i < n; ++i) {
    if (i > 0) {
      arr1[i] += arr1[i - 1];
    }
  }
  for (int i = 0; i < n; ++i) {
    if (i > 0) {
      arr2[i] += arr2[i - 1];
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    if (arr1[i] > k)
      break;
    ll curr = i + 1;
    ans = max(ans, curr);
    ll idx = upper_bound(all(arr2), k - arr1[i]) - arr2.begin();
    --idx;
    if (idx < 0) {
      break;
    } else {
      curr += idx + 1;
    }
    ans = max(ans, curr);
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
#define ll long long int
int A[200002], B[200002];

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    ll n, m, ms;
    cin >> n >> m >> ms;

    for (int A_i = 0; A_i < n; A_i++) {
      cin >> A[A_i];
    }
    for (int B_i = 0; B_i < m; B_i++) {
      cin >> B[B_i];
    }

    ll sum = 0;
    ll x = 0, y = 0;

    while (x < n && sum + A[x] <= ms) {
      sum += A[x++];
    }

    ll ans = x;

    while (y < m && x >= 0) {
      sum += B[y++];
      while (sum > ms && x > 0) {
        sum -= A[--x];
      }

      if (sum <= ms && ans < x + y) {
        ans = x + y;
      }
    }

    cout << ans << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int A[100002], B[100002];

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, m, ms;
    scanf("%d%d%d", &n, &m, &ms);

    for (int A_i = 0; A_i < n; A_i++) {
      scanf("%d", &A[A_i]);
    }
    for (int B_i = 0; B_i < m; B_i++) {
      scanf("%d", &B[B_i]);
    }

    long long sum = 0;
    int x = 0, y = 0;

    while (x < n && sum + A[x] <= ms) {
      sum += A[x++];
    }

    int ans = x;

    while (y < m && x >= 0) {
      sum += B[y++];
      while (sum > ms && x > 0) {
        sum -= A[--x];
      }

      if (sum <= ms && ans < x + y) {
        ans = x + y;
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  long long ai = 0;
  long long bi = 0;
  long long time = 0;

  for (ai = 0; ai < A.size(); ++ai) {
    if (A[ai] + time <= K) {
      time += A[ai];
    } else {
      break;
    }
  }
  for (bi = 0; bi < B.size(); ++bi) {
    if (time <= K && max < ai + bi)
      max = ai + bi;

    time += B[bi];
    while (time > K && ai > 0) {
      ai--;
      time -= A[ai];
    }
  }
  if (time <= K && max < ai + bi)
    max = ai + bi;

  cout << max << 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: 0

Fixed submission:

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

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

  for (int i = 0; i < B.size(); ++i) {
    marged.push_back(A[B.size() - i - 1]);
  }
  for (int i = 0; i < A.size(); ++i) {
    marged.push_back(A[i]);
  }

  long int max = 0;
  long int begin = 0;
  long int end = 0;

  long int time = 0;
  for (int i = begin; i < marged.size();) {
    if (marged[i] <= K - time) {
      time += marged[i];
      end = i + 1;
      if (max < end - begin)
        max = end - begin;
      ++i;
    } else {
      time -= marged[begin];
      begin++;
    }
  }

  cout << max << 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: Time Limit Exceeded

#include <stdio.h>
long long pa[200005], pb[200005], a[200005], b[200005];
int main() {
  int n, m;
  long long k;
  scanf("%d%d%lld", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    pa[i] = pa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    scanf("%lld", &b[i]);
    pb[i] = pb[i - 1] + b[i];
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (k - pa[i] < 0ll)
      break;
    k -= pa[i];
    int l = 0, r = m;
    while (l < r) {
      int mid = (l + r) / 2;
      if (pb[mid] <= k)
        l = mid + 1;
      else
        r = mid;
    }
    if (pb[l] > k)
      l--;
    if (i + l > ans)
      ans = i + l;
    k += pa[i];
  }
  printf("%d", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int pa[200005], pb[200005], a[200005], b[200005];
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    pa[i] = pa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &b[i]);
    pb[i] = pb[i - 1] + b[i];
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (k - pa[i] < 0)
      break;
    int j = 0;
    k -= pa[i];
    for (; j <= m; j++)
      if (pb[j] > k)
        break;
    j--;
    if (i + j > ans)
      ans = i + j;
    k += pa[i];
  }
  printf("%d", ans);
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define ubo upper_bound
#define lbo lower_bound
#define VI vector<int>
#define VL vector<ll>
#define VP vector<pii>
#define all(x) x.begin(), x.end()
#define M vector<VI>

const int N = 202020;

using namespace std;

int n, m;
ll k, a[N], b[N];

int main() {

  cin >> n >> m >> k;
  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 l = 0, r = m, ans = 0;
  for (; l <= n && r >= 0; l++) {
    while (r >= 0 && a[l] + b[r] > k)
      r--;
    ans = max(ans, l + r);
  }

  cout << ans << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define ubo upper_bound
#define lbo lower_bound
#define VI vector<int>
#define VL vector<ll>
#define VP vector<pii>
#define all(x) x.begin(), x.end()
#define M vector<VI>

const int N = 202020;

using namespace std;

int n, m;
ll k, a[N], b[N];

int main() {

  cin >> n >> m >> k;
  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 l = 0, r = m, ans = 0;
  for (; l <= n && r >= 0; l++) {
    while (a[l] + b[r] > k)
      r--;
    ans = max(ans, l + r);
  }

  cout << ans << '\\n';

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

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

int main() {
  int N, M;
  ll K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  vector<ll> Asum(N + 1, 0);
  vector<ll> Bsum(M + 1, 0);
  for (int i = 1; i <= N; i++)
    Asum[i] = Asum[i - 1] + A[i - 1];
  for (int i = 1; i <= M; i++)
    Bsum[i] = Bsum[i - 1] + B[i - 1];

  int ans = 0;
  int best0 = M;
  rep(i, N + 1) {
    int j;
    for (j = best0; j >= 0; --j) {
      if (Bsum[j] <= K - Asum[i] && j >= 0) {
        ans = max(ans, i + j);
        break;
      }
    }
    best0 = j;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  ll K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  vector<ll> Asum(N + 1, 0);
  vector<ll> Bsum(M + 1, 0);
  for (int i = 1; i <= N; i++)
    Asum[i] = Asum[i - 1] + A[i - 1];
  for (int i = 1; i <= M; i++)
    Bsum[i] = Bsum[i - 1] + B[i - 1];

  int ans = 0;
  rep(i, N + 1) {
    int j = 0;
    rep(j, M + 1) {
      if (Bsum[j] <= K - Asum[i] && j <= M)
        ans = max(ans, i + j);
      else
        break;
    }
  }

  cout << ans << endl;

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

#include <cmath>
#include <iostream>
using namespace std;

// const double PI=3.141592653589793238;
int main(void) {
  long long N, M, K;

  cin >> N >> M >> K;

  int i, j;
  long long _a, _b;
  int ans = 0;

  long long a[N + 1], b[M + 1];

  a[0] = b[0] = 0;

  for (i = 1; i < N + 1; i++) {
    cin >> _a;
    a[i] = a[i - 1] + _a;
  }
  for (j = 1; j < M + 1; j++) {
    cin >> _b;
    b[j] = b[j - 1] + _b;
  }
  j = M;

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

    while (b[j] + a[i] > K)
      j -= 1;

    ans = max(ans, i + j);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

// const double PI=3.141592653589793238;
int main(void) {
  long long N, M, K;

  cin >> N >> M >> K;

  int i, j;
  long long _a, _b;
  int ans = 0;

  long long a[N + 1], b[M + 1];

  a[0] = b[0] = 0;

  for (i = 1; i < N + 1; i++) {
    cin >> _a;
    a[i] = a[i - 1] + _a;
  }
  for (j = 1; j < M + 1; j++) {
    cin >> _b;
    b[j] = b[j - 1] + _b;
  }

  for (i = N; i >= 0; i--) {
    for (j = 0; j <= M; j++) {
      if (a[i] + b[j] > K)
        break;
      else
        ans = max(ans, i + j);
    }
  }

  cout << ans;

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

#include <algorithm>
#include <iostream>
#include <vector>

int main() {

  size_t n, m, k;
  std::cin >> n >> m >> k;

  std::vector<size_t> a = {0}, b = {0};

  for (size_t i = 1; i < n + 1; i++) {
    size_t in;
    std::cin >> in;
    a.emplace_back(a.at(i - 1) + in);
  }

  for (size_t i = 1; i < m + 1; i++) {
    size_t in;
    std::cin >> in;
    b.emplace_back(b.at(i - 1) + in);
  }

  size_t ans = 0, index = m;

  for (size_t i = 0; i < n + 1; i++) {
    if (a.at(i) > k)
      break;

    while (b.at(index) > k - a.at(i))
      index--;

    ans = std::max(ans, i + index);
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

size_t dfs(const std::vector<size_t> &a, const std::vector<size_t> &b,
           size_t idx_a, size_t idx_b, size_t cnt, size_t k) {
  if (cnt > k)
    return idx_a + idx_b - 1;

  if (idx_a == a.size()) {
    while (cnt < k) {
      cnt += b.at(idx_b);
      idx_b++;
    }

    if (cnt > k)
      idx_b--;

    return idx_a + idx_b;
  } else if (idx_b == b.size()) {
    while (cnt < k) {
      cnt += a.at(idx_a);
      idx_a++;
    }

    if (cnt > k)
      idx_a--;

    return idx_a + idx_b;
  } else {
    size_t ret1 = dfs(a, b, idx_a + 1, idx_b, cnt + a.at(idx_a), k);
    size_t ret2 = dfs(a, b, idx_a, idx_b + 1, cnt + b.at(idx_b), k);

    return std::max(ret1, ret2);
  }
}

int main() {

  size_t n, m, k;
  std::cin >> n >> m >> k;

  std::vector<size_t> a(n), b(m);

  for (auto &&e : a) {
    std::cin >> e;
  }

  for (auto &&e : b) {
    std::cin >> e;
  }

  std::cout << dfs(a, b, 0, 0, 0, k) << std::endl;

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

#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll a[200005], b[200005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, k, ans = 0;
  cin >> n >> m >> k;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    a[i] += a[i - 1] + x;
  }
  for (int i = 1, x; i <= m; i++) {
    cin >> x;
    b[i] += b[i - 1] + x;
  }
  int j = m;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    while (a[i] + b[j] > k && j)
      j--;
    if (a[i] + b[j] <= k)
      ans = max(i + j, ans);
  }
  cout << ans;
  // system("pause");
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll a[200005], b[200005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, k, ans = 0;
  cin >> n >> m >> k;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    a[i] += a[i - 1] + x;
  }
  for (int i = 1, x; i <= m; i++) {
    cin >> x;
    b[i] += b[i - 1] + x;
  }
  for (int i = 0; i <= n; i++) {
    int j = m;
    if (a[i] > k)
      break;
    while (a[i] + b[j] > k && j)
      j--;
    if (a[i] + b[j] <= k)
      ans = max(i + j, ans);
  }
  cout << ans;
  //   system("pause");
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define N 100010
#define M 200010
#define endl "\\n"
#define mod 1000000007

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(NULL);

  ll n, m, k;

  cin >> n >> m >> k;

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

  for (int i = 0; i < n; i++) {
    sumA[i + 1] = sumA[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    sumB[i + 1] = sumB[i] + b[i];
  }

  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    ll rem = k - sumA[i];

    if (rem < 0)
      continue;

    ll l = lower_bound(sumB.begin(), sumB.end(), rem) - sumB.begin();

    if (l > m || sumB[l] > rem) {
      l--;
    }

    ans = max(ans, i + l);
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define N 100010
#define M 200010
#define endl "\\n"
#define mod 1000000007

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(NULL);

  int n, m, k, x;
  list<int> a, b;

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

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

  int ans = 0;
  while (true) {
    int x = a.front();
    int y = b.front();

    if (x > k && y > k) {
      break;
    }

    if (x < y) {
      k -= x;
      a.pop_front();
    } else {
      k -= y;
      b.pop_front();
    }

    ans++;

    if (a.size() == 0) {
      while (true) {
        x = b.front();
        if (x > k)
          break;
        ans++;
        k -= x;
        b.pop_front();
      }
      break;
    }

    if (b.size() == 0) {
      while (true) {
        x = a.front();
        if (x > k)
          break;
        ans++;
        k -= x;
        a.pop_front();
      }
      break;
    }

    // cout << x << " " << y << endl;
  }

  cout << ans << endl;

  return 0;
}