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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define gcd(a, b) __gcd(a, b)
#define ff first
#define ss second
#define p pair<int, int>
#define pb push_back
#define endl '\\n'
#define w(t)                                                                   \\
  ll test;                                                                     \\
  cin >> test;                                                                 \\
  while (test--)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);
#define pi acos(-1)

int main() {
  fast;
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> v1(n), v2(m);
  vector<ll> sumv1(n + 1), sumv2(m + 1);
  for (ll i = 0; i < n; i++) {
    cin >> v1[i];
    i ? v1[i] += v1[i - 1] : v1[i] += 0;
    sumv1[i + 1] = v1[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> v2[i];
    i ? v2[i] += v2[i - 1] : v2[i] += 0;
    sumv2[i + 1] = v2[i];
  }
  ll ans = 0, j = m;
  for (ll i = 0; i < n + 1; i++) {
    if (sumv1[i] > k)
      break;
    //  int j=0;
    while (sumv2[j] > k - sumv1[i]) {
      j--;
    }
    // cout<<i<<" "<<j<<" ";
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define gcd(a, b) __gcd(a, b)
#define ff first
#define ss second
#define p pair<int, int>
#define pb push_back
#define endl '\\n'
#define w(t)                                                                   \\
  ll test;                                                                     \\
  cin >> test;                                                                 \\
  while (test--)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);
#define pi acos(-1)

int main() {
  fast;
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> v1(n), v2(m);

  for (int i = 0; i < n; i++) {
    cin >> v1[i];
    i ? v1[i] += v1[i - 1] : v1[i] += 0;
  }
  for (int i = 0; i < m; i++) {
    cin >> v2[i];
    i ? v2[i] += v2[i - 1] : v2[i] += 0;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (v1[i] > k)
      break;
    int j = 0;
    while (v2[j] < k - v1[i]) {
      j++;
    }
    ans = max(ans, i + j + 1);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int64_t> a(n + 1);
  vector<int64_t> b(m + 1);

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

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

  int ans = 0;

  for (int i = 0, j = m; i <= n; 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: WA

Fixed submission:

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

int main() {
  int n, m;
  int64_t k;
  cin >> n >> m >> k;
  queue<int64_t> a;
  queue<int64_t> b;

  int i = 0;
  while (i < n) {
    int64_t tmp;
    cin >> tmp;
    a.push(tmp);
    i++;
  }

  int j = 0;
  while (j < m) {
    int64_t tmp;
    cin >> tmp;
    b.push(tmp);
    j++;
  }

  int64_t sum = 0;
  int count = 0;
  while (sum < k + 1) {
    if (b.empty() && !a.empty()) {
      sum += a.front();
      count += 1;
      a.pop();
    }

    if (a.empty() && !b.empty()) {
      sum += b.front();
      count += 1;
      b.pop();
    }

    if (a.front() <= b.front() || b.empty()) {
      sum += a.front();
      count += 1;
      a.pop();
    }
    if (a.front() > b.front() || a.empty()) {
      sum += b.front();
      count += 1;
      b.pop();
    }

    if (count == n + m && sum <= k) {
      count++;
      break;
    }
  }

  cout << count - 1 << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

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

  cin >> N >> M >> K;

  int64_t A[N];
  int64_t B[M];

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

  vector<int64_t> sumA(N + 1, 0), sumB(M + 1, 0);
  for (int i = 1; i <= N; i++)
    sumA[i] = sumA[i - 1] + A[i - 1];
  for (int i = 1; i <= M; i++)
    sumB[i] = sumB[i - 1] + B[i - 1];

  /*
      for ( int i = 0; i < N+1; i ++ ) cout << sumA[i] << " ";
      cout << endl;
      for ( int i = 0; i < M+1; i ++ ) cout << sumB[i] << " ";
      cout << endl;
  */

  int maxcount = 0;
  int n = N;
  int m = 0;
  for (; n >= 0; n--) {
    if (sumA[n] > K)
      continue;

    //        cout << "test A " << n << " sum:" <<  sumA[n] << " remain " <<
    //        K-sumA[n] << endl;

    for (; m <= M; m++) {
      //           cout << "  test B " << m << " sumB " << sumB[m] << endl;
      if (sumB[m] > K - sumA[n]) {
        break;
      }
    }
    //        cout << "  b-> " << m << endl;
    m--;

    //       cout << "   count " << n+m << endl;
    maxcount = max(maxcount, n + m);
  }

  cout << maxcount << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int N, M, K;
int *A;
int *B;
int maxcount = 0;

bool dfs(int a, int b, int sum, int count) {
  //    cout << "a, b " << a << " " << b << " " << "sum:" << sum << " count:" <<
  //    count << endl;
  if (sum == K) {
    if (maxcount < count)
      maxcount = count;
    return false;
  }

  if (sum > K)
    return false;
  if (a == N && b == M) {
    maxcount = N + M;
    return false;
  }

  if (a < N)
    if (!dfs(a + 1, b, sum + A[a], count + 1)) {
      if (maxcount < count)
        maxcount = count;
    }
  if (b < M)
    if (!dfs(a, b + 1, sum + B[b], count + 1)) {
      if (maxcount < count)
        maxcount = count;
    }

  return true;
}

int main() {
  cin >> N >> M >> K;
  A = new int[N];
  B = new int[M];

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

  dfs(0, 0, 0, 0);
  cout << maxcount << endl;

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

#include <bits/stdc++.h>
using namespace std;
int a[200010], b[200010];
long long sa[200010], sb[200010];
int n, m, k, ans = 0;
int Binary_search(int sum) {
  int l = 0, r = m, mid;
  while (l < r) {
    mid = (l + r + 1) / 2; //+1取决于mid-1
    if (sb[mid] + sum > k) {
      r = mid - 1;
    } else {
      l = mid;
    }
  }
  return l;
}
int main() {
  cin >> n >> m >> k;
  sa[0] = sb[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sa[i] = sa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    sb[i] = sb[i - 1] + b[i];
  }
  for (int i = 0; i <= n; i++) {
    if (sa[i] <= k) {
      ans = max(ans, i + Binary_search(sa[i]));
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[200010], b[200010];
int n, m, k, ans = 0;
void dfs(int numa, int numb, int sum, int cnt) {
  if (sum > k) {
    cnt--;
    ans = max(cnt, ans);
    return;
  }
  if (numa == n && numb == m) {
    if (sum > k) {
      cnt--;
    }
    ans = max(cnt, ans);
    return;
  }
  if (numa + 1 <= n)
    dfs(numa + 1, numb, sum + a[numa], cnt + 1);
  if (numb + 1 <= m)
    dfs(numa, numb + 1, sum + b[numb], cnt + 1);
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  dfs(0, 0, 0, 0);
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N + 1, 0), B(M + 1, 0);
  int i, ab;
  for (i = 0; i < N; i++) {
    cin >> ab;
    A[i + 1] = A[i] + ab;
  }
  for (i = 0; i < M; i++) {
    cin >> ab;
    B[i + 1] = B[i] + ab;
  }
  int ans = 0;
  for (i = 0; i <= N; i++) {
    if (A[i] > K)
      break;
    int l = 0, r = M + 1, m;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (A[i] + B[m] > K)
        r = m;
      else
        l = m;
    }
    ans = max(ans, i + l);
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N + 1, 0), B(N + 1, 0);
  int i, ab;
  for (i = 0; i < N; i++) {
    cin >> ab;
    A[i + 1] = A[i] + ab;
  }
  for (i = 0; i < M; i++) {
    cin >> ab;
    B[i + 1] = B[i] + ab;
  }
  int ans = 0;
  for (i = 0; i < 0; i++) {
    int l = 0, r = M, m;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (A[i] + B[m] > K)
        r = m;
      else
        l = m;
    }
    ans = max(ans, i + l);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<ll> a(n + 1);
  vector<ll> b(m + 1);

  rep(i, n + 1) {
    if (i == 0)
      continue;
    ll x;
    cin >> x;
    a[i] = x + a[i - 1];
  }

  rep(i, m + 1) {
    if (i == 0)
      continue;
    ll x;
    cin >> x;
    b[i] = x + b[i - 1];
  }
  ll ans = 0, best = m;
  rep(i, n + 1) {
    if (a[i] > k)
      break;
    for (ll j = best; j >= 0; j--) {
      if (k >= a[i] + b[j]) {
        best = j;
        ll x = i + j;
        if (ans < x)
          ans = x;
        break;
      }
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

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

  vector<ll> a(n);
  vector<ll> b(m);

  rep(i, n) {
    ll x;
    cin >> x;
    if (i == 0)
      a[i] = x;
    else
      a[i] = x + a[i - 1];
  }

  rep(i, m) {
    ll x;
    cin >> x;
    if (i == 0)
      b[i] = x;
    else
      b[i] = x + b[i - 1];
  }
  ll ans = 0;
  if (k >= a[n - 1] + b[n - 1]) {
    cout << n + m << endl;
    return 0;
  }
  rep(i, n) rep(j, m) {
    if (a[i] >= k)
      break;
    if (k >= a[i] + b[j]) {
      ll x = i + j + 2;
      if (ans < x) {
        ans = x;
      }
    }
  }

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

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

int main() {
  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> v1(n);
  vector<int64_t> v2(m);
  rep(i, n) { cin >> v1[i]; }
  rep(i, m) { cin >> v2[i]; }
  int64_t ans = 0;
  vector<int64_t> tot1(n + 1);
  vector<int64_t> tot2(m + 1);
  tot1[0] = 0;
  tot2[0] = 0;
  rep(i, n) { tot1[i + 1] = v1[i] + tot1[i]; }
  rep(i, m) { tot2[i + 1] = v2[i] + tot2[i]; }
  rep(i, n + 1) {
    int64_t x = k - tot1[i];
    if (x < 0)
      break;
    if (x == 0) {
      ans = max(ans, i);
      break;
    }
    int ok = -1;
    int ng = m + 1;

    while (ok + 1 != ng) {
      int mid = (ok + ng) / 2;
      if (tot2[mid] > x)
        ng = mid;
      else
        ok = mid;
      if (ok + 1 == ng) {
        ans = max(ans, i + ok);
        break;
      }
    }
  }
  cout << ans << endl;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

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

int main() {
  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> v1(n);
  vector<int64_t> v2(m);
  rep(i, n) { cin >> v1[i]; }
  rep(i, m) { cin >> v2[i]; }
  int64_t ans = 0;
  vector<int64_t> tot1(n);
  vector<int64_t> tot2(m);
  rep(i, n + 1) {
    if (i != 0)
      tot1[i] = v1[i] + tot1[i - 1];
    else
      tot1[i] = v1[i];
  }
  rep(i, m + 1) {
    if (i != 0)
      tot2[i] = v2[i] + tot2[i - 1];
    else
      tot2[i] = v2[i];
  }
  rep(i, n) {
    int64_t x = k - tot1[i];
    if (x < 0)
      break;
    if (x == 0) {
      ans = max(ans, i + 1);
      break;
    }
    int64_t begin = 0;
    int64_t end = m - 1;
    while (end != -1) {
      int64_t mid = (begin + end) / 2;
      if (tot2[m - 1] <= x) {
        ans = max(ans, m + i + 1);
      }
      if (tot2[mid] > x) {
        end = mid;
      } else {
        begin = mid;
      }
      if (begin + 1 == end) {
        if (tot2[end] <= x) {
          begin = end;
        } else
          end = begin;
      }
      if (begin == end) {
        ans = max(ans, i + begin + 2);
        end = -1;
      }
    }
  }
  rep(i, m) {
    if (tot2[i] <= k) {
      ans = max(ans, i + 1);
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[200005], ai[200005], b[200005], bi[200005], n, m, k;
int main() {
  cin >> n >> m >> k;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
    ai[i] = ai[i - 1] + a[i];
  }
  for (long long i = 1; i <= m; ++i) {
    cin >> b[i];
    bi[i] = bi[i - 1] + b[i];
  }
  long long ans = 0, j;
  for (long long i = 0; i <= n; ++i) {
    j = 0;
    if (ai[i] > k)
      break;
    while (1) {
      if (bi[j] > k - ai[i] || j > m)
        break;
      j++;
    }
    ans = max(ans, i + j - 1);
  }
  cout << ans;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int 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];
  }
  ll t = 0;
  rep(i, m) t += b[i];
  int j = m;
  int ans = 0;
  rep(i, n + 1) {
    while (j > 0 && t > k) {
      --j;
      t -= b[j];
    }
    if (t > k)
      break;
    ans = max(ans, i + j);
    if (i == n)
      break;
    t += a[i];
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, c) for (long long i = 0; i < (long long)c; i++)
const ll INF = 1000000000000001;
// 146813779479510
// 1000000000000001
int main() {
  int n, m, k, a1, b1;
  int ans = 0;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a1;
    a[i] = a[i - 1] + a1;
    /*		if (a[i]>k){
                            n=i-1;
                            break;

                    }*/
  }
  for (int i = 1; i <= m; i++) {
    cin >> b1;
    b[i] = b[i - 1] + b1;
    /*		if (b[i]>k){
                            m=i-1;
                            break;
                    }*/
  }
  for (int i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    for (int j = 0; j <= m && b[j] <= k; j++) {
      if (a[i] + b[j] > k)
        break;
      ans = max(ans, i + j);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
typedef long long int ll;
typedef long double ld;

#define MAX 1000005
#define mod 1000000007

ll add(ll a, ll b) { return (mod + (a + b) % mod) % mod; }
ll mul(ll a, ll b) { return (mod + (a * b) % mod) % mod; }

ll n, m;

unordered_map<ll, vector<ll>> mp;

int main() {

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

  ll n, m, k;
  cin >> n >> m >> k;

  vector<ll> A(n + 1), B(m + 1);

  ll sum0 = 0, sum1 = 0;

  for (ll i = 0; i < n; i++)
    cin >> A[i], sum0 += A[i];
  for (ll i = 0; i < m; i++)
    cin >> B[i];
  ll last = n - 1;
  ll ans = 0;
  for (ll i = 0; i <= m; i++) {
    while (sum0 + sum1 > k && last >= 0) {
      sum0 -= A[last];
      last--;
    }
    if (sum0 + sum1 <= k) {
      ans = max(ans, i + last + 1);
    }

    if (i != m)
      sum1 += B[i];
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
typedef long long int ll;
typedef long double ld;

#define MAX 1000005
#define mod 1000000007

ll add(ll a, ll b) { return (mod + (a + b) % mod) % mod; }
ll mul(ll a, ll b) { return (mod + (a * b) % mod) % mod; }

ll n, m;

unordered_map<ll, vector<ll>> mp;

int main() {

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

  ll n, m, k;
  cin >> n >> m >> k;

  vector<ll> A(n + 1), B(n + 1);

  ll sum0 = 0, sum1 = 0;

  for (ll i = 0; i < n; i++)
    cin >> A[i], sum0 += A[i];
  for (ll i = 0; i < m; i++)
    cin >> B[i];
  ll last = n - 1;
  ll ans = 0;
  for (ll i = 0; i <= m; i++) {
    while (sum0 + sum1 > k && last >= 0) {
      sum0 -= A[last];
      last--;
    }
    if (sum0 + sum1 <= k) {
      ans = max(ans, i + last + 1);
    }

    if (i != m)
      sum1 += B[i];
  }

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

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m);
  rep(i, n) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  rep(i, m) {
    cin >> b[i];
    if (i >= 1)
      b[i] += b[i - 1];
  }

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

Error: TLE

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m);
  rep(i, n) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  rep(i, m) {
    cin >> b[i];
    if (i >= 1)
      b[i] += b[i - 1];
  }

  int ans = 0;
  rep(i, n + 1) {
    if (a[i] > k)
      break;
    int b_r = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin();
    ans = max(ans, i + b_r);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;

int N, M, K;
int Asum[200200];
int Bsum[200200];

signed main() {
  cin >> N >> M >> K;
  rep(i, N) {
    int a;
    cin >> a;
    Asum[i + 1] = Asum[i] + a;
  }
  rep(i, M) {
    int b;
    cin >> b;
    Bsum[i + 1] = Bsum[i] + b;
  }
  int ans = 0;
  for (int a = 0; a <= N; ++a) {
    if (Asum[a] > K)
      break;
    int b = upper_bound(Bsum, Bsum + M + 1, K - Asum[a]) - Bsum - 1;
    ans = max(ans, a + b);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m);
  rep(i, n) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  rep(i, m) {
    cin >> b[i];
    if (i >= 1)
      b[i] += b[i - 1];
  }

  ll ans = 0;
  rep(i, n + 1) {
    ll rem = k - a[i];
    if (rem < 0)
      break;
    auto iter_b = upper_bound(b.begin(), b.end(), rem);
    ll b_read = iter_b - b.begin();
    ll read = i + b_read;
    ans = max(ans, read);
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

/* a と b の最大公約数を返す関数 */
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

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

  vector<ll> Ai(N + 1, 0);
  vector<ll> Bi(M + 1, 0);
  ll t;

  ll sa = 0;
  ll sb = 0;
  for (ll i = 1; i <= N; ++i) {
    cin >> t;
    Ai.at(i) = t + Ai.at(i - 1);
    if (Ai.at(i) <= K) {
      sa++;
    }
  }
  for (ll i = 1; i <= M; ++i) {
    cin >> t;
    Bi.at(i) = t + Bi.at(i - 1);
    if (Bi.at(i) <= K) {
      sb++;
    }
  }

  ll read_cnt = 0;
  ll last_j = sb;
  for (ll i = 0; i <= sa; ++i) {
    for (ll j = last_j; j >= 0; --j) {
      if ((Ai.at(i) + Bi.at(j)) <= K) {
        read_cnt = max(read_cnt, (i + j));
        last_j = j;
        break;
      }
    }
  }

  cout << read_cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

/* a と b の最大公約数を返す関数 */
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

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

  vector<ll> Ai(N + 1, 0);
  vector<ll> Bi(M + 1, 0);
  ll t;

  ll sa = 0;
  ll sb = 0;
  for (ll i = 1; i <= N; ++i) {
    cin >> t;
    Ai.at(i) = t + Ai.at(i - 1);
    if (Ai.at(i) <= K) {
      sa++;
    }
  }
  for (ll i = 1; i <= M; ++i) {
    cin >> t;
    Bi.at(i) = t + Bi.at(i - 1);
    if (Bi.at(i) <= K) {
      sb++;
    }
  }

  ll a = 0;
  ll b = 0;
  ll read_cnt = 0;
  for (ll i = 0; i <= sa; ++i) {
    for (ll j = 0; j <= sb; ++j) {
      if ((Ai.at(i) + Bi.at(j)) <= K) {
        read_cnt = max(read_cnt, (i + j));
      } else {
        break;
      }
    }
  }

  cout << read_cnt << endl;
  return 0;
}