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

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

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


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, k;

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

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

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

  vector<ll> a(n), b(m), p1(n), p2(m);

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

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

  int ans1 = -1;
  auto at = upper_bound(p1.begin(), p1.end(), k);
  if (at == p1.end()) {
    ans1 = n;
  } else
    ans1 = at - p1.begin();

  int ans2 = -1;
  auto bt = upper_bound(p2.begin(), p2.end(), k);
  if (bt == p2.end()) {
    ans2 = m;
  } else
    ans2 = bt - p2.begin();

  int ans3 = -1;
  for (int i = 0; i < p1.size(); i++) {
    if (k - p1[i] < 0)
      break;
    auto ct = upper_bound(p2.begin(), p2.end(), k - p1[i]);
    if (ct == p2.end())
      ans3 = max(ans3, i + m + 1);
    else {
      int x = ct - p2.begin();
      ans3 = max(ans3, i + x + 1);
    }
  }

  int ans4 = -1;
  for (int i = 0; i < p2.size(); i++) {
    if (k - p2[i] < 0)
      break;
    auto dt = upper_bound(p1.begin(), p1.end(), k - p2[i]);
    if (dt == p1.end())
      ans4 = max(ans4, i + n + 1);
    else {
      int x = dt - p1.begin();
      ans4 = max(ans4, i + x + 1);
    }
  }

  cout << max(ans1, max(ans2, max(ans3, ans4))) << endl;
}

Error: WA

Fixed submission:

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

  vector<ll> a(n), b(n), p1(n), p2(m);

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

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

  int ans1 = -1;
  auto at = upper_bound(p1.begin(), p1.end(), k);
  if (at == p1.end()) {
    ans1 = n;
  } else
    ans1 = at - p1.begin();

  int ans2 = -1;
  auto bt = upper_bound(p2.begin(), p2.end(), k);
  if (bt == p2.end()) {
    ans2 = m;
  } else
    ans2 = bt - p2.begin();

  int ans3 = -1;
  for (int i = 0; i < p1.size(); i++) {
    if (k - p1[i] < 0)
      break;
    auto ct = upper_bound(p2.begin(), p2.end(), k - p1[i]);
    if (ct == p2.end())
      ans3 = max(ans3, i + m + 1);
    else {
      int x = ct - p2.begin();
      ans3 = max(ans3, i + x + 1);
    }
  }

  int ans4 = -1;
  for (int i = 0; i < p2.size(); i++) {
    if (k - p2[i] < 0)
      break;
    auto dt = upper_bound(p1.begin(), p1.end(), k - p2[i]);
    if (dt == p1.end())
      ans4 = max(ans4, i + n + 1);
    else {
      int x = dt - p1.begin();
      ans4 = max(ans4, i + x + 1);
    }
  }

  cout << max(ans1, max(ans2, max(ans3, ans4))) << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll alst[n + 1], blst[m + 1];
  alst[0] = 0;
  blst[0] = 0;
  rep(i, 1, n + 1) {
    cin >> alst[i];
    alst[i] += alst[i - 1];
  }
  rep(i, 1, m + 1) {
    cin >> blst[i];
    blst[i] += blst[i - 1];
  }
  int best = 0, best_j = m;
  rep(i, 0, n + 1) {
    ll tmp = k - alst[i];
    if (tmp < 0) {
      break;
    }
    while (blst[best_j] > tmp)
      best_j--;
    best = max(best, i + best_j);
  }
  cout << best << endl;
  return 0;
}


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

ll dfs(ll alst[], ll blst[], ll aidx, ll bidx, ll ans, ll amax, ll bmax, ll cnt,
       ll rmax) {
  if (cnt > rmax) {
    return ans;
  } else {
    ans++;
  }
  if (aidx == amax) {
    if (bidx == bmax) {
      return ans;
    } else {
      return dfs(alst, blst, aidx, bidx + 1, ans, amax, bmax, cnt + blst[bidx],
                 rmax);
    }
  } else {
    if (bidx == bmax) {
      return dfs(alst, blst, aidx + 1, bidx, ans, amax, bmax, cnt + alst[aidx],
                 rmax);
    } else {
      return max(dfs(alst, blst, aidx + 1, bidx, ans, amax, bmax,
                     cnt + alst[aidx], rmax),
                 dfs(alst, blst, aidx, bidx + 1, ans, amax, bmax,
                     cnt + blst[bidx], rmax));
    }
  }
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll alst[n], blst[m];
  rep(i, n) cin >> alst[i];
  rep(i, m) cin >> blst[i];
  cout << max(dfs(alst, blst, 1, 0, 0, n, m, alst[0], k),
              dfs(alst, blst, 0, 1, 0, n, m, blst[0], k))
       << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

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

  long long t = 0;
  rep(i, m) t += b[i];

  int j = m;
  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 <iostream>
#include <stdio.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int n, m, k, ca = 0, cb = 0, ansA = 0, ansB = 0, ans = 0;
  cin >> n >> m >> k;
  vector<int> A(n + 1), B(m + 1);
  A[0] = 0, B[0] = 0;
  rep(i, n) cin >> A[i + 1];
  rep(i, m) cin >> B[i + 1];
  rep(i, n + 1) {
    ansB = 0;
    if ((ansA += A[i]) > k)
      break;
    ca = i;
    rep(t, m + 1) {
      ansB += B[t];
      cb = t;
      if (ansB + ansA > k)
        break;
      if (ca + cb > ans)
        ans = ca + cb;
    }
  }
  cout << ans;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long a[222222], b[222222];
int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  a[0] = b[0] = 0;
  for (long long i = 1; i <= n; i++) {
    long long tmp;
    cin >> tmp;
    a[i] = a[i - 1] + tmp;
  }
  for (long long i = 1; i <= m; i++) {
    long long tmp;
    cin >> tmp;
    b[i] = b[i - 1] + tmp;
  }
  int maxn = 0;
  int j = m;
  for (int i = 0; i <= n; i++) {
    for (; j >= 0; j--) {
      if (a[i] + b[j] > k)
        continue;
      else {
        maxn = max(maxn, i + j);
        break;
      }
    }
  }
  cout << maxn;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[111111], b[111111];
int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  a[0] = b[0] = 0;
  for (long long i = 1; i <= n; i++) {
    long long tmp;
    cin >> tmp;
    a[i] = a[i - 1] + tmp;
  }
  for (long long i = 1; i <= m; i++) {
    long long tmp;
    cin >> tmp;
    b[i] = b[i - 1] + tmp;
  }
  int maxn = 0;
  int j = m;
  for (int i = 1; i <= n; i++) {
    for (; j >= 0; j--) {
      if (a[i] + b[j] > k)
        continue;
      else {
        maxn = max(maxn, i + j);
        break;
      }
    }
  }
  cout << maxn;
  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 v1 = vector<int>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

int binary(vector<ll> &b, int left, int right, ll target) {
  while (left + 1 < right) {
    int mid = (right + left) / 2;
    if (target > b.at(mid)) {
      left = mid;
    } else if (target == b.at(mid)) {
      return mid;
    } else {
      right = mid;
    }
  }
  return left;
}

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  a.at(0) = 0;
  rep(i, n) { cin >> a.at(i + 1); }
  b.at(0) = 0;
  rep(i, m) {
    cin >> b.at(i + 1);
    if (i >= 1)
      b.at(i) += b.at(i - 1);
  }
  int bookmax = 0;
  rep(i, n + 1) {
    k -= a.at(i);
    if (k < 0)
      break;
    int bc = binary(b, 0, m + 1, k);
    int n_book = i + bc;
    if (bookmax < n_book)
      bookmax = n_book;
  }
  cout << bookmax << 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 v1 = vector<int>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

int Book = 0;
ll K;
int N, M;

void bookcount(ll *a, ll *b, int ai, int bi, ll time, int bookc) {
  if (time > K) {
    bookc--;
    if (Book < bookc)
      Book = bookc;
    return;
  } else if (time == K) {
    if (Book < bookc)
      Book = bookc;
    return;
  }
  rep(i, 2) { // ０がa１がb
    if (i == 0) {
      if (ai == N)
        continue;
      ll n_time = time + a[ai];
      int n_ai = ai + 1;
      int n_bookc = bookc + 1;
      bookcount(a, b, n_ai, bi, n_time, n_bookc);
    } else if (i == 1) {
      if (bi == M)
        continue;
      ll n_time = time + b[bi];
      int n_bi = bi + 1;
      int n_bookc = bookc + 1;
      bookcount(a, b, ai, n_bi, n_time, n_bookc);
    }
  }
}

int main() {
  cin >> N >> M >> K;
  ll a[200001];
  ll b[200001];
  rep(i, N) {
    ll x;
    cin >> x;
    a[i] = x;
  }
  rep(i, M) {
    ll x;
    cin >> x;
    b[i] = x;
  }
  bookcount(a, b, 0, 0, 0, 0);
  cout << Book << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

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

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

  vector<long long int> Asum(N + 1);
  vector<long long int> Bsum(M + 1);
  Asum[0] = 0;
  for (int i = 1; i <= N; i++)
    Asum[i] = A[i - 1] + Asum[i - 1];
  Bsum[0] = 0;
  for (int i = 1; i <= M; i++)
    Bsum[i] = B[i - 1] + Bsum[i - 1];

  int res = 0;
  for (int i = 0; i <= N; i++) {
    if (Asum[i] > K) {
      break;
    }

    long long int rest = K - Asum[i];
    auto it = upper_bound(Bsum.cbegin(), Bsum.cend(), rest);
    int tmp = i + distance(Bsum.cbegin(), it) - 1;
    if (tmp > res)
      res = tmp;
  }
  cout << res << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

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

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

  vector<long long int> Asum(N + 1);
  vector<long long int> Bsum(M + 1);
  Asum[0] = 0;
  for (int i = 1; i <= N; i++)
    Asum[i] = A[i - 1] + Asum[i - 1];
  Bsum[0] = 0;
  for (int i = 1; i <= M; i++)
    Bsum[i] = B[i - 1] + Bsum[i - 1];

  int res = 0;
  for (int i = 0; i <= N; i++) {
    if (Asum[i] > K) {
      break;
    }

    long long int rest = K - Asum[i];
    auto it = upper_bound(Bsum.cbegin(), Bsum.cend(), rest);
    int tmp = i + distance(Bsum.cbegin(), it) - 1;
    if (tmp > res)
      res = tmp;
  }
  cout << res << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> N(n + 1), M(m + 10);
  int64_t a = 0, b = 0;

  N.at(0) = 0;
  M.at(0) = 0;
  int64_t sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a);
    // cin >> a;
    sum += a;
    N.at(i) = sum;
  }

  sum = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%lld", &b);
    // cin >> b;
    sum += b;
    M.at(i) = sum;
  }

  int64_t tmpmax = 0;
  int64_t j = m;
  for (int64_t i = 0; i <= n; i++) {
    if (N.at(i) > k) {
      break;
    }
    while (M.at(j) > k - N.at(i)) {
      j--;
    }
    tmpmax = max(tmpmax, i + j);
  }

  cout << tmpmax << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> N(n + 1), M(m + 10);
  int64_t a = 0, b = 0;

  N.at(0) = 0;
  M.at(0) = 0;
  int64_t sum = 0;
  for (int i = 1; i <= n; i++) {
    // scanf("%lld", &a);
    cin >> a;
    sum += a;
    N.at(i) = sum;
  }

  sum = 0;
  for (int i = 1; i <= m; i++) {
    // scanf("%lld", &b);
    cin >> b;
    sum += b;
    M.at(i) = sum;
  }

  int64_t tmpmax = 0;
  int64_t j = m;
  for (int64_t i = 1; i <= n + 1; i++) {
    if (N.at(i) > k) {
      break;
    }
    while (M.at(j) > k - N.at(i)) {
      j--;
    }
    tmpmax = max(tmpmax, i + j);
  }

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

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

#define int long long
#define pb push_back
#define ff first
#define ss second

bool util(vector<int> &a, vector<int> &b, int n, int m, int mid, int k) {
  if (mid - 1 < n && a[mid - 1] <= k)
    return 1;
  if (mid - 1 < m && b[mid - 1] <= k)
    return 1;
  for (int i = 0; i < mid; i++) {
    if (i - 1 >= 0 && mid - 1 - 1 >= 0 && i - 1 < n && mid - i - 1 < m &&
        a[i - 1] + b[mid - i - 1] <= k)
      return 1;
  }
  return 0;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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

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

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

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

  int l = 0, r = m + n + 1;

  while (l <= r) {
    int mid = l + (r - l) / 2;
    mid = l + (r - l) / 2;

    if (util(a, b, n, m, mid, k)) {
      ans = max(ans, mid);
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }

  cout << ans;
}

Error: WA

Fixed submission:

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

bool util(long long int *a, long long int *b, long long int mid,
          long long int n, long long int m, long long int k) {
  if (mid - 1 < n && mid - 1 >= 0 && a[mid - 1] <= k) {
    return 1;
  }

  if (mid - 1 < m && mid - 1 >= 0 && b[mid - 1] <= k) {
    return 1;
  }

  for (int i = 0; i < mid - 1; i++) {
    if (i < n && mid - i - 2 < m && mid - i - 2 >= 0 && i >= 0 &&
        a[i] + b[mid - i - 2] <= k) {
      return 1;
    }
  }
  return 0;
}

int main() {
  long long int n, m, k, ans = 0;
  cin >> n >> m >> k;

  long long int a[n], b[n];

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

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

  long long int l = 0, r = m + n, mid;

  while (l <= r) {
    mid = l + (r - l) / 2;

    if (util(a, b, mid, n, m, k)) {
      ans = max(ans, mid);
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }

  cout << ans;

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

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

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

  vector<ll> aTime(n + 1);
  aTime[0] = 0;
  for (int i = 1; i < aTime.size(); i++) {
    aTime[i] = aTime[i - 1] + a[i - 1];
  }

  vector<ll> bTime(m + 1);
  bTime[0] = 0;
  for (int i = 1; i < bTime.size(); i++) {
    bTime[i] = bTime[i - 1] + b[i - 1];
  }

  ll count = 0;
  ll timeBest = 0;
  ll lastJ = bTime.size() - 1;
  for (int i = 0; i < aTime.size(); i++) {
    for (int j = lastJ; j >= 0; j--) {
      ll time = aTime[i] + bTime[j];
      if (time <= k) {
        ll c = i + j;
        if (count < c) {
          count = c;
        }
        lastJ = j;
        break;
      }
    }
  }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<ll> aTime(n + 1);
  aTime[0] = 0;
  for (int i = 1; i < aTime.size(); i++) {
    aTime[i] = aTime[i - 1] + a[i - 1];
  }

  vector<ll> bTime(m + 1);
  bTime[0] = 0;
  for (int i = 1; i < bTime.size(); i++) {
    bTime[i] = bTime[i - 1] + b[i - 1];
  }

  ll count = 0;
  ll timeBest = 0;
  for (int i = 0; i < aTime.size(); i++) {
    for (int j = bTime.size() - 1; j >= 0; j--) {
      ll time = aTime[i] + bTime[j];
      if (time <= k) {
        ll c = i + j;
        if (count < c) {
          count = c;
        }
        break;
      }
    }
  }

  cout << count << endl;

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

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

int main() {
  ll 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];
  ll s = accumulate(B.begin(), B.end(), 0LL);
  int j = M;
  int ans = 0;
  for (int i = 0; i <= N; ++i) {
    while (j > 0 && K < s) {
      --j;
      s -= B[j];
    }
    if (K >= s)
      ans = max(ans, i + j);
    if (i < N)
      s += A[i];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

ll N, M, K;
vector<ll> A, B;
ll cnt = 0;
int i = 0, j = 0;

bool rmA() {
  ll t = A[i];
  ++i;
  if (K - t < 0)
    return false;
  ++cnt;
  K -= t;
  return true;
}
bool rmB() {
  ll t = B[j];
  ++j;
  if (K - t < 0)
    return false;
  ++cnt;
  K -= t;
  return true;
}

int main() {
  cin >> N >> M >> K;
  for (int i = 0; i < N; ++i) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  for (int i = 0; i < M; ++i) {
    ll b;
    cin >> b;
    B.push_back(b);
  }
  while (!(i >= N && j >= M) && K >= 0) {
    if (i >= N) {
      if (!rmB())
        break;
      continue;
    }
    if (j >= N) {
      if (!rmA())
        break;
      continue;
    }
    if (A[i] == B[i]) {
      int i0 = i, j0 = j;
      while (i0 < N && j0 < M && A[i0] == B[j0]) {
        ++i0;
        ++j0;
      }
      if (i0 < N && j0 < M) {
        if (A[i0] < B[j0]) {
          while (i != i0) {
            if (!rmA())
              break;
          }
        } else {
          while (j != j0) {
            if (!rmB())
              break;
          }
        }
      } else {
        if (i0 >= N) {
          while (j != j0) {
            if (!rmB())
              break;
          }
          continue;
        } else {
          while (i != i0) {
            if (!rmA())
              break;
          }
        }
      }
    } else {
      if (A[i] < B[i]) {
        if (!rmA())
          break;
      } else {
        if (!rmB())
          break;
      }
    }
  }
  cout << cnt << 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)
#define REPR(i, n, m) for (int i = (n); i >= (m); --i)
using namespace std;
using LL = long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLD = vector<PLD>;

const int IINF = numeric_limits<int>::max();

int main() {

  LL n, m, k;
  cin >> n >> m >> k;
  VLL a(n + 1, 0), b(m + 1, 0);
  REP(i, n) {
    LL tmp;
    cin >> tmp;
    a[i + 1] = a[i] + tmp;
  }
  REP(i, m) {
    LL tmp;
    cin >> tmp;
    b[i + 1] = b[i] + tmp;
  }

  LL ans = 0;
  LL tmp_j = m;
  REP(i, n + 1) {
    if (a[i] > k)
      break;
    LL j = tmp_j;
    while (b[j] > k - a[i]) {
      j--;
    }
    ans = max(ans, i + j);
    tmp_j = j;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n, m) for (int i = (n); i >= (m); --i)
using namespace std;
using LL = long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLD = vector<PLD>;

const int IINF = numeric_limits<int>::max();

int main() {

  LL n, m, k;
  cin >> n >> m >> k;
  VLL a(n + 1, 0), b(m + 1, 0);
  REP(i, n) {
    LL tmp;
    cin >> tmp;
    a[i + 1] = a[i] + tmp;
  }
  REP(i, m) {
    LL tmp;
    cin >> tmp;
    b[i + 1] = b[i] + tmp;
  }

  LL ans = 0;
  LL tmp_j = m;
  REP(i, n + 1) {
    if (a[i] > k)
      break;
    LL j = tmp_j;
    while (b[j] > k - a[i]) {
      j--;
    }
    ans = max(ans, i + j);
    j = tmp_j;
  }

  cout << ans << endl;

  return 0;
}