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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define SP << " "
#define LLi long long int

using namespace std;

int imax = 2147483647;
long long int llimax = 9223372036854775807;

// int型vectorを出力
void PV(vector<int> pvv) {
  rep(i, pvv.size()) cout << pvv[i] SP;
  cout << endl;
}

// LLi型vectorを出力
void PVL(vector<LLi> pvv) {
  rep(i, pvv.size()) cout << pvv[i] SP;
  cout << endl;
}

int main() {

  LLi n, m, k, ra = 0, rb = 0, rt = 0, ma = 0;
  vector<LLi> a, b;

  cin >> n >> m >> k;
  a.resize(n);
  b.resize(m);

  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  rep(i, n) {
    if (rt + a[i] <= k) {
      rt += a[i];
      ra++;
    } else {
      break;
    }
  }

  rep(i, m) {
    if (rt + b[i] <= k) {
      rt += b[i];
      rb++;
    } else {
      break;
    }
  }

  ma = ra + rb;

  while (1) {
    if (ra == 0)
      break;
    ra--;
    rt -= a[ra];

    while (1) {
      if (rb == m)
        break;
      if (rt + b[rb] <= k) {
        rt += b[rb];
        rb++;
      } else {
        break;
      }
    }

    ma = max(ma, ra + rb);
  }

  cout << ma << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define SP << " "
#define LLi long long int

using namespace std;

int imax = 2147483647;
long long int llimax = 9223372036854775807;

// int型vectorを出力
void PV(vector<int> pvv) {
  rep(i, pvv.size()) cout << pvv[i] SP;
  cout << endl;
}

// LLi型vectorを出力
void PVL(vector<LLi> pvv) {
  rep(i, pvv.size()) cout << pvv[i] SP;
  cout << endl;
}

int main() {

  int n, m, k, ra = 0, rb = 0, rt = 0, ma = 0;
  vector<int> a, b;

  cin >> n >> m >> k;
  a.resize(n);
  b.resize(n);

  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  rep(i, n) {
    if (rt + a[i] <= k) {
      rt += a[i];
      ra++;
    } else {
      break;
    }
  }

  rep(i, m) {
    if (rt + b[i] <= k) {
      rt += b[i];
      rb++;
    } else {
      break;
    }
  }

  ma = ra + rb;

  while (1) {
    if (ra == 0)
      break;
    ra--;
    rt -= a[ra];

    while (1) {
      if (rb == m)
        break;
      if (rt + b[rb] <= k) {
        rt += b[rb];
        rb++;
      } else {
        break;
      }
    }

    ma = max(ma, ra + rb);
  }

  cout << ma << endl;

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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define vv(T) std::vector<std::vector<T>>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;

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

  vector<ll> A(n + 1);
  vector<ll> B(m + 1);
  for (int i = 1; i < n + 1; ++i)
    cin >> A[i];
  for (int i = 1; i < m + 1; ++i)
    cin >> B[i];

  vector<ll> AA(n + 1);
  vector<ll> BB(m + 1);
  AA[0] = A[0];
  for (int i = 1; i < n + 1; ++i)
    AA[i] = AA[i - 1] + A[i];
  BB[0] = B[0];
  for (int i = 1; i < m + 1; ++i)
    BB[i] = BB[i - 1] + B[i];

  ll ans = 0, j;
  for (j = 0; j < m + 1; ++j) {
    if (BB[j] > k)
      break;
  }
  --j;
  for (ll i = 0; i < n + 1; ++i) {
    if (AA[i] > k)
      break;
    while (BB[j] > k - AA[i])
      --j;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define vv(T) std::vector<std::vector<T>>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;

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

  vector<ll> A(n + 1);
  vector<ll> B(m + 1);
  for (int i = 1; i < n + 1; ++i)
    cin >> A[i];
  for (int i = 1; i < m + 1; ++i)
    cin >> B[i];

  vector<ll> AA(n + 1);
  vector<ll> BB(m + 1);
  AA[0] = A[0];
  for (int i = 1; i < n + 1; ++i)
    AA[i] = AA[i - 1] + A[i];
  BB[0] = B[0];
  for (int i = 1; i < m + 1; ++i)
    BB[i] = BB[i - 1] + B[i];

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

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

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

  vector<long long int> A(N);
  vector<long long int> B(M);
  rep(i, N) { cin >> A.at(i); }
  rep(i, M) { cin >> B.at(i); }

  /* Main Logic */
  vector<long long int> sumA(N + 1);
  vector<long long int> sumB(M + 1);
  rep(i, N + 1) {
    if (i == 0)
      sumA.at(i) = 0;
    else
      sumA.at(i) = sumA.at(i - 1) + A.at(i - 1);
  }
  rep(i, M + 1) {
    if (i == 0)
      sumB.at(i) = 0;
    else
      sumB.at(i) = sumB.at(i - 1) + B.at(i - 1);
  }

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

  /* Output */
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  /* Input */
  int N, M, K;
  cin >> N >> M >> K;

  vector<int> A(N);
  vector<int> B(M);
  rep(i, N) { cin >> A.at(i); }
  rep(i, M) { cin >> B.at(i); }

  /* Main Logic */
  vector<int> sumA(N + 1);
  vector<int> sumB(M + 1);
  rep(i, N + 1) {
    if (i == 0)
      sumA.at(i) = 0;
    else
      sumA.at(i) = sumA.at(i - 1) + A.at(i - 1);
  }
  rep(i, M + 1) {
    if (i == 0)
      sumB.at(i) = 0;
    else
      sumB.at(i) = sumB.at(i - 1) + B.at(i - 1);
  }

  int ans = 0;
  int j = M;
  for (int i = 0; i < N; i++) {
    while ((sumA.at(i) + sumB.at(j)) > K) {
      j--;
    }
    ans = max(ans, i + j);
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
#define f first
#define s second
#define pb push_back
#define ENDL '\\n'
#define all(a) begin(a), end(a)
#define sz(a) (int)(a.size())
#define deb(a) cout << #a << " = " << a << ENDL;
#define fore(i, a, b) for (int i(a), ThkMk(b); i < ThkMk; ++i)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> ii;

int main() {
  _ lli n, m, k;
  cin >> n >> m >> k;
  vector<lli> a(n);
  cin >> a[0];
  fore(i, 1, n) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  vector<lli> b(m);
  cin >> b[0];
  fore(i, 1, m) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  lli cnt = 0;
  lli cur = 0;
  int ans = 0;
  fore(i, 0, n + 1) {
    cnt = (i == 0 ? 0 : a[i - 1]);
    if (cnt > k)
      break;
    lli rem = k - cnt;
    int pos = upper_bound(all(b), rem) - b.begin();
    ans = max(ans, i + pos);
  }
  cout << ans << ENDL;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
#define f first
#define s second
#define pb push_back
#define ENDL '\\n'
#define all(a) begin(a), end(a)
#define sz(a) (int)(a.size())
#define deb(a) cout << #a << " = " << a << ENDL;
#define fore(i, a, b) for (int i(a), ThkMk(b); i < ThkMk; ++i)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> ii;

int main() {
  _ lli n, m, k;
  cin >> n >> m >> k;
  vector<lli> a(n);
  cin >> a[0];
  fore(i, 1, n) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  vector<lli> b(m);
  fore(i, 0, m) cin >> b[i];
  lli cnt = 0;
  lli cur = 0;
  lli ans = 0;
  fore(i, 0, n + 1) {
    cnt = (i == 0 ? 0 : a[i - 1]);
    cur = i;
    if (cnt > k)
      break;
    fore(j, 0, m) {
      if (cnt + b[j] > k)
        break;
      ++cur;
      cnt += b[j];
    }
    ans = max(ans, cur);
  }
  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 size_of_array(array) (sizeof(array) / sizeof(array[0]))
#define DEBUG 0
#define LIMIT (LONG_MAX / 10)
#define POSITIVE_LAST_D (LONG_MAX % 10)
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;

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

  ll ans = 0;
  rep(i, n + 1) {
    ll j = m;
    while (j >= 0) {
      if (sa[i] + sb[j] <= k) {
        ans = max(i + j, ans);
        break;
      } else {
        --j;
        --m;
      }
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define size_of_array(array) (sizeof(array) / sizeof(array[0]))
#define DEBUG 0
#define LIMIT (LONG_MAX / 10)
#define POSITIVE_LAST_D (LONG_MAX % 10)
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;

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

  ll ans = 0;
  rep(i, n + 1) {
    ll j = m;
    while (1) {
      if (sa[i] + sb[j] <= k) {
        ans = max(i + j, ans);
        break;
      } else {
        --j;
        --m;
      }
    }
  }
  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<int> a(n), b(m);
  long long asum = 0, bsum = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    bsum += b[i];
  }

  int ans = 0, bi = m;
  for (int ai = 0; ai <= n; ai++) {
    if (ai > 0)
      asum += a[ai - 1];
    if (asum > k)
      break;
    for (; bi >= 0; bi--) {
      if (asum + bsum <= k || bi == 0) {
        ans = max(ans, ai + bi);
        break;
      } else
        bsum -= b[bi - 1];
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

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

#include <bits/stdc++.h>
#define ll long long
#define P pair<ll, ll>
using namespace std;

const ll inf = 1e18;

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

  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] += b[i - 1];
  a.push_back(inf);
  b.push_back(inf);

  vector<ll> aa(n, 0), bb(m, 0);
  for (int i = 0; i < n; i++) {
    if (a[i] > k)
      break;
    aa[i] = (int)(upper_bound(b.begin(), b.end(), k - a[i]) - b.begin());
    aa[i] += i + 1;
  }
  for (int i = 0; i < m; i++) {
    if (b[i] > k)
      break;
    bb[i] = (int)(upper_bound(a.begin(), a.end(), k - b[i]) - a.begin());
    bb[i] += i + 1;
  }

  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, aa[i]);
  for (int i = 0; i < m; i++)
    ans = max(ans, bb[i]);

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define P pair<ll, ll>
using namespace std;

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

  vector<ll> A(n);
  vector<ll> B(m);

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

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

  ll a = 0, b = 0;
  while (A[a] <= k) {
    a++;
    if (a == n - 1)
      break;
  }
  if (a > 0 && !(A[n - 1] <= k))
    a--;

  ll ans = 0;
  if (A[a] <= k)
    ans = a + 1;

  while (B[b] <= k) {
    b++;
    if (b == m - 1)
      break;
  }
  if (b > 0 && !(B[m - 1] <= k))
    b--;
  if (B[b] <= k)
    ans = max(ans, b + 1);
  b = 0;

  while (a >= 0) {

    while (A[a] + B[b] <= k && b < m - 1) {
      b++;
      if (b == m - 1)
        break;
    }

    if (b > 0 && !(A[a] + B[m - 1] <= k))
      b--;
    if (A[a] + B[b] <= k)
      ans = max(ans, a + b + 2);
    a--;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), aa(n + 1), bb(m + 1), time(n + m + 1);
  aa[0] = 0;
  bb[0] = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (ll i = 1; i <= n; i++) {
    aa[i] = aa[i - 1] + a[i - 1];
  }
  for (ll i = 1; i <= m; i++) {
    bb[i] = bb[i - 1] + b[i - 1];
  }

  ll max = 0;

  for (ll j = 0; j <= m; j++) {
    if (bb[j] <= k) {
      if (j > max) {
        max = j;
      }
    } else {
      break;
    }
  }

  ll maxmax = max;

  for (ll i = 1; i <= n; i++) {
    if (aa[i] > k)
      break;
    for (ll j = max; bb[j] > k - aa[i]; j--) {
      max--;
    }
    if (i + max > maxmax) {
      maxmax = max + i;
    }
  }

  cout << maxmax << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), aa(n + 1), bb(m + 1), time(n + m + 1);
  aa[0] = 0;
  bb[0] = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (ll i = 1; i <= n; i++) {
    aa[i] = aa[i - 1] + a[i - 1];
  }
  for (ll i = 1; i <= m; i++) {
    bb[i] = bb[i - 1] + b[i - 1];
  }

  ll max = 0;

  for (ll j = 0; j <= m; j++) {
    if (bb[j] <= k) {
      if (j > max) {
        max = j;
      }
    } else {
      break;
    }
  }

  ll maxmax = max;

  for (ll i = 1; i <= n; i++) {
    if (aa[i] > k)
      break;
    for (ll j = max; j >= 0; j--) {
      if (aa[i] + bb[j] <= k) {
        if (i + j > maxmax) {
          maxmax = j + i;
        }
        j = max;
        break;
      }
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

#define debug(x)                                                               \\
  cerr << endl << "DEBUG: (" << (#x) << " = " << x << ")" << endl;
#define debugArray(v)                                                          \\
  for (auto i : v)                                                             \\
    cerr << i << " ";                                                          \\
  cerr << endl;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const long long INFLL = 0x3f3f3f3f3f3f3f3fLL;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);

int main(void) {
  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);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }

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

  int ans = 0;

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

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(x)                                                               \\
  cerr << endl << "DEBUG: (" << (#x) << " = " << x << ")" << endl;
#define debugArray(v)                                                          \\
  for (auto i : v)                                                             \\
    cerr << i << " ";                                                          \\
  cerr << endl;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const long long INFLL = 0x3f3f3f3f3f3f3f3fLL;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);

int main(void) {
  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(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }

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

  int ans = 0;

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

  cout << ans << endl;

  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;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;

int bs(vector<ll> b, ll tar) {
  int start = 0, end = b.size() - 1;
  int ans = -1;
  while (start <= end) {
    int mid = start + (end - start) / 2;
    if (b[mid] <= tar) {
      ans = mid;
      start = mid + 1;
    } else
      end = mid - 1;
  }
  return ans;
}

int solve(vector<ll> a, vector<ll> b, ll k) {
  int res = 0;
  ll left;
  for (int i = 0; i < a.size(); ++i) {
    left = k - a[i];
    if (left < 0)
      break;
    int index = bs(b, left);
    if (index == -1)
      break;
    res = max(res, i + index);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  ll k, now;
  cin >> n >> m;
  cin >> k;
  vector<ll> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  rep(i, n) {
    cin >> now;
    a[i + 1] = now + a[i];
  }
  rep(i, m) {
    cin >> now;
    b[i + 1] = now + b[i];
  }
  ll ans = 0;
  rep(i, n + 1) {
    ll cur = k - a[i];
    if (cur >= 0) {
      ll cur2 = upper_bound(b.begin(), b.end(), cur) - b.begin() - 1;
      ans = max(ans, i + cur2);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int bs(vector<ll> b, ll tar) {
  int start = 0, end = b.size() - 1;
  int ans = -1;
  while (start <= end) {
    int mid = start + (end - start) / 2;
    if (b[mid] <= tar) {
      ans = mid;
      start = mid + 1;
    } else
      end = mid - 1;
  }
  return ans;
}

int solve(vector<ll> a, vector<ll> b, ll k) {
  int res = 0;
  ll left;
  for (int i = 0; i < a.size(); ++i) {
    left = k - a[i];
    if (left < 0)
      break;
    int index = bs(b, left);
    if (index == -1)
      break;
    res = max(res, i + index);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  ll k, now;
  cin >> n >> m;
  cin >> k;
  vector<ll> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  rep(i, n) {
    cin >> now;
    a[i + 1] = now + a[i];
  }
  rep(i, m) {
    cin >> now;
    b[i + 1] = now + b[i];
  }
  int res = max(solve(a, b, k), solve(b, a, k));
  cout << res << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
// mt19937 mrand(random_device{}());
const ll mod = 1000000007;
// int rnd(int x) { return mrand() % x;}
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
const int maxx = 1e6 + 10;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
// mt19937 mrand(random_device{}());
const ll mod = 1000000007;
// int rnd(int x) { return mrand() % x;}
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
const int maxx = 1e6 + 10;

ll a[maxx] = {0}, b[maxx] = {0};
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  rep(i, 1, n + 1) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  rep(i, 1, m + 1) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    for (ll j = 0; j <= m; j++) {
      if (b[j] > k)
        break;
      if (a[i] + b[j] > k)
        break;
      if (a[i] + b[j] <= k)
        ans = max(ans, i + j);
    }
  }
  cout << ans;
}