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

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

long N, M, K, ans;
vector<long> A, B;

int main(void) {

  cin >> N >> M >> K;
  A.resize(N + 1);
  B.resize(M + 1);
  for (long i = 0; i < N; i++)
    cin >> A[i + 1];
  for (long i = 0; i < M; i++)
    cin >> B[i + 1];

  for (long i = 1; i <= N; i++)
    A[i] += A[i - 1];
  for (long i = 1; i <= M; i++)
    B[i] += B[i - 1];

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

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

long N, M, K;
vector<long> A, B;
vector<vector<pair<long, long>>> dp;

int main(void) {

  cin >> N >> M >> K;
  A.resize(N + 1);
  B.resize(M + 1);
  for (long i = 1; i <= N; i++)
    cin >> A[i];
  for (long i = 1; i <= M; i++)
    cin >> B[i];

  dp.resize(N + 1, vector<pair<long, long>>(M + 1));
  dp[0][0] = pair<long, long>(0, 0);
  for (long i = 1; i <= N; i++) {
    auto tmp = dp[i - 1][0];
    if (tmp.first + A[i] <= K) {
      tmp.first += A[i];
      tmp.second++;
    }
    dp[i][0] = tmp;
  }
  for (long i = 1; i <= M; i++) {
    auto tmp = dp[0][i - 1];
    if (tmp.first + B[i] <= K) {
      tmp.first += B[i];
      tmp.second++;
    }
    dp[0][i] = tmp;
  }

  for (long i = 1; i <= N; i++) {
    for (long j = 1; j <= M; j++) {
      auto tmp1 = dp[i - 1][j];
      auto tmp2 = dp[i][j - 1];
      if (tmp1.first + A[i] <= K) {
        tmp1.first += A[i];
        tmp1.second++;
      }
      if (tmp2.first + B[i] <= K) {
        tmp2.first += B[i];
        tmp2.second++;
      }
      if (tmp1.second > tmp2.second) {
        dp[i][j] = tmp1;
      } else {
        if (tmp1.first <= tmp2.first) {
          dp[i][j] = tmp1;
        } else {
          dp[i][j] = tmp2;
        }
      }
    }
  }

  // for (long i = 0; i <= N; i++) {
  //     for (long j = 0; j <= M; j++) {
  //         cout << "(" << dp[i][j].first << "," << dp[i][j].second << ")" << "
  //         ";
  //     }
  //     cout << endl;
  // }

  cout << dp[N][M].second << endl;

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

/*
 *  Author: Arpit Bhayani
 *  https://arpitbhayani.me
 */
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long

#define MIN(a, b) a < b ? a : b
#define MAX(a, b) a > b ? a : b

using namespace std;

int readline(char *str) {
  int i = 0;
  char ch;
  while ((ch = getchar()) != '\\n') {
    str[i++] = ch;
  }
  str[i] = '\\0';
  return i;
}

int arr1[200001];
int arr2[200001];

int foo(int n, int m, ll int x) {
  ll int sum = 0;
  int i = 0, j = 0;
  int count = 0;

  while (i < n) {
    sum += (ll int)arr1[i];
    i++;
    if (sum > x) {
      i--;
      sum -= (ll int)arr1[i];
      break;
    }
  }
  count = i;

  while (j < m) {
    sum += (ll int)arr2[j];
    j++;
    while (sum > x && i > 0) {
      i--;
      sum -= (ll int)arr1[i];
    }
    count = (sum <= x) ? MAX((count), (i + j)) : count;
  }

  return count;
}

int main(int argc, char *argv[]) {

  ll int x;
  int n, m;
  scanf("%d%d%lld", &n, &m, &x);

  for (int i = 0; i < n; i++) {
    scanf("%d", &arr1[i]);
  }

  for (int j = 0; j < m; j++) {
    scanf("%d", &arr2[j]);
  }

  printf("%d\\n", foo(n, m, x));
}

Error: 0

Fixed submission:

/*
 *  Author: Arpit Bhayani
 *  https://arpitbhayani.me
 */
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long

#define MIN(a, b) a < b ? a : b
#define MAX(a, b) a > b ? a : b

using namespace std;

int readline(char *str) {
  int i = 0;
  char ch;
  while ((ch = getchar()) != '\\n') {
    str[i++] = ch;
  }
  str[i] = '\\0';
  return i;
}

int arr1[100001];
int arr2[100001];

int foo(int n, int m, ll int x) {
  ll int sum = 0;
  int i = 0, j = 0;
  int count = 0;

  while (i < n) {
    sum += (ll int)arr1[i];
    i++;
    if (sum > x) {
      i--;
      sum -= (ll int)arr1[i];
      break;
    }
  }
  count = i;

  while (j < m) {
    sum += (ll int)arr2[j];
    j++;
    while (sum > x && i > 0) {
      i--;
      sum -= (ll int)arr1[i];
    }
    count = (sum <= x) ? MAX((count), (i + j)) : count;
  }

  return count;
}

int main(int argc, char *argv[]) {

  ll int x;
  int n, m;
  scanf("%d%d%lld", &n, &m, &x);

  for (int i = 0; i < n; i++) {
    scanf("%d", &arr1[i]);
  }

  for (int j = 0; j < m; j++) {
    scanf("%d", &arr2[j]);
  }

  printf("%d\\n", foo(n, m, x));
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long int n, m, k;
long long int all = 0;
vector<long long int> a, b;
int main(void) {
  cin >> n >> m >> k;
  a.resize(n + 1);
  b.resize(m + 1);
  a.at(0) = 0;
  b.at(0) = 0;
  for (long long int i = 0; i < n; i++) {
    long long int hoge;
    cin >> hoge;
    a.at(i + 1) = a.at(i) + hoge;
  }
  for (long long int i = 0; i < m; i++) {
    long long int hoge;
    cin >> hoge;
    b.at(i + 1) = b.at(i) + hoge;
  }
  all = a.at(n) + b.at(m);
  if (all <= k) {
    cout << n + m << endl;
  } else {
    long long int ans = 0;
    for (long long int i = 0; i <= n; i++) {
      long long int pre = i;
      long long int high = m;
      long long int low = 0;
      long long int mid;
      long long int hoge;
      while (high >= low) {
        mid = (high + low) / 2;
        hoge = a.at(i) + b.at(mid);
        if (hoge == k) {
          break;
        } else if (hoge > k) {
          high = mid - 1;
        } else {
          low = mid + 1;
        }
      }
      if (hoge > k) {
        if (mid != 0 && a.at(i) + b.at(mid - 1) <= k) {
          ans = max(ans, i + mid - 1);
        }
      } else {
        if (mid != m && a.at(i) + b.at(mid + 1) <= k) {
          ans = max(ans, i + mid + 1);
        } else {
          ans = max(ans, i + mid);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
vector<long long int> a, b;
long long int ans = 0;
long long int n, m, k;
map<pair<long long int, pair<long long int, long long int>>, long long int> dp;
long long int saiki(long long int i, long long int A, long long int B) {
  if (i <= 0 || (A == n && B == m)) {
    return 0;
  }
  pair<long long int, pair<long long int, long long int>> hoge =
      make_pair(i, make_pair(A, B));
  if (dp.count(hoge)) {
    return dp[hoge];
  } else {
    long long int prea = -1;
    long long int preb = -1;
    if (A < n) {
      if (i >= a.at(A)) {
        prea = saiki(i - a.at(A), A + 1, B) + 1;
      }
    }
    if (B < m) {
      if (i >= b.at(B)) {
        preb = saiki(i - b.at(B), A, B + 1) + 1;
      }
    }
    dp[hoge] = max(prea, max(preb, (long long int)0));
    return dp[hoge];
  }
}
int main(void) {
  cin >> n >> m >> k;
  a.resize(n);
  long long int all = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> a.at(i);
    all += a.at(i);
  }
  b.resize(m);
  for (long long int i = 0; i < m; i++) {
    cin >> b.at(i);
    all += b.at(i);
  }
  if (all <= k) {
    cout << n + m << endl;
    return 0;
  }
  ans = max((long long int)0, saiki(k, 0, 0));
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N);
  vector<int> B(M);

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

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

  long long int weightA = 0;
  int a, len_A = N;
  for (int i = 0; i < N; i++) {
    a = A[i];
    if (weightA + a > K) {
      len_A = i;
      break;
    } else {
      weightA += a;
    }
  }

  long long int max_count, current_count;
  max_count = len_A;
  current_count = len_A;
  len_A = len_A;

  long long int weightB = 0;
  long long int b;
  for (int j = 0; j < M; j++) {
    b = B[j];

    if (len_A == 0 && weightA + weightB + b > K) {
      break;
    }

    if (weightA + weightB + b <= K) {
      weightB += b;
      current_count++;
    } else {
      while (weightA + weightB + b > K) {
        len_A--;
        weightA -= A[len_A];
        current_count--;
        if (len_A == 0) {
          break;
        }
      }
      if (weightA + weightB + b <= K) {
        weightB += b;
        current_count++;
      }
    }
    max_count = max(max_count, current_count);
  }
  cout << max_count << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<int> B(M);
  for (int j = 0; j < M; j++) {
    cin >> B[j];
  }

  long long int weightA = 0;
  int a, tmp_A = N;
  for (int i = 0; i < N; i++) {
    a = A[i];
    if (weightA + a > K) {
      tmp_A = i;
      break;
    } else {
      weightA += a;
    }
  }

  long long int max_count, current_count, len_A;
  max_count = tmp_A;
  current_count = tmp_A;
  len_A = tmp_A;

  long long int weightB = 0;
  bool terminated = false;
  long long int b;
  for (int j = 0; j < M; j++) {
    b = B[j];
    if (terminated == false && weightA + weightB > K) {
      break;
    }

    if (len_A == 0 && weightA + weightB + b > K) {
      break;
    }

    if (weightA + weightB + b <= K) {
      weightB += b;
      current_count++;
    }

    else if (terminated == false) {
      while (weightA + weightB + b > K) {
        if (len_A > 0) {
          weightA -= A[len_A - 1];
          len_A--;
          current_count--;
        } else {
          terminated = true;
        }
      }
      if (weightA + weightB + b <= K) {
        weightB += b;
        current_count++;
      }
    }
    max_count = max(max_count, current_count);
  }
  cout << max_count << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> as(n);
  vector<int> bs(m);
  for (int &a : as)
    cin >> a;
  for (int &b : bs)
    cin >> b;

  vector<long long> aas(n + 1);
  vector<long long> bbs(m + 1);
  aas[0] = 0;
  bbs[0] = 0;
  int result = 0;
  for (int i = 1; i < n + 1; i++)
    aas[i] = as[i - 1] + aas[i - 1];
  for (int i = 1; i < m + 1; i++)
    bbs[i] = bs[i - 1] + bbs[i - 1];

  for (int i = 0; i < n + 1; i++) {
    if (k < aas[i])
      break;

    vector<long long>::iterator it =
        upper_bound(bbs.begin(), bbs.end(), k - aas[i]);
    int idx =
        it == bbs.end() ? (bbs.size() - 1) : (distance(bbs.begin(), it) - 1);
    result = max(result, (int)(i + idx));
  }

  cout << result << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> as(n);
  vector<int> bs(m);
  for (int &a : as)
    cin >> a;
  for (int &b : bs)
    cin >> b;

  vector<long long> aas(n + 1);
  vector<long long> bbs(m + 1);
  aas[0] = 0;
  bbs[0] = 0;
  int result = 0;
  for (int i = 1; i < n + 1; i++)
    aas[i] = as[i - 1] + aas[i - 1];
  for (int i = 1; i < m + 1; i++)
    bbs[i] = bs[i - 1] + bbs[i - 1];
  // for (int &a: aas) clog << a << endl;

  for (int i = 0; i < n + 1; i++) {
    if (k < aas[i])
      break;

    for (int j = 0; j < m + 1; j++) {
      if (k < aas[i] + bbs[j])
        break;
      if (aas[i] + bbs[j] <= k) {
        // clog << "result: " << result << ", i + j: " << i + j << endl;
        result = max(result, i + j);
      }
    }
  }

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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, n, iv) for (int i = iv; i < n; i++)
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

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

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

  int AS = A.size();
  int BS = B.size();
  int ai = 0;
  int bi = 0;

  int result = 0;

  while (true) {
    if (ai >= AS)
      break;
    int a = A[ai];
    if (a > K)
      break;

    K -= a;
    result++;
    ai++;
  }

  while (true) {
    if (bi >= BS)
      break;
    int b = B[bi];
    if (b > K)
      break;

    K -= b;
    result++;
    bi++;
  }

  cerr << result << endl;

  int t = result;
  int prev = result;
  rep(i, t) {
    int tmp = prev;

    if (ai > 0) {
      ai--;
      tmp--;
      K += A[ai];
    }

    while (true) {
      if (bi >= BS)
        break;
      int b = B[bi];
      if (b > K)
        break;

      K -= b;
      tmp++;
      bi++;
    }

    cerr << tmp << endl;
    result = max(result, tmp);
    prev = tmp;
  }

  cout << result << endl;
}


Error: 0
3
2
2
1


Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, n, iv) for (int i = iv; i < n; i++)
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

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

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

  int AS = A.size();
  int BS = B.size();
  int ai = 0;
  int bi = 0;

  int result = 0;

  while (true) {
    if (ai >= AS)
      break;
    int a = A[ai];
    if (a > K)
      break;

    K -= a;
    result++;
    ai++;
  }

  while (true) {
    if (bi >= BS)
      break;
    int b = B[bi];
    if (b > K)
      break;

    K -= b;
    result++;
    bi++;
  }

  cerr << result << endl;

  int t = result;
  int prev = result;
  rep(i, t) {
    int tmp = prev;

    ai--;
    tmp--;
    K += A[ai];

    while (true) {
      if (bi >= BS)
        break;
      int b = B[bi];
      if (b > K)
        break;

      K -= b;
      tmp++;
      bi++;
    }

    cerr << tmp << endl;
    result = max(result, tmp);
    prev = tmp;
  }

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

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define endl "\\n"
#define fi first
#define se second
#define For(i, a, b) for (ll i = a; i < b; i++)
#define RFor(i, b, a) for (ll i = b; i >= a; i--)
#define sz size()
#define PQ priority_queue
#define len length()
#define mll map<ll, ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define mp(x, y) make_pair(x, y)
#define pb push_back
#define ins insert
#define er erase
#define pf push_front
#define ALL(v) v.begin(), v.end()
#define LAPAK                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;

const ll MOD = 1e9 + 7;
const ll INF = 2e18;
const ll MAX = 2e5 + 2;

int main() {
  LAPAK
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  For(i, 0, n) cin >> a[i];
  For(i, 0, m) cin >> b[i];

  vll prefb(m + 2);
  For(i, 0, m) prefb[i + 1] = prefb[i] + b[i];
  prefb[m + 1] = INF;

  ll timea = 0, ans = 0;
  For(i, 0, n + 1) {
    ll to_search = k - timea;
    ll in = lower_bound(ALL(prefb), to_search) - prefb.begin();
    if (prefb[in] != to_search)
      in--;
    if (timea <= k)
      ans = max(ans, in + i);
    if (i < n)
      timea += a[i];
  }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define endl "\\n"
#define fi first
#define se second
#define For(i, a, b) for (ll i = a; i < b; i++)
#define RFor(i, b, a) for (ll i = b; i >= a; i--)
#define sz size()
#define PQ priority_queue
#define len length()
#define mll map<ll, ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define mp(x, y) make_pair(x, y)
#define pb push_back
#define ins insert
#define er erase
#define pf push_front
#define ALL(v) v.begin(), v.end()
#define LAPAK                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;

const ll MOD = 1e9 + 7;
const ll INF = 2e18;
const ll MAX = 2e5 + 2;
ll n, m, k, ans = 0;
vll a(MAX), b(MAX);

void solve(ll sum = 0, ll i = 0, ll j = 0, ll run = 0) {
  if (sum > k) {
    ans = max(ans, run - 1);
    return;
  }

  if (i < n && j < m) {
    solve(sum + a[i], i + 1, j, run + 1);
    solve(sum + a[j], i, j + 1, run + 1);
  } else if (i < n) {
    solve(sum + a[i], i + 1, j, run + 1);
  } else if (j < m) {
    solve(sum + a[j], i, j + 1, run + 1);
  } else {
    ans = max(ans, run);
    return;
  }
}

int main() {
  LAPAK

  cin >> n >> m >> k;
  a.resize(n);
  b.resize(m);
  For(i, 0, n) cin >> a[i];
  For(i, 0, m) cin >> b[i];

  solve();
  cout << ans;

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

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

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

  // a0 = 0
  // a1 = a0 + A1
  // a2 = a1 + A2
  vector<ll> a(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    ll A;
    cin >> A;
    a.at(i) = a.at(i - 1) + A;
  }

  vector<ll> b(M + 1, 0);
  for (int i = 1; i <= M; i++) {
    ll B;
    cin >> B;
    b.at(i) = b.at(i - 1) + B;
  }

  // ai + bj <= K
  // bj <= K - aj
  int best = M;
  int ans = 0;
  for (int i = 0; i <= N; i++) {
    ll a_num = a.at(i);

    if (a_num > K) {
      break;
    }

    for (int j = best; j >= 0; j--) {
      if (b.at(j) <= K - a_num) {
        best = j;
        break;
      }
    }

    ans = max(ans, (i + best));
  }

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


Error: TLE

Fixed submission:

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

int N, M, K;
vector<int> A;
vector<int> B;

int recursive(int a_index, int b_index, int remaining) {
  // ベースケース
  if (a_index == N && b_index == M) {
    return (N + M);
  }
  if (a_index == N && b_index < M && B.at(b_index) > remaining) {
    return N + b_index;
  }
  if (a_index < N && A.at(a_index) > remaining && b_index == M) {
    return a_index + M;
  }
  if (a_index < N && A.at(a_index) > remaining && b_index < M &&
      B.at(b_index) > remaining) {
    return a_index + b_index;
  }

  // 再帰ステップ
  int ans_a = 0;
  if (a_index < N && A.at(a_index) <= remaining) {
    ans_a = recursive(a_index + 1, b_index, (remaining - A.at(a_index)));
  }
  int ans_b = 0;
  if (b_index < M && B.at(b_index) <= remaining) {
    ans_b = recursive(a_index, b_index + 1, (remaining - B.at(b_index)));
  }

  return max(ans_a, ans_b);
}

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

  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  B.resize(M);
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }

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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int n, m;
  long long k;
  cin >> n >> m >> k;
  vector<long long> A(n);
  vector<long long> B(m);
  vector<long long> a(n + 1, 0);
  vector<long long> b(m + 1, 0);

  for (int i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < m; i++) {
    cin >> B.at(i);
  }

  for (int i = 1; i < n + 1; i++) {
    a.at(i) = a.at(i - 1) + A.at(i - 1);
  }
  for (int i = 1; i < m + 1; i++) {
    b.at(i) = b.at(i - 1) + B.at(i - 1);
    // cout << b.at(i)<< endl;
  }

  long long ans = 0;
  int j = m;
  for (int i = 0; i < n + 1; i++) {
    if (a.at(i) > k) {
      break;
    }
    // cout << endl;
    while (a.at(i) + b.at(j) > k) {
      j--;
    }
    chmax(ans, (long long)(i + j));
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int n, m;
  long long k;
  cin >> n >> m >> k;
  vector<long long> A(n);
  vector<long long> B(m);
  vector<long long> a(n + 1, 0);
  vector<long long> b(m + 1, 0);

  for (int i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < m; i++) {
    cin >> B.at(i);
  }

  for (int i = 1; i < n + 1; i++) {
    a.at(i) = a.at(i - 1) + A.at(i - 1);
  }
  for (int i = 1; i < m + 1; i++) {
    b.at(i) = b.at(i - 1) + B.at(i - 1);
    // cout << b.at(i)<< endl;
  }

  long long ans = 0;
  for (int i = 0; i < n + 1; i++) {
    if (a.at(i) > k) {
      break;
    }
    // cout << endl;
    for (int j = 0; j < m + 1; j++) {
      if (a.at(i) + b.at(j) > k) {
        break;
      }
      // cout << a.at(i) + b.at(j) << endl;
      chmax(ans, (long long)(i + j));
    }
  }

  cout << ans << endl;
}