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

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

int main() {

  long long int N, M, K;
  cin >> N >> M >> K;
  long long 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];
  }

  long long int Atime = K;
  long long int last_num = 0;

  for (int i = 0; i < N; i++) {
    last_num = i;
    Atime -= A[i];
    if (Atime < 0) {
      Atime += A[i];
      last_num = i - 1;
      break;
    }
  }

  vector<int> ANS(M + 1, last_num + 1);

  bool OVER = false;

  for (int i = 0; i < M; i++) {
    if (i >= 1) {
      ANS[i] = ANS[i - 1];
    }
    while (Atime - B[i] < 0) {
      if (last_num == -1) {
        OVER = true;
        break;
      }
      Atime += A[last_num];
      last_num--;
      ANS[i]--;
    }
    if (OVER) {
      break;
    }

    Atime -= B[i];
    ANS[i]++;
  }

  sort(ANS.begin(), ANS.end(), greater<int>());

  cout << ANS[0] << endl;
}

Error: 0

Fixed submission:

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

int main() {

  long long int N, M, K;
  cin >> N >> M >> K;
  long long 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];
  }

  vector<vector<int>> dp(2, vector<int>(K));

  long long int Atime = K;
  long long int last_num = 0;

  for (int i = 0; i < N; i++) {
    last_num = i + 1;
    Atime -= A[i];
    if (Atime <= 0) {
      Atime += A[i];
      last_num = i;
      break;
    }
  }

  vector<int> ANS(M, last_num);

  bool OVER = false;

  for (int i = 0; i < M; i++) {
    if (i >= 1) {
      ANS[i] = ANS[i - 1];
    }
    while (Atime - B[i] < 0) {
      if (last_num = 0) {
        OVER = true;
        break;
      }
      Atime += A[last_num];
      last_num--;
      ANS[i]--;
    }
    if (OVER) {
      break;
    }

    Atime -= B[i];
    ANS[i]++;
  }

  sort(ANS.begin(), ANS.end(), greater<int>());

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

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

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll arr1[n], arr2[m];
    for (int i = 0; i < n; i++)
      cin >> arr1[i];
    for (int i = 0; i < m; i++)
      cin >> arr2[i];
    ll dp[n + 1];
    dp[0] = arr1[0];
    ll max_ = 0, run = 0, i = 0;
    for (i = 0; i < n && run + arr1[i] <= k; i++) {
      run += arr1[i];
    }
    max_ = i;
    run = 0;
    for (int i = 1; i < n; i++)
      dp[i] = dp[i - 1] + arr1[i];
    for (int i = 0; i < m && run <= k; i++) {
      // i books from second pile
      //  costed run
      run += arr2[i];
      if (run > k)
        break;
      ll idx = upper_bound(dp, dp + n, k - run) - dp;
      // cout<<" for run ="<<run<<" "<<i+1<<" + "<<idx<<endl;
      max_ = max(max_, i + 1 + idx);
    }
    cout << max_;
  }
}

Error: WA

Fixed submission:

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

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll arr1[n], arr2[m];
    for (int i = 0; i < n; i++)
      cin >> arr1[i];
    for (int i = 0; i < m; i++)
      cin >> arr2[i];
    ll dp[n + 1];
    dp[0] = arr1[0];
    ll max_ = 0, run = 0, i = 0;
    for (i = 0; i < n && run + arr1[i] <= k; i++) {
      run += arr1[i];
    }
    max_ = i;
    run = 0;
    for (int i = 1; i < m; i++)
      dp[i] = dp[i - 1] + arr1[i];
    for (int i = 0; i < m && run <= k; i++) {
      // i books from second pile
      //  costed run
      run += arr2[i];
      if (run > k)
        break;
      ll idx = upper_bound(dp, dp + n, k - run) - dp;
      // cout<<" for run ="<<run<<" "<<i+1<<" + "<<idx<<endl;
      max_ = max(max_, i + 1 + idx);
    }
    cout << max_;
  }
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define reps(i, a, b) for (int i = a; i < b; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> A(n, 0);
  vector<ll> B(m, 0);
  rep(t, n) cin >> A.at(t);
  rep(j, m) cin >> B.at(j);
  vector<ll> culA(n + 1, 0);
  vector<ll> culB(m + 1, 0);
  reps(r, 1, n + 1) { culA.at(r) = culA.at(r - 1) + A.at(r - 1); }
  reps(l, 1, m + 1) { culB.at(l) = culB.at(l - 1) + B.at(l - 1); }
  ll ans = 0;
  ll bp = m;
  rep(i, n + 1) {
    ll tmp = culA.at(i) + culB[bp];
    while (tmp > k && bp > 0) {
      bp--;
      tmp = culA.at(i) + culB.at(bp);
    }
    if (tmp <= k) {
      ll res = bp + i;
      ans = max(ans, res);
    }
  }

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define reps(i, a, b) for (int i = a; i < b; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> A(n, 0);
  vector<ll> B(m, 0);
  rep(t, n) cin >> A[t];
  rep(j, m) cin >> B[j];
  vector<ll> culA(n + 1, 0);
  vector<ll> culB(m + 1, 0);
  reps(r, 1, n + 1) { culA[r] = culA[r - 1] + A[r - 1]; }
  reps(l, 1, m + 1) { culB[l] = culB[l - 1] + B[l - 1]; }
  ll ans = 0;
  ll bp = m;
  rep(i, n + 1) {
    ll tmp = culA[i] + culB[bp];
    while (tmp > k) {
      bp--;
      tmp = culA[i] + culB[bp];
    }
    ll res = bp + i;
    ans = max(ans, res);
  }

  cout << ans << endl;

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

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

int main() {
  vector<long long> A, B;
  long long K;
  long long N, M;
  cin >> N >> M >> K;
  vector<long long> Sum_A(N + 1, 0), Sum_B(M + 1, 0);
  int tmp;
  Sum_A.push_back(0);
  for (int i = 1; i <= N; i++) {
    cin >> tmp;
    A.push_back(tmp);
    Sum_A[i] = Sum_A[i - 1] + tmp;
  }
  Sum_B.push_back(0);
  for (int i = 1; i <= M; i++) {
    cin >> tmp;
    B.push_back(tmp);
    Sum_B[i] = Sum_B[i - 1] + tmp;
  }
  int ans = 0;
  long long p = M;
  for (int i = 0; i <= N; i++) {
    for (int j = p; j >= 0; j--) {
      if (Sum_A[i] + Sum_B[j] <= K) {
        if (ans < i + j) {
          ans = i + j;
        }
        p = j;
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  vector<long long> A, B;
  long long K;
  long long N, M;
  cin >> N >> M >> K;
  vector<long long> Sum_A(N + 1, 0), Sum_B(M + 1, 0);
  int tmp;
  Sum_A.push_back(0);
  for (int i = 1; i <= N; i++) {
    cin >> tmp;
    A.push_back(tmp);
    Sum_A[i] = Sum_A[i - 1] + tmp;
  }
  Sum_B.push_back(0);
  for (int i = 1; i <= M; i++) {
    cin >> tmp;
    B.push_back(tmp);
    Sum_B[i] = Sum_B[i - 1] + tmp;
  }
  int ans = 0;
  long long p = M;
  for (int i = 0; i <= N; i++) {
    for (int j = p; j >= 0; j--) {
      if (Sum_A[i] + Sum_B[j] <= K) {
        if (ans < i + j) {
          ans = i + j;
        }
        // p = j;
        break;
      }
    }
  }
  cout << ans << endl;

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

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

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

  for (ll i = 0; i < n; i++) {
    if (k - a[i] < 0) {
      break;
    }
    ll now = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin();
    now += i + 1;
    ans = max(ans, now);
  }

  ll now2 = upper_bound(b.begin(), b.end(), k) - b.begin();
  ans = max(ans, now2);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

  for (ll i = 0; i < n; i++) {
    if (k - a[i] < 0) {
      break;
    }
    ll now = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin();
    now += i + 1;
    ans = max(ans, now);
  }
  if (b[0] < k) {
    cout << ans << endl;
    return 0;
  }
  ll now2 = upper_bound(b.begin(), b.end(), k) - b.begin();
  ans = max(ans, now2);

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stdlib.h>
#include <string>
#include <vector>
#define MOD_P 1000000007
#define MOD_Q 998244353
#define PI 3.14159265358979
#define ll long long
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a;
  vector<ll> b;
  ll sum = 0LL;

  a.push_back(0);
  for (int i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    sum += tmp;
    a.push_back(sum);
  }
  sum = 0;
  b.push_back(0);
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    sum += tmp;
    b.push_back(sum);
  }

  int ans = 0;
  /*for (int v : a) {
          printf("%d ", v);
  }
  printf("\\n");
  for (int v : b) {
          printf("%d ", v);
  }
  printf("\\n");*/
  int reft = b.size() - 1;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    ll t = k - a[i];
    while (b[reft] > t) {
      reft--;
    }
    if (ans < reft + i) {
      ans = reft + i;
    }
  }
  printf("%d", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stdlib.h>
#include <string>
#include <vector>
#define MOD_P 1000000007
#define MOD_Q 998244353
#define PI 3.14159265358979
#define ll long long
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a;
  vector<ll> b;
  ll sum = 0LL;

  a.push_back(0);
  for (int i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    sum += tmp;
    a.push_back(sum);
  }
  sum = 0;
  b.push_back(0);
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    sum += tmp;
    b.push_back(sum);
  }

  int ans = 0;
  /*for (int v : a) {
          printf("%d ", v);
  }
  printf("\\n");
  for (int v : b) {
          printf("%d ", v);
  }
  printf("\\n");*/

  for (int i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    ll t = k - a[i];
    int reft = b.size() - 1;
    while (b[reft] > t) {
      reft--;
    }
    if (ans < reft + i) {
      ans = reft + i;
    }
  }
  printf("%d", ans);

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

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

int main() {
  ll i, j, x, y, x1, y1, x2, y2, q, p, r, z, k, t, n, m;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> k;
  ll a[n], b[m];
  for (i = 0; i < n; i++) {
    cin >> x;
    if (i == 0) {
      a[i] = x;
    } else {
      a[i] = a[i - 1] + x;
    }
  }
  for (j = 0; j < m; j++) {
    cin >> x;
    if (j == 0) {
      b[j] = x;
    } else {
      b[j] = b[j - 1] + x;
    }
  }
  ll mx = lower_bound(b, b + m, k) - b;
  if (binary_search(b, b + m, k))
    mx++;
  for (i = 0; i < n; i++) {

    if (a[i] > k) {
      break;
    }

    if (b[0] <= k - a[i]) {

      j = lower_bound(b, b + m, k - a[i]) - b;
      if (binary_search(b, b + m, k - a[i]))
        j++;

    } else {
      j = 0;
    }
    mx = max(mx, i + j + 1);
  }
  cout << mx;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mx = 0;
map<pair<ll, ll>, ll> mp;
void rec(ll *a, ll *b, ll n, ll m, ll i, ll j, ll k, ll c) {
  if (mp.find({i, j}) != mp.end()) {
    mx = max(mx, mp[{i, j}]);
    return;
  }
  mx = max(mx, c);
  if (i < n && a[i] <= k) {

    rec(a, b, n, m, i + 1, j, k - a[i], c + 1);
  }
  if (j < m && b[j] <= k) {
    rec(a, b, n, m, i, j + 1, k - b[j], c + 1);
  }
  mp[{i, j}] = mx;
}
int main() {
  ll i, j, x, y, x1, y1, x2, y2, q, p, r, z, k, t, n, m;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  t = 1;
  while (t--) {
    cin >> n >> m >> k;
    ll a[n], b[m];
    for (i = 0; i < n; i++)
      cin >> a[i];
    for (i = 0; i < m; i++)
      cin >> b[i];
    rec(a, b, n, m, 0, 0, k, 0);
    cout << mx;
  }

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

#include <bits/stdc++.h>

using namespace std;
#define REPI(x) for (int i = 0; i < x; i++)
#define REPJ(x) for (int j = 0; j < x; j++)

int main() {
  long long N, M, K, b, time, timeb, anum, bnum, res;
  vector<long long> A, B;

  cin >> N >> M >> K;

  REPI(N) {
    cin >> b;
    A.push_back(b);
  }

  REPI(M) {
    cin >> b;
    B.push_back(b);
  }

  time = 0;
  anum = 0;
  while (1) {
    if (anum == N || time + A[anum] > K)
      break;

    time += A[anum];
    anum++;
  }
  res = anum;

  bnum = 0;
  while (1) {

    while (bnum != M && time + B[bnum] <= K) {
      time += B[bnum];
      bnum++;
    }

    res = max(res, anum + bnum);

    anum--;
    if (anum < 0)
      break;

    time -= A[anum];
  }

  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define REPI(x) for (int i = 0; i < x; i++)
#define REPJ(x) for (int j = 0; j < x; j++)

int main() {
  long long N, M, K, b, time, timeb, anum, bnum, res;
  vector<long long> A, B;

  cin >> N >> M >> K;

  REPI(N) {
    cin >> b;
    A.push_back(b);
  }

  REPI(M) {
    cin >> b;
    B.push_back(b);
  }

  time = 0;
  anum = 0;
  while (1) {
    if (anum == N || time + A[anum] > K)
      break;

    time += A[anum];
    anum++;
  }
  res = anum;

  bnum = 0;
  while (1) {
    while (time + B[bnum] <= K) {
      time += B[bnum];
      bnum++;
      if (bnum == M)
        break;
    }

    res = max(res, anum + bnum);

    anum--;
    if (anum < 0)
      break;

    time -= A[anum];
  }

  cout << res << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repll(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
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() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m, k;
  cin >> n >> m >> k;

  vector<int> a(n), b(m);
  vector<ll> cum_a(n + 2), cum_b(m + 2);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  cum_a[0] = 0;
  cum_a[n + 1] = 1000000000;
  cum_b[0] = 0;
  cum_b[m + 1] = 1000000000;
  rep(i, n) cum_a[i + 1] = cum_a[i] + a[i];
  rep(i, m) cum_b[i + 1] = cum_b[i] + b[i];

  // cum_b[i] == j冊読むのにかかる時間

  int ans = 0;

  rep(i, n + 1) { // i:[0, n]
    int remaining = k - cum_a[i];
    if (remaining < 0)
      break; // '='は抜く(cum_a == k のピッタリもok)
    auto itr = upper_bound(all(cum_b), remaining);

    int j = distance(cum_b.begin(), itr) - 1;
    ans = max(ans, i + j);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
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() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;

  queue<ll> q1, q2;

  ll a;

  rep(i, n) {
    cin >> a;
    q1.push(a);
  }
  rep(i, m) {
    cin >> a;
    q2.push(a);
  }

  int j = -1;

  while (ans < k) {
    if (q1.front() <= q2.front()) {
      ans += q1.front();
      q1.pop();
    } else {
      ans += q2.front();
      q2.pop();
    }
    j++;
    if (j == n + m) {
      cout << j << endl;
      return 0;
    }
  }

  cout << j << endl;

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

#include <algorithm>
#include <iostream>

int main() {
  int N, M, K;
  std::cin >> N >> M >> K;
  unsigned long long A[N + 1], B[M + 1];
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  for (int i = 0; i < M; i++) {
    std::cin >> B[i];
  }
  A[N] = INT32_MAX;
  B[M] = INT32_MAX;

  for (int i = 1; i < N; i++) {
    A[i] += A[i - 1];
  }
  for (int j = 1; j < M; j++) {
    B[j] += B[j - 1];
  }

  int i = N - 1;
  int best = 0;
  while (A[i] > K) {
    i--;
    if (i < 0)
      break;
  }

  int j = -1;
  for (; i >= -1; i--) {
    int ans = 0;
    if (i == -1) {
      while (B[j + 1] <= K) {
        j++;
        if (j >= M)
          break;
      }
    } else {
      while (A[i] + B[j + 1] <= K) {
        j++;
        if (j >= M)
          break;
      }
    }
    ans = i + j + 2;
    if (ans > best) {
      best = ans;
    }
  }

  std::cout << best << std::endl;

  // int end;
  // std::cin >> end;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

int main() {
  int N, M, K;
  std::cin >> N >> M >> K;
  unsigned long long A[N + 1], B[M + 1];
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  for (int i = 0; i < M; i++) {
    std::cin >> B[i];
  }
  A[N] = INT32_MAX;
  B[M] = INT32_MAX;

  for (int i = 1; i < N; i++) {
    A[i] += A[i - 1];
  }
  for (int j = 1; j < M; j++) {
    B[j] += B[j - 1];
  }

  int i = N - 1;
  int best = 0;
  while (A[i] > K) {
    i--;
    if (i < 0)
      break;
  }

  for (; i >= -1; i--) {
    int ans = 0;
    int j = -1;
    if (i == -1) {
      while (B[j + 1] <= K) {
        j++;
        if (j >= M)
          break;
      }
    } else {
      while (A[i] + B[j + 1] <= K) {
        j++;
        if (j >= M)
          break;
      }
    }
    ans = i + j + 2;
    if (ans > best) {
      best = ans;
    }
  }

  std::cout << best << std::endl;

  // int end;
  // std::cin >> end;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, M;
  long K;
  cin >> N >> M >> K;
  vector<long> A(N + 1, 0), B(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    long W;
    cin >> W;
    A.at(i) = W + A.at(i - 1);
  }
  for (int i = 1; i <= M; i++) {
    long W;
    cin >> W;
    B.at(i) = W + B.at(i - 1);
  }
  priority_queue<int> X;
  int best;
  for (int i = M; i >= 0; i--) {
    if (K >= B.at(i)) {
      best = i;
      break;
    }
  }
  X.push(best);
  for (int i = 1; i <= N; i++) {
    if (A.at(i) > K)
      break;
    for (int j = best; j >= 0; j--) {
      if (K >= A.at(i) + B.at(j)) {
        X.push(i + j);
        best = j;
        break;
      }
    }
  }
  cout << X.top() << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  long K;
  cin >> N >> M >> K;
  vector<long> A(N + 1, 0), B(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    long W;
    cin >> W;
    A.at(i) = W + A.at(i - 1);
  }
  for (int i = 1; i <= M; i++) {
    long W;
    cin >> W;
    B.at(i) = W + B.at(i - 1);
  }
  priority_queue<int> X;
  int best;
  for (int i = M; i >= 0; i--) {
    if (K >= B.at(i)) {
      best = i;
      break;
    }
  }
  X.push(best);
  for (int i = 1; i <= N; i++) {
    if (A.at(i) > K)
      break;
    for (int j = 0; j <= best; j++) {
      if (A.at(i) + B.at(j) <= K)
        X.push(i + j);
    }
  }
  cout << X.top() << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, M;
  long K;
  cin >> N >> M >> K;
  vector<long> A(N + 1, 0), B(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    long W;
    cin >> W;
    A.at(i) = W + A.at(i - 1);
  }
  for (int i = 1; i <= M; i++) {
    long W;
    cin >> W;
    B.at(i) = W + B.at(i - 1);
  }
  int left = -1;
  int right = N + M + 1;
  while (left < right) {
    int X = (left + right) / 2;
    int ableA = min(N, X);
    int ableB = min(M, X);
    bool can = false;
    if (N >= M) {
      for (int i = X - ableB; i <= ableA; i++) {
        long time = A.at(i) + B.at(X - i);
        if (time <= K) {
          can = true;
          break;
        }
      }
    } else {
      for (int i = X - ableA; i <= ableB; i++) {
        long time = B.at(i) + A.at(X - i);
        if (time <= K) {
          can = true;
          break;
        }
      }
    }
    if (can)
      left = X + 1;
    else
      right = X;
  }
  cout << max(left - 1, 0) << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  long K;
  cin >> N >> M >> K;
  vector<long> A(N + 1, 0), B(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    long W;
    cin >> W;
    A.at(i) = W + A.at(i - 1);
  }
  for (int i = 1; i <= M; i++) {
    long W;
    cin >> W;
    B.at(i) = W + B.at(i - 1);
  }
  priority_queue<int> X;
  int best;
  for (int i = M; i >= 0; i--) {
    if (K >= B.at(i)) {
      best = i;
      break;
    }
  }
  X.push(best);
  for (int i = 1; i <= N; i++) {
    if (A.at(i) > K)
      break;
    for (int j = best; j >= 0; j--) {
      if (K >= A.at(i) + B.at(j)) {
        X.push(i + j);
        // best=j;
        break;
      }
    }
  }
  cout << X.top() << endl;
}