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

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

typedef long long ll;

void solve(long long N, long long M, long long K, std::vector<long long> A,
           std::vector<long long> B) {
  ll rv = 0;
  vector<ll> sA(N + 1, 0);
  vector<ll> sB(M + 1, 0);
  for (ll i = 0; i < N; ++i) {
    sA[i + 1] = A[i] + sA[i];
  }
  for (ll i = 0; i < M; ++i) {
    sB[i + 1] = B[i] + sB[i];
  }

  ll j = M;
  for (ll i = 0; i <= N;) {
    if (sA[i] + sB[j] <= K) {
      rv = max(rv, i + j);
      i++;
      continue;
    } else {
      if (j != 0) {
        j--;
      } else {
        i++;
      }
    }
  }
  cout << rv << endl;
}

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>
using namespace std;

typedef long long ll;

void solve(long long N, long long M, long long K, std::vector<long long> A,
           std::vector<long long> B) {
  ll rv = 0;
  vector<ll> sA(N + 1, 0);
  vector<ll> sB(N + 1, 0);
  for (ll i = 0; i < N; ++i) {
    sA[i + 1] = A[i] + sA[i];
  }
  for (ll i = 0; i < M; ++i) {
    sB[i + 1] = B[i] + sB[i];
  }

  ll j = M;
  for (ll i = 0; i <= N;) {
    if (sA[i] + sB[j] <= K) {
      rv = max(rv, i + j);
      i++;
      continue;
    } else {
      if (j != 0) {
        j--;
      } else {
        i++;
      }
    }
  }
  cout << rv << endl;
}

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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
// cout << setprecision(20) <<

ll N, M, K;
vector<ll> a;
vector<ll> b;

int main() {
  cin >> N >> M >> K;
  a.assign(N, 0);
  b.assign(M, 0);
  ll b_sum = 0;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  for (int i = 0; i < M; ++i)
    cin >> b[i];
  ll A[N + 1], B[M + 1];
  A[0] = 0;
  B[0] = 0;
  for (int i = 0; i < N; ++i)
    A[i + 1] = a[i] + A[i];
  for (int i = 0; i < M; ++i)
    B[i + 1] = b[i] + B[i];

  ll ans = 0;
  int j = M;
  for (int i = 0; i <= N && A[i] <= K; ++i) {
    while (A[i] + B[j] > K && j > 0)
      j--;
    ans = max(ans, (ll)(i + j));
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
// cout << setprecision(20) <<

ll N, M, K;
vector<ll> a;
vector<ll> b;
ll ans_sum = 0;

map<pair<ll, ll>, bool> m;

void dfs(ll a_i, ll b_i, ll cur, ll sum) {
  if (m.count(make_pair(a_i, b_i)))
    return;
  else
    m[make_pair(a_i, b_i)] = true;
  // cout << "at a:" << a_i << " b:" << b_i << endl;
  // cout << "cur " << cur << " sum " << sum << endl;
  // cout << endl;
  ans_sum = max(ans_sum, sum);

  if (a_i == N - 1 && b_i == M - 1 || cur == K)
    return;
  // 本が残ってる + コスト的に選べる
  if (a_i + 1 < N && cur + a[a_i + 1] <= K) {
    dfs(a_i + 1, b_i, cur + a[a_i + 1], sum + 1);
  }
  // 本が残ってる + コスト的に選べる
  if (b_i + 1 < M && cur + b[b_i + 1] <= K) {
    dfs(a_i, b_i + 1, cur + b[b_i + 1], sum + 1);
  }
}

int main() {
  cin >> N >> M >> K;
  a.assign(N, 0);
  b.assign(M, 0);
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  for (int i = 0; i < M; ++i)
    cin >> b[i];
  dfs(-1, -1, 0, 0);
  cout << ans_sum << 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++)
#define put(i) cout << fixed << i << endl
using namespace std;
using ll = long long;

// 全探索だというヒントを聞いて解答

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];

  ll ans = 0;
  ll topB = 0;
  ll sumB = accumulate(b.begin(), b.end(), 0LL);

  vector<ll> cumA(n + 1);
  cumA[0] = 0;
  cumA[1] = a[0];
  for (int i = 2; i <= n; i++)
    cumA[i] = a[i - 1] + cumA[i - 1];

  vector<ll> cumB(m + 1);
  cumB[0] = 0;
  cumB[1] = b[0];
  for (int i = 2; i <= m; i++)
    cumB[i] = b[i - 1] + cumB[i - 1];

  for (int i = 0; i <= m; i++) {
    if (cumB[i] > k) {
      topB = i - 1;
      // ans = max(ans,topB);
      break;
    } else {
      topB = i;
      // ans = max(ans,topB);
    }
  }

  // cout << "cumA:"; for(auto x:cumA) cout << x << " "; cout << endl;
  // cout << "cumB:"; for(auto x:cumB) cout << x << " "; cout << endl;

  for (int i = 0; i <= n; i++) {
    for (; cumA[i] + cumB[topB] > k;) {
      if (topB == 0)
        break;
      topB--;
    }
    // if(topB < 0) break;
    if (cumA[i] + cumB[topB] <= k)
      ans = max(ans, i + topB);

    // cout << "i:" << i << " topB:" << topB << " ans:" << ans << endl;
  }

  put(ans);
}


Error: 0

Fixed submission:

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

// 全探索だというヒントを聞いて解答

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];

  ll ans = 0;
  ll topB = 0;
  ll sumB = accumulate(b.begin(), b.end(), 0LL);

  vector<ll> cumA(n);
  cumA[0] = a[0];
  for (int i = 1; i < n; i++)
    cumA[i] = a[i] + cumA[i - 1];

  vector<ll> cumB(m);
  cumB[0] = b[0];
  for (int i = 1; i < m; i++)
    cumB[i] = b[i] + cumB[i - 1];

  for (int i = 0; i < m; i++) {
    if (cumB[i] > k) {
      if (i > 0) {
        topB = i - 1;
      } else {
        topB = -1;
      }
      ans = max(ans, topB + 1);
      break;
    } else if (i == m - 1) {
      topB = i;
      ans = max(ans, topB + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    // cout << "i:" << i << " topB:" << topB << endl;
    for (; cumA[i] + cumB[topB] > k;) {
      if (topB == 0)
        break;
      topB--;
    }
    // if(topB < 0) break;
    if (cumA[i] + cumB[topB] <= k)
      ans = max(ans, i + topB + 2);
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 200010;
typedef long long ll;
int n, m;
ll A[N];
ll B[N];
ll K;
int main() {
  cin >> n >> m >> K;
  for (int i = 1; i <= n; ++i) {
    ll a;
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i <= m; ++i) {
    ll b;
    cin >> b;
    B[i] = B[i - 1] + b;
  }
  int ans = 0;
  for (int x = 0; x <= n; ++x) {
    if (A[x] > K)
      break;
    int h = m, l = 0;
    int y = m / 2;
    for (int i = 0; i < 1000; ++i) {
      y = (h + l) / 2;
      if (A[x] + B[y] > K)
        h = y;
      else
        l = y;
    }
    if (A[x] + B[h] <= K)
      ans = max(ans, x + h);
    else if (A[x] + B[l] <= K)
      ans = max(ans, x + l);
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 200000;
typedef long long ll;
int n, m;
ll A[N];
ll B[N];
ll K;
int main() {
  cin >> n >> m >> K;
  for (int i = 1; i <= n; ++i) {
    ll a;
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i <= m; ++i) {
    ll b;
    cin >> b;
    B[i] = B[i - 1] + b;
  }
  int ans = 0;
  for (int x = 0; x <= n; ++x) {
    ll Y = K - A[x];
    if (Y < 0)
      break;
    int h = m, l = 0;
    for (int i = 0; i < 1000; ++i) {
      int y = m / 2;
      if (B[y] > Y)
        h = y;
      else
        l = y;
    }
    int Bread = l;
    if (B[h] <= Y)
      Bread = h;
    ans = max(ans, x + Bread);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = 1; i < (n); i++)

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0), b(m + 1, 0);
  rep2(i, n + 1) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  rep2(i, m + 1) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  ll now = m;
  ll ans = 0;
  rep(i, n + 1) {
    bool flag = 1;
    while (flag && now > -1) {
      if (a[i] + b[now] <= k) {
        ans = max(ans, i + now);
        flag = 0;
      }
      while (a[i] + b[now] > k && flag) {
        now--;
        if (now < 0)
          break;
      }
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

typedef long long ll;
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = 1; i < (n); i++)

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0), b(m + 1, 0);
  rep2(i, n + 1) {
    cin >> a[i];
    if (i > 0)
      a[i] += a[i - 1];
  }
  rep2(i, m + 1) {
    cin >> b[i];
    if (i > 0)
      b[i] += b[i - 1];
  }
  ll now = m;
  ll ans = 0;
  rep(i, n + 1) {
    bool flag = 1;
    while (flag && now > -1) {
      if (a[i] + b[now] <= k) {
        ans = max(ans, i + now);
        flag = 0;
      }
      while (a[i] + b[now] > k && flag) {
        now--;
      }
    }
  }
  cout << ans << 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 REPV(iter, v)                                                          \\
  for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter))
#define ALL(v) (v).begin(), (v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

int main() {
  ll N, M, 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> sumA(N + 1), sumB(M + 1);
  sumA[0] = 0;
  REP(i, N) { sumA[i + 1] = sumA[i] + A[i]; }
  sumB[0] = 0;
  REP(i, M) { sumB[i + 1] = sumB[i] + B[i]; }
  ll ans = 0;
  ll indexB = M;
  REP(i, N + 1) {
    ll totalTime = sumA[i];
    while (indexB >= 0 && totalTime + sumB[indexB] > K) {
      indexB--;
    }
    if (indexB < 0) {
      break;
    }
    if (totalTime + sumB[indexB] <= K) {
      ans = max(ans, i + indexB);
    }
  }

  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPV(iter, v)                                                          \\
  for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter))
#define ALL(v) (v).begin(), (v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

int main() {
  ll N, M, 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> sumA(N), sumB(M);
  sumA[0] = A[0];
  REP(i, N - 1) { sumA[i + 1] = sumA[i] + A[i + 1]; }
  sumB[0] = B[0];
  REP(i, M - 1) { sumB[i + 1] = sumB[i] + B[i + 1]; }
  ll ans = 0;
  ll indexB = M - 1;
  REP(i, N) {
    ll totalTime = sumA[i];
    while (totalTime + sumB[indexB] > K) {
      indexB--;
    }
    if (totalTime + sumB[indexB] <= K) {
      ans = max(ans, i + 1 + indexB + 1);
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define pp pop_back
#define in insert
#define ff first
#define ss second
#define e endl
#define bc __builtin_popcount
#define MAXN 200005
#define debug cout << "we are debuuging" << endl;
#define MOD 998244353
ll fast_expo(ll x, ll p) {
  if (p == 0)
    return 1;
  else if (p % 2 == 0) {
    ll t = fast_expo(x, p / 2) % MOD;
    return (t * t) % MOD;
  } else
    return (x * (fast_expo(x, p - 1)) % MOD) % MOD;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  // ll a[n];
  // ll b[m];
  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];
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] <= k) {
      ll k1 = k - a[i];
      ll it = upper_bound(b.begin(), b.end(), k1) - b.begin();
      ans = max(ans, i + 1 + it);
    }
  }
  for (int i = 0; i < m; i++) {
    if (b[i] <= k) {
      ll k1 = k - b[i];
      ll it = upper_bound(a.begin(), a.end(), k1) - a.begin();
      ans = max(ans, i + 1 + it);
    }
  }
  cout << ans << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define pp pop_back
#define in insert
#define ff first
#define ss second
#define e endl
#define bc __builtin_popcount
#define MAXN 200005
#define debug cout << "we are debuuging" << endl;
#define MOD 998244353
ll fast_expo(ll x, ll p) {
  if (p == 0)
    return 1;
  else if (p % 2 == 0) {
    ll t = fast_expo(x, p / 2) % MOD;
    return (t * t) % MOD;
  } else
    return (x * (fast_expo(x, p - 1)) % MOD) % MOD;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n];
  ll b[n];
  vector<ll> v;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    v.pb(a[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    v.pb(b[i]);
  }
  ll ans = 0;
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (v[i] <= k) {
      k -= v[i];
      ans++;
    }
  }
  cout << ans;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long n, m, K;
  cin >> n >> m >> K;
  vector<long long> 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<long long> sum_a(n + 1, 0), sum_b(m + 1, 0);
  for (int i = 0; i < n; i++) {
    sum_a[i + 1] += sum_a[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    sum_b[i + 1] += sum_b[i] + b[i];
  }
  long long ans = 0;
  for (int i = 0; i < n + 1; i++) {
    if (K < sum_a[i])
      break;
    long long x = K - sum_a[i];
    long long y =
        upper_bound(sum_b.begin(), sum_b.end(), x) - (sum_b.begin()) - 1;
    ans = max(ans, y + i);
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

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

int main() {
  long long n, m, K;
  cin >> n >> m >> K;
  vector<long long> 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<long long>> dp(n + 1, vector<long long>(m + 1, 0));
  long long ans = 0;
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    dp[i + 1][0] += dp[i][0] + a[i];
  }
  for (int i = 0; i < m; i++) {
    dp[0][i + 1] += dp[0][i] + b[i];
  }
  for (int i = 0; i < n + 1; i++) {
    if (K < dp[i][0])
      break;
    long long x = K - dp[i][0];
    long long y = lower_bound(dp[0].begin(), dp[0].end(), x) - dp[0].begin();
    ans = max(ans, y + i);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1);
  ll sum = 0;
  vector<ll> tmp(m);
  a[0] = 0;
  b[0] = 0;
  map<ll, ll> bm;
  for (ll i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i < m + 1; i++) {
    cin >> b[i];
    sum += b[i];
    bm[sum] = i;
    tmp[i - 1] = sum;
  }
  sum = 0;
  ll ans = 0;
  for (ll i = 0; i < m; i++)
    tmp[i] = -tmp[i];
  sort(tmp.begin(), tmp.end());
  for (ll i = 0; i < n + 1; i++) {
    sum += a[i];
    if (sum > k)
      break;
    ll can = i;
    auto itr = lower_bound(tmp.begin(), tmp.end(), sum - k);
    if (itr != b.end()) {
      if (-*itr + sum > k)
        continue;
      can += bm[-*itr];
      // cout << -*itr << endl;
    }
    // cout << can << " " << i << endl;
    ans = max(ans, can);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int suma = 0;
  int sumb = 0;
  vector<int> a(n), b(n);
  int nowa = 0;
  int nowb = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (suma + a[i] <= k) {
      suma += a[i];
      nowa = i;
      ans++;
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (sumb + suma + b[i] <= k) {
      sumb += b[i];
      nowb = i;
      ans++;
    }
  }
  for (int i = nowa; i >= 0; i--) {
    suma -= a[i];
    int can = -1;
    int st = nowb;
    for (int j = st + 1; j < m; j++) {
      if (suma + sumb + b[j] <= k) {
        sumb += b[j];
        nowb++;
        can++;
      } else
        break;
    }
    if (can > 0)
      ans += can;
  }
  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 < (int)(n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);

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

  ll total = 0;
  rep(i, m) total += b[i];

  int j = m;
  int ans = 0;
  rep(i, n + 1) {
    while (j > 0 && total > k) {
      --j;
      total -= b[j];
    }
    if (total > k)
      break;
    ans = max(ans, i + j);
    if (i == n)
      break;
    total += a[i];
  }
  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;
using ll = long long;
using P = pair<int, int>;

const ll MOD = 1e9 + 7;

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

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(n);

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

  ll total = 0;
  rep(i, m) total += b[i];

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

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define loop(i, b, n) for (int i = b; i < (n); ++i)
#define each(e, v) for (auto &&e : v)

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)v.size()

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const double EPS = 1e-9;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
template <typename T> inline void print(const T &x) { cout << x << endl; }

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n + 1);
  vector<int> b(m + 1);

  a[0] = 0;
  cin >> a[1];
  loop(i, 2, n + 1) {
    int t;
    cin >> t;
    a[i] = (k - a[i - 1]) >= t ? a[i - 1] + t : INF;
  }

  b[0] = 0;
  cin >> b[1];
  loop(i, 2, m + 1) {
    int t;
    cin >> t;
    b[i] = (k - b[i - 1]) >= t ? b[i - 1] + t : INF;
  }

  int count = 0;
  for (int i = 0; i <= n; ++i) {
    if (a[i] > k)
      break;
    for (int j = count - i + 1; j <= m; ++j) {
      if (a[i] + b[j] > k)
        break;
      count = i + j;
    }
  }
  print(count);
  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define loop(i, b, n) for (int i = b; i < (n); ++i)
#define each(e, v) for (auto &&e : v)

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)v.size()

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const double EPS = 1e-9;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
template <typename T> inline void print(const T &x) { cout << x << endl; }

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

  cin >> a[0];
  loop(i, 1, n) {
    int t;
    cin >> t;
    a[i] = (k - a[i - 1]) >= t ? a[i - 1] + t : INF;
  }

  cin >> b[0];
  loop(i, 1, m) {
    int t;
    cin >> t;
    b[i] = (k - b[i - 1]) >= t ? b[i - 1] + t : INF;
  }

  int count = 0;
  for (int i = 0; i <= n; ++i) {
    if (i > 0 && a[i - 1] > k)
      break;
    for (int j = 0; j <= m; ++j) {
      if (j > 0 && a[i - 1] + b[j - 1] > k)
        break;
      chmax(count, i + j);
    }
  }
  print(count);
  return 0;
}
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;
  long long A[N + 1];
  A[0] = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }
  long long B[M + 1];
  B[0] = 0;
  for (int i = 1; i < M + 1; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }

  int ans = 0;
  for (int i = 0; i < N + 1; i++) {
    int l = -1;
    int r = M + 1;
    while (r - l > 1) {
      int c = l + (r - l) / 2;
      if (A[i] + B[c] > K) {
        r = c;
      } else {
        ans = max(ans, i + c);
        l = c;
      }
    }
  }
  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;
  long long A[N + 1];
  A[0] = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }
  long long B[M + 1];
  B[0] = 0;
  for (int i = 1; i < M + 1; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }

  int ans = 0;
  for (int i = N; i >= 0; i--) {
    for (int j = M; j >= 0; j--) {
      if (A[i] + B[j] <= K) {
        ans = max(ans, i + j);
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}