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

#include <bits/stdc++.h>
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v) sort(v.begin(), v.end(), greater<ll>())
#define prvsort(v) sort(v.begin(), v.end(), greater<pair<ll, ll>>())
#define ll long long
#define ld long double
#define fixset(val) fixed << setprecision(10) << val
#define rep(i, s, g) for (ll i = (s); i < (g); i++)
#define mod 1000000007
#define vfind(v, target) find(v.begin(), v.end(), target)
#define notFound(v, target) (vfind(v, target) == v.end())

using namespace std;

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v) sort(v.begin(), v.end(), greater<ll>())
#define prvsort(v) sort(v.begin(), v.end(), greater<pair<ll, ll>>())
#define ll long long
#define ld long double
#define fixset(val) fixed << setprecision(10) << val
#define rep(i, s, g) for (ll i = (s); i < (g); i++)
#define mod 1000000007
#define vfind(v, target) find(v.begin(), v.end(), target)
#define notFound(v, target) (vfind(v, target) == v.end())

using namespace std;

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

// g++ .cpp && ./a.out
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef pair<int, int> p;
typedef long long ll;

const int mod = 1000000007;
const int inf = 1000000007;

int main() {
  int n, m;
  ll 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];
  vector<ll> ra(n + 1);
  vector<ll> rb(m + 1);
  ll cnt = 0;
  for (int i = 1; i < n + 1; i++)
    ra[i] = ra[i - 1] + a[i - 1];
  for (int i = 1; i < m + 1; i++)
    rb[i] = rb[i - 1] + b[i - 1];
  /*rep(i, n + 1) cout << ra[i] << ' ';
  cout << endl;
  rep(i, m + 1) cout << rb[i] << ' ';
  cout << endl;*/
  for (int i = 0; i <= n; i++) {
    ll d = k - ra[i];
    if (d < 0)
      continue;
    ll l = 0;
    ll r = m + 1;
    // cout << i << endl;
    // cout << d << endl;
    while (r - l > 1) {
      ll m = (r + l) / 2;
      if (rb[m] > d)
        r = m;
      else
        l = m;
    }
    // cout << l << endl;
    if (cnt < l + i)
      cnt = l + i;
  }
  cout << cnt << endl;
}


Error: 0

Fixed submission:

// g++ .cpp && ./a.out
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef pair<int, int> p;
typedef long long ll;

const int mod = 1000000007;
const int inf = 1000000007;

int main() {
  int n, m;
  ll 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];
  int cnt = 0;
  int aIn = 0;
  int bIn = 0;
  while (k >= a[aIn] || k >= b[bIn]) {
    if (aIn == n && bIn == m)
      break;
    if (a[aIn] >= b[bIn]) {
      if (bIn == m) {
        if (k >= a[aIn]) {
          k -= a[aIn];
          aIn--;
        } else {
          break;
        }
      }
      k -= b[bIn];
      bIn++;
    } else {
      if (aIn == n) {
        if (k >= b[bIn]) {
          k -= b[bIn];
          bIn--;
        } else {
          break;
        }
      }
      k -= a[aIn];
      aIn++;
    }
    cnt++;
  }
  cout << cnt << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A, B;
  rep(i, N) {
    ll x;
    cin >> x;
    A.pb(x);
  }
  rep(i, M) {
    ll x;
    cin >> x;
    B.pb(x);
  }
  vector<ll> SB(M);
  rep(i, M) {
    if (i == 0)
      SB[i] = B[i];
    else {
      SB[i] = SB[i - 1] + B[i];
    }
  }
  ll ans = 0;
  ll s = 0;
  auto pos = lower_bound(ALL(SB), K + 1);
  ll x = pos - SB.begin();
  chmax(ans, x);
  rep(i, N) {
    s += A[i];
    ll k = K - s;
    if (k < 0)
      break;
    auto pos = lower_bound(ALL(SB), k + 1);
    ll x = pos - SB.begin();
    chmax(ans, x + i + 1);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  queue<ll> A, B;
  rep(i, N) {
    ll x;
    cin >> x;
    A.push(x);
  }
  rep(i, M) {
    ll x;
    cin >> x;
    B.push(x);
  }
  int ans = 0;
  rep(k, N + M) {
    ll a = A.front();
    ll b = B.front();
    if (a > b) {
      if (K - b < 0)
        break;
      B.pop();
      K -= b;
      ans++;
    } else {
      if (K - a < 0)
        break;
      A.pop();
      K -= a;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#define ll long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define gmid ((l + r) >> 1)
using namespace std;

const int maxn = 500050;
ll a[maxn], b[maxn], f1[maxn], f2[maxn], n, m, k, f = 0;
int main() {
  cin >> n >> m >> k;
  f1[0] = 0;
  f2[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    f1[i] = f1[i - 1] + a[i];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
    f2[i] = f2[i - 1] + b[i];
  }
  int j = m;
  ll ans = 0;
  for (int i = 0; i <= n; ++i) {
    while (f1[i] + f2[j] > k && j >= 0)
      j--;
    // cout<<i<<" "<<j<<" "<<f1[i]<<" "<<f2[j]<<endl;
    if (j >= 0 && f1[i] + f2[j] <= k) {
      if (i + j > ans)
        ans = i + j;
    }
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#define ll long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define gmid ((l + r) >> 1)
using namespace std;

const int maxn = 200050;
ll a[maxn], b[maxn], n, m, k;
int main() {
  scanf("%lld %lld %lld", &n, &m, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", a[i]);
  for (int i = 1; i <= m; ++i)
    scanf("%lld", b[i]);
  int i = 1, j = 1;
  ll ans = 0;
  while (k > 0) {
    // cout<<i<<" "<<j<<endl;
    if (j > m && i > n)
      break;
    if (j > m) {
      if (a[i] > k)
        break;
      else {
        ans++;
        k -= a[i];
        i++;
      }
    } else if (i > n) {
      if (b[j] > k)
        break;
      else {
        ans++;
        k -= b[j];
        j++;
      }
    } else {
      int t = 0;
      if (a[i] < b[j])
        t = a[i];
      else
        t = b[j];
      if (t > k)
        break;
      else {
        ans++;
        k -= t;
        if (t == a[i])
          i++;
        else
          j++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0), b(m + 1, 0);
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    a.at(i) = a.at(i - 1) + c;
  }
  for (int i = 1; i <= m; i++) {
    int d;
    cin >> d;
    b.at(i) = b.at(i - 1) + d;
  }

  int jbest = 0;
  bool judge = true;
  while (judge) {
    if (k >= b.at(jbest + 1)) {
      jbest++;
    } else {
      judge = false;
    }
    if (jbest == m) {
      judge = false;
    }
  }
  // cout << jbest << endl;

  int ans = jbest;
  for (int i = 1; i <= n; i++) {
    if (a.at(i) > k) {
      break;
    }
    int j = jbest;
    while (b.at(j) > k - a.at(i)) {
      j--;
    }
    ans = max(ans, i + j);
    jbest = j;
  }

  /*for(int j = jbest; j >= 0; j--) {
    for(int i = 1; i <= n; i++) {
      if(a.at(i) <= k - b.at(j) && ans < i + j) {
        ans = i + j;
      }
      if(a.at(i) > k - b.at(j)) {
        break;
      }
    }
  }*/

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

Error: TLE

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0), b(m + 1, 0);
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    a.at(i) = a.at(i - 1) + c;
  }
  for (int i = 1; i <= m; i++) {
    int d;
    cin >> d;
    b.at(i) = b.at(i - 1) + d;
  }

  int jbest = 0;
  bool judge = true;
  while (judge) {
    if (k >= b.at(jbest + 1)) {
      jbest++;
    } else {
      judge = false;
    }
    if (jbest == m) {
      judge = false;
    }
  }
  // cout << jbest << endl;

  int ans = jbest;
  for (int i = 1; i <= n; i++) {
    if (a.at(i) > k) {
      break;
    }
    int j = jbest;
    while (b.at(j) > k - a.at(i)) {
      j--;
    }
    ans = max(ans, i + j);
  }

  /*for(int j = jbest; j >= 0; j--) {
    for(int i = 1; i <= n; i++) {
      if(a.at(i) <= k - b.at(j) && ans < i + j) {
        ans = i + j;
      }
      if(a.at(i) > k - b.at(j)) {
        break;
      }
    }
  }*/

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

typedef long long ll;

int main(int argc, char *argv[]) {
  ll n, m, k;
  std::vector<ll> a, b;

  // 入力
  std::cin >> n >> m >> k;

  a.resize(n);
  b.resize(m);

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

  // pre
  std::vector<ll> aSum(n + 1), bSum(m + 1);

  aSum[0] = 0;
  bSum[0] = 0;

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

  int aPtr, bPtr;
  for (aPtr = 0; aPtr < n; aPtr++) {
    if (aSum[aPtr + 1] > k) {
      break;
    }
  }
  for (bPtr = 0; bPtr < m; bPtr++) {
    if (aSum[aPtr] + bSum[bPtr + 1] > k) {
      break;
    }
  }

  // solve
  int max = aPtr + bPtr;
  for (; aPtr >= 0; aPtr--) {
    for (; bPtr < m; bPtr++) {
      if (aSum[aPtr] + bSum[bPtr + 1] > k) {
        break;
      }
    }
    max = std::max(max, aPtr + bPtr);
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

typedef long long ll;

int main(int argc, char *argv[]) {
  ll n, m, k;
  std::vector<ll> a, b;

  // 入力
  std::cin >> n >> m >> k;

  a.resize(n);
  b.resize(m);

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

  // pre
  std::vector<ll> aSum(n + 1), bSum(m + 1);

  aSum[0] = 0;
  bSum[0] = 0;

  for (int i = 0; i < n; i++) {
    aSum[i + 1] = aSum[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    bSum[i + 1] = bSum[i] + a[i];
  }

  int aPtr, bPtr;
  for (aPtr = 0; aPtr < n; aPtr++) {
    if (aSum[aPtr + 1] > k) {
      break;
    }
  }
  for (bPtr = 0; bPtr < m; bPtr++) {
    if (aSum[aPtr] + bSum[bPtr + 1] > k) {
      break;
    }
  }

  // solve
  int max = aPtr + bPtr;
  for (; aPtr >= 0; aPtr--) {
    for (; bPtr < m; bPtr++) {
      if (aSum[aPtr] + bSum[bPtr + 1] > k) {
        break;
      }
    }
    max = std::max(max, aPtr + bPtr);
  }

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

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

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)

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

Error: -11

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)

int main() {
  long long int n, m, k, a, b;
  int x, y;
  cin >> n >> m >> k;
  vector<long long int> aa(n + 1);
  vector<long long int> bb(m + 1);
  rep(i, n) {
    cin >> a;
    aa[i + 1] = aa[i] + a;
    if (aa[i] > k)
      x = i - 1;
  }
  rep(i, m) {
    cin >> b;
    bb[i + 1] = bb[i] + b;
    if (bb[i] > k)
      y = i - 1;
  }
  int ans = 0;
  rep(i, x) {
    rep(j, y) {
      if (aa[i] + bb[j] > k)
        break;
      else
        ans = max(ans, i + j);
    }
  }
  cout << ans;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

int N, M;
ll K;
vector<ll> A, B;
void input() {
  cin >> N >> M >> K;
  for (int i = 0; i < N; ++i) {
    ll a;
    cin >> a;
    A.emplace_back(a);
  }
  for (int i = 0; i < M; ++i) {
    ll a;
    cin >> a;
    B.emplace_back(a);
  }
}

void solve() {
  vector<ll> sumA, sumB;
  sumA.emplace_back(0);
  sumB.emplace_back(0);
  ll sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += A[i];
    sumA.emplace_back(sum);
  }
  sum = 0;
  for (int i = 0; i < M; ++i) {
    sum += B[i];
    sumB.emplace_back(sum);
  }
  // cout<<sumA[N-1]<<" "<<sumB[M]<<" "<<"\\n";
  int ans = 0;
  int maxj = M;
  for (int i = 0; i < N + 1; ++i) {
    if (sumA[i] > K)
      break;
    for (int j = maxj; j >= 0; --j) {
      if (sumA[i] + sumB[j] > K)
        continue;
      ans = max(i + j, ans);
      maxj = j;
      break;
      // cout<<sumA[i]<<" "<<sumB[j]<<" "<<i+j<<"\\n";
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie();
  ios::sync_with_stdio(false);
  input();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int N, M;
ll K;
vector<ll> A, B;
void input() {
  cin >> N >> M >> K;
  for (int i = 0; i < N; ++i) {
    ll a;
    cin >> a;
    A.emplace_back(a);
  }
  for (int i = 0; i < M; ++i) {
    ll a;
    cin >> a;
    B.emplace_back(a);
  }
}

void solve() {
  vector<ll> sumA, sumB;
  sumA.emplace_back(0);
  sumB.emplace_back(0);
  ll sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += A[i];
    sumA.emplace_back(sum);
  }
  sum = 0;
  for (int i = 0; i < M; ++i) {
    sum += B[i];
    sumB.emplace_back(sum);
  }
  // cout<<sumA[N-1]<<" "<<sumB[M]<<" "<<"\\n";
  int ans = 0;
  for (int i = 0; i < N + 1; ++i) {
    if (sumA[i] > K)
      break;
    for (int j = 0; j < M + 1; ++j) {
      if (sumA[i] + sumB[j] > K)
        break;
      ans = max(i + j, ans);
      // cout<<sumA[i]<<" "<<sumB[j]<<" "<<i+j<<"\\n";
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie();
  ios::sync_with_stdio(false);
  input();
  solve();
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  vector<ll> acc1;
  {
    ll sum = 0ll;
    acc1.push_back(0ll);
    for (int i = 0; i < n; i++) {
      sum += a[i];
      acc1.push_back(sum);
    }
  }
  vector<ll> acc2;
  {
    ll sum = 0ll;
    acc2.push_back(0ll);
    for (int i = 0; i < m; i++) {
      sum += b[i];
      acc2.push_back(sum);
    }
  }

  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll left = k - acc1[i];
    if (left < 0) {
      break;
    }
    auto it = upper_bound(acc2.begin(), acc2.end(), left);
    ll sum = i;
    if (it != acc2.begin()) {
      sum += distance(acc2.begin(), it) - 1;
    }
    ans = max(ans, sum);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];

  vector<ll> acc1;
  {
    ll sum = 0ll;
    acc1.push_back(0ll);
    for (int i = 0; i < n; i++) {
      sum += a[i];
      acc1.push_back(sum);
    }
  }
  vector<ll> acc2;
  {
    ll sum = 0ll;
    acc2.push_back(0ll);
    for (int i = 0; i < m; i++) {
      sum += b[i];
      acc2.push_back(sum);
    }
  }

  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll left = k - acc1[i];
    if (left < 0) {
      break;
    }
    auto it = upper_bound(acc2.begin(), acc2.end(), left);
    ll sum = i;
    if (it != acc2.begin()) {
      sum += distance(acc2.begin(), it) - 1;
    }
    ans = max(ans, sum);
  }

  cout << ans << endl;

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

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

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

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> B(M);
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }
  int64_t sum = 0;
  int64_t hon = 0;
  int64_t akaz = -1;
  int64_t bkaz = -1;

  for (int i = 0; i < N; i++) {
    if (sum + A.at(i) > K)
      break;
    else {
      sum += A.at(i);
      hon++;
      akaz++;
    }
  }
  for (int i = 0; i < M; i++) {
    if (sum + B.at(i) > K)
      break;
    else {
      sum += B.at(i);
      hon++;
      bkaz++;
    }
  }
  if (hon == N + M) {
    cout << hon << endl;
    return 0;
  }
  if (akaz == -1 && bkaz == -1) {
    cout << 0 << endl;
    return 0;
  }
  int64_t honm = hon;
  for (int i = akaz; i >= 0; i--) {
    sum -= A.at(akaz);
    akaz--;
    hon--;
    if (bkaz == M - 1)
      break;
    while (bkaz <= M - 2 && B.at(bkaz + 1) <= K - sum) {
      sum += B.at(bkaz + 1);
      bkaz++;
      hon++;
      honm = max(hon, honm);
      if (bkaz == M - 1)
        break;
    }
  }
  cout << honm << endl;
}

Error: 0

Fixed submission:

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

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

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> B(M);
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }
  int64_t sum = 0;
  int64_t hon = 0;
  int64_t akaz = -1;
  int64_t bkaz = -1;

  for (int i = 0; i < N; i++) {
    if (sum + A.at(i) > K)
      break;
    else {
      sum += A.at(i);
      hon++;
      akaz++;
    }
  }
  for (int i = 0; i < M; i++) {
    if (sum + B.at(i) > K)
      break;
    else {
      sum += B.at(i);
      hon++;
      bkaz++;
    }
  }
  if (hon == N + M) {
    cout << hon << endl;
    return 0;
  }
  int64_t honm = hon;
  for (int i = akaz; i >= 0; i--) {
    sum -= A.at(akaz);
    akaz--;
    hon--;
    while (B.at(bkaz + 1) <= K - sum) {
      sum += B.at(bkaz + 1);
      hon++;
      honm = max(hon, honm);
      if (bkaz = M - 1)
        break;
    }
  }
  cout << honm << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  vector<ll> psa(n + 1, 0);
  vector<ll> psb(m + 1, 0);
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    psa[i] = psa[i - 1] + a[i];
  }
  for (ll i = 1; i <= m; i++) {
    cin >> b[i];
    psb[i] = psb[i - 1] + b[i];
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll x = k - psa[i];
    if (x < 0) {
      break;
    }
    ll low = 1, high = m, y = 0;
    while (low <= high) {
      ll mid = (low + high) / 2;
      if (psb[mid] <= x) {
        y = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    ans = max(ans, i + y);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int totalBooksRead = 0;
  vector<int> a(n);
  vector<int> b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int i = 0;
  int j = 0;
  while (true) {
    if ((i == n && j == m) || (k <= 0)) {
      break;
    }
    if (i < n && j < m) {
      if (a[i] < b[j]) {
        if (k - a[i] >= 0) {
          totalBooksRead++;
          k = k - a[i++];
        }
      } else {
        if (k - b[j] >= 0) {
          totalBooksRead++;
          k = k - b[j++];
        }
      }
    } else if (i < n) {
      if (k - a[i] >= 0) {
        totalBooksRead++;
        k = k - a[i++];
      }
    } else {
      if (k - b[j] >= 0) {
        totalBooksRead++;
        k = k - b[j++];
      }
    }
  }
  cout << totalBooksRead << endl;
}