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 < (n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using p = pair<int, int>;
using ll = long long;

int main() {
  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);

  ll t = 0;
  rep(i, m) t += b.at(i);

  int j = m;
  int ans = 0;
  rep(i, n + 1) {

    while (j > 0 && t > k) {
      --j;
      t -= b.at(j);
    }

    if (t > k)
      break;
    ans = max(ans, i + j);

    if (i == n)
      break;
    t += a.at(i);
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  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);

  ll t = 0;
  rep(i, m) t += b.at(i);

  int j = m;
  int ans = 0;
  rep(i, n + 1) {

    while (j > 0 && t > k) {
      --j;
      t -= b.at(i);
    }

    if (t > k)
      break;
    ans = max(ans, i + j);

    if (i == n)
      break;
    t += a.at(i);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
using LL = long long;

int main() {
  LL N, M, K;
  cin >> N >> M >> K;
  vector<LL> A(N + 1), B(M + 1), sumA(N + 1, 0), sumB(M + 1, 0);
  LL overK = -1;
  for (LL m = 1; m <= N; m++) {
    cin >> A[m];
    sumA[m] = sumA[m - 1] + A[m];
    if (sumA[m] > K && overK == -1) {
      overK = m - 1;
    }
  }
  if (overK == -1) {
    overK = N;
  }
  for (int m = 1; m <= M; m++) {
    cin >> B[m];
    sumB[m] = sumB[m - 1] + B[m];
  }
  int ans = 0;
  LL overK_b = 0;
  LL zenkai = overK;
  int j = M;
  for (int i = 0; i <= N; i++) {
    if (sumA[i] > K) {
      break;
    }
    LL temp = K - sumA[i];
    LL t = sumB[j];
    while (sumB[j] > K - sumA[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
using LL = long long;

int main() {
  LL N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N + 1), B(M + 1), sumA(N + 1, 0), sumB(M + 1, 0);
  LL overK = -1;
  for (LL m = 1; m <= N; m++) {
    cin >> A[m];
    sumA[m] = sumA[m - 1] + A[m];
    if (sumA[m] > K && overK == -1) {
      overK = m - 1;
    }
  }
  if (overK == -1) {
    overK = N;
  }
  for (int m = 1; m <= M; m++) {
    cin >> B[m];
    sumB[m] = sumB[m - 1] + B[m];
  }
  LL ans = 0;
  LL overK_b = 0;
  LL zenkai = overK;
  while (overK >= 0) {
    LL K_B = K - sumA[overK];
    LL temp = overK;
    while (true) {
      if (sumB[temp] > K_B) {
        overK_b = temp - 1;
        break;
      }
      if (temp >= M) {
        overK_b = M;
        break;
      }
      temp++;
    }
    LL konkai = overK + overK_b;
    zenkai = konkai;
    overK--;
    ans = max(ans, konkai);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#define For(i, x, y) for (int i = (x); i <= (y); i++)
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
ll a[maxn], b[maxn];
ll pre1[maxn], pre2[maxn];
int main() {
  int n, m, k, x, pos;
  int ans = 0;
  cin >> n >> m >> k;
  For(i, 1, n) cin >> a[i];
  For(i, 1, m) cin >> b[i];
  For(i, 1, n) pre1[i] = pre1[i - 1] + a[i];
  For(i, 1, m) pre2[i] = pre2[i - 1] + b[i];
  For(i, 0, n) {
    if (pre1[i] <= k) {
      x = k - pre1[i];
      pos = lower_bound(pre2 + 1, pre2 + 1 + m, x) - pre2;
      // cout<<i<<' '<<pos<<endl;
      // cout<<x<<' '<<b[pos]<<endl<<endl;
      if (pre2[pos] != x && pos > 0)
        pos--;
      ans = max(i + pos, ans);
    } else
      break;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#define For(i, x, y) for (int i = (x); i <= (y); i++)
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
ll a[maxn], b[maxn];
ll pre1[maxn], pre2[maxn];
int main() {
  int n, m, k, x, pos;
  int ans = 0;
  cin >> n >> m >> k;
  For(i, 1, n) cin >> a[i];
  For(i, 1, m) cin >> b[i];
  For(i, 1, n) pre1[i] = pre1[i - 1] + a[i];
  For(i, 1, m) pre2[i] = pre2[i - 1] + b[i];
  For(i, 1, n) {
    if (pre1[i] <= k) {
      x = k - pre1[i];
      pos = lower_bound(pre2 + 1, pre2 + 1 + m, x) - pre2;
      // cout<<i<<' '<<pos<<endl;
      // cout<<x<<' '<<b[pos]<<endl<<endl;
      if (pre2[pos] != x && pos != 0)
        pos--;
      ans = max(i + pos, ans);
    }
  }
  cout << ans << 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 < (int)(n); i++)
using ll = long long;

int main() {
  int N, M;
  ll K;
  cin >> N >> M >> K;
  vector<int> adesk(N + 1);
  vector<ll> bdesk(M + 1);
  ll sumb = 0;
  ll suma = 0;
  int ans = 0;

  rep(i, N) { cin >> adesk.at(i + 1); }
  rep(i, M) {
    int b;
    cin >> b;
    sumb += b;
    bdesk.at(i + 1) = sumb;
  }

  rep(i, N + 1) {
    suma += adesk.at(i);
    int left = -1, right = M + 1;
    if (suma > K)
      break;
    while (right - left > 1) {
      int mid = (left + right) / 2;
      if (bdesk.at(mid) <= (K - suma))
        left = mid;
      else
        right = mid;
    }
    ans = max(ans, i + left);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> adesk(N + 1);
  vector<int> bdesk(M + 1);
  ll sumb = 0;
  ll suma = 0;
  int ans = 0;

  rep(i, N) { cin >> adesk.at(i + 1); }
  rep(i, M) {
    int b;
    cin >> b;
    sumb += b;
    bdesk.at(i + 1) = sumb;
  }

  rep(i, N + 1) {
    suma += adesk.at(i);
    int left = -1, right = M + 2;
    if (suma > K)
      break;
    while (right - left > 1) {
      int mid = (left + right) / 2;

      if (bdesk.at(mid) <= (K - suma))
        left = mid;
      else
        right = mid;
    }
    ans = max(ans, i + left);
  }

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main(void) {
  int n, m, k, ans = 0, x = 0;
  cin >> n >> m >> k;
  int a[n], b[m];
  unsigned long sum = 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 y = m;
  while (k >= sum) {
    while (y >= 0) {
      if (bsum + sum > k) {
        y--;
        bsum = bsum - b[y];
      } else
        break;
    }
    ans = max({ans, x + y});
    if (x == n)
      break;
    sum += a[x];
    x++;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main(void) {
  int n, m, k, ans = 0, sum = 0, x = 0;
  cin >> n >> m >> k;
  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];

  while (k >= sum) {
    int mis = k - sum, y = 0;
    while (y < m) {
      if (mis - b[y] >= 0) {
        mis = mis - b[y];
        y++;
      } else
        break;
    }
    ans = max({ans, x + y});
    if (x == n)
      break;
    sum += a[x];
    x++;
  }

  cout << ans << endl;

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

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

int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N), B(M);
  long long A_sum = 0, B_sum = 0;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A[i] = a;
  }
  for (int i = 0; i < M; i++) {
    long long b;
    cin >> b;
    B[M - 1 - i] = b;
    B_sum += b;
  }
  long long j = M;
  long long ans = 0;
  for (long long i = 0; i <= N; i++) {
    while (A_sum + B_sum > K && j > 0) {
      j--;
      B_sum -= B[M - 1 - j];
    }
    if (A_sum + B_sum <= K) {
      ans = max(ans, i + j);
    }
    A_sum += A[i];
  }
  cout << ans << endl;

  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N), B(N);
  long long A_sum = 0, B_sum = 0;
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A[i] = a;
  }
  for (int i = 0; i < M; i++) {
    long long b;
    cin >> b;
    B[M - 1 - i] = b;
    B_sum += b;
  }
  long long j = M;
  long long ans = 0;
  for (long long i = 0; i <= N; i++) {
    while (A_sum + B_sum > K && j > 0) {
      j--;
      B_sum -= B[M - 1 - j];
    }
    if (A_sum + B_sum <= K) {
      ans = max(ans, i + j);
    }
    A_sum += A[i];
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, n) FOR(i, 0, n)
#define pb push_back
#define en "\\n"
#define ITER(i, a) for (typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define mod 1000000007
#define mpp make_pair
#define mes(a, b) memset((a),(b),sizeof((a))
typedef map<int, int> MI;
typedef pair<int, int> PI;
typedef vector<int> VI;
typedef long long int LL;
int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long int n, m, x;
  cin >> n >> m >> x;
  long long int a[n], b[m];
  for (long long int i = 0; i < n; i++)
    cin >> a[i];
  for (long long int i = 0; i < m; i++)
    cin >> b[i];

  long long int k = 0, i = 0;
  while (i < n && k + a[i] <= x) {
    k = k + a[i];
    i++;
  }
  long long int ans = i, j = 0;

  long long int sum = k;
  while (j < m && i >= 0) {
    sum = sum + b[j];
    j++;
    while (sum > x && i > 0) {
      i--;
      sum = sum - a[i];
    }
    if (sum <= x && i + j > ans)
      ans = (i + j);
  }
  cout << ans << en;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, n) FOR(i, 0, n)
#define pb push_back
#define en "\\n"
#define ITER(i, a) for (typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define mod 1000000007
#define mpp make_pair
#define mes(a, b) memset((a),(b),sizeof((a))
typedef map<int, int> MI;
typedef pair<int, int> PI;
typedef vector<int> VI;
typedef long long int LL;
int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m, x;
    cin >> n >> m >> x;
    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 k = 0, i = 0;
    while (i < n && k + a[i] <= x) {
      k = k + a[i];
      i++;
    }
    int ans = i, j = 0;

    int sum = k;
    while (j < m && i >= 0) {
      sum = sum + b[j];
      j++;
      while (sum > x && i > 0) {
        i--;
        sum = sum - a[i];
      }
      if (sum <= x && i + j > ans)
        ans = (i + j);
    }
    cout << ans << en;
  }
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define fors(i, s, e) for (ll i = s; i < e; i++)
#define endl "\\n"
#define zoom                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define PB push_back
#define MP make_pair
#define F first
#define S second

int main() {

  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n];
  ll b[m];
  forn(i, n) cin >> a[i];
  forn(i, m) cin >> b[i];

  ll i = 0;
  ll pre_a = 0;

  while ((pre_a + a[i]) <= k) {

    pre_a += a[i];
    if (i + 1 < n)
      i++;
    else {
      i++;
      break;
    }
  }

  ll maks = 0;
  ll ans = i;

  ll j = 0;
  while (i > (-1)) {

    while (pre_a + b[j] <= k) {
      pre_a += b[j];
      j++;
      ans++;
      if (j == m) {
        break;
      }
    }
    if (j == m) {
      maks = max(maks, ans);
      i = -1;
    } else {
      maks = max(maks, ans);
      ans--;
      pre_a = pre_a - a[i - 1];
      i--;
    }
    //	cout << maks << endl ;
  }
  cout << maks << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define fors(i, s, e) for (ll i = s; i < e; i++)
#define endl "\\n"
#define zoom                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define PB push_back
#define MP make_pair
#define F first
#define S second

int main() {

  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n];
  ll b[m];
  forn(i, n) cin >> a[i];
  forn(i, m) cin >> b[i];

  ll i = 0;
  ll pre_a = 0;

  while ((pre_a + a[i]) <= k) {

    pre_a += a[i];
    if (i + 1 < n)
      i++;
    else {
      i++;
      break;
    }
  }
  cout << pre_a << endl;

  ll maks = 0;
  ll ans = i;
  cout << ans << endl;
  ll j = 0;
  while (i > (-1)) {

    while (pre_a + b[j] <= k) {
      pre_a += b[j];
      j++;
      ans++;
      if (j == m) {
        break;
      }
    }
    if (j == m) {
      maks = max(maks, ans);
      i = -1;
    } else {
      maks = max(maks, ans);
      ans--;
      pre_a = pre_a - a[i - 1];
      i--;
    }
    //	cout << maks << endl ;
  }
  cout << maks << endl;
  ll sum = 0;
  forn(i, 177091) { sum += a[i]; }
  cout << sum << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  vector<llint> asum(n + 1);
  vector<llint> bsum(m + 1);
  llint atmp, btmp;
  for (int in = 0; in < n; in++) {
    cin >> atmp;
    asum.at(in + 1) = asum.at(in) + atmp;
  }
  for (int im = 0; im < m; im++) {
    cin >> btmp;
    bsum.at(im + 1) = bsum.at(im) + btmp;
  }

  llint ans = 0, anstmp, bmax = m;
  for (llint in = 0; in <= n; in++) {
    for (llint ib = bmax; ib >= 0; ib--) {
      if (asum.at(in) + bsum.at(ib) <= k) {
        bmax = ib;
        anstmp = in + ib;
        if (ans < anstmp)
          ans = anstmp;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  vector<llint> asum(n + 1);
  vector<llint> bsum(m + 1);
  llint atmp, btmp;
  for (int in = 0; in < n; in++) {
    cin >> atmp;
    asum.at(in + 1) = asum.at(in) + atmp;
  }
  for (int im = 0; im < m; im++) {
    cin >> btmp;
    bsum.at(im + 1) = bsum.at(im) + btmp;
  }

  int ans = 0;
  for (int in = 0; in <= n; in++) {
    for (int im = 0; im <= m; im++) {
      if (asum.at(in) + bsum.at(im) <= k && ans <= in + im)
        ans = in + im;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main() {
#ifdef LOCAL_EXEC
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif // LOCAL_EXEC
  long long int n, i, j, k, m, ans = 0, a, b, mid;
  cin >> n >> m >> k;
  long long int A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }
  for (i = 1; i <= m; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }
  for (i = 1; i <= n; i++) {
    if (A[i] > k)
      break;
    a = 1;
    b = m;
    while (a <= b) {
      mid = a + (b - a) / 2;
      if (B[mid] == k - A[i]) {
        a = mid + 1;
        break;
      } else if (B[mid] > k - A[i])
        b = mid - 1;
      else
        a = mid + 1;
    }
    ans = max(ans, i + a - 1);
  }
  for (i = 1; i <= m; i++) {
    if (B[i] > k)
      break;
    a = 1;
    b = n;
    while (a <= b) {
      mid = a + (b - a) / 2;
      if (A[mid] == k - B[i]) {
        a = mid + 1;
        break;
      } else if (A[mid] > k - B[i])
        b = mid - 1;
      else
        a = mid + 1;
    }
    ans = max(ans, i + a - 1);
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main() {
#ifdef LOCAL_EXEC
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif // LOCAL_EXEC
  long long int n, i, j, k, m, ans = 0, a, b, mid;
  cin >> n >> m >> k;
  long long int A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }
  for (i = 1; i <= m; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }
  for (i = 1; i <= n; i++) {
    if (A[i] > k)
      break;
    a = 1;
    b = m;
    while (a <= b) {
      mid = a + (b - a) / 2;
      if (B[mid] == k - A[i]) {
        a = mid + 1;
        break;
      } else if (B[mid] > k - A[i])
        b = mid - 1;
      else
        a = mid + 1;
    }
    ans = max(ans, i + a - 1);
  }
  for (i = 1; i <= m; i++) {
    if (B[i] > k)
      break;
    a = 1;
    b = m;
    while (a <= b) {
      mid = a + (b - a) / 2;
      if (A[mid] == k - B[i]) {
        a = mid + 1;
        break;
      } else if (A[mid] > k - B[i])
        b = mid - 1;
      else
        a = mid + 1;
    }
    ans = max(ans, i + a - 1);
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int 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];

  ll A[n + 1];
  ll B[m + 1];
  A[0] = 0;
  B[0] = 0;
  for (ll i = 1; i <= n; i++)
    A[i] = A[i - 1] + a[i - 1];

  for (ll i = 1; i <= m; i++)
    B[i] = B[i - 1] + b[i - 1];

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

  long long int count = 0;
  long long int sum = 0;
  while (sum <= k && a.size() > 0 && b.size() > 0) {
    if (a.size() > 0 && b.size() > 0 && a[0] < b[0]) {

      sum += a[0];
      if (sum > k)
        break;
      count++;
      a.erase(a.begin());
    } else if (a.size() > 0 && b.size() > 0 && a[0] > b[0]) {
      sum += b[0];
      if (sum > k)
        break;
      count++;
      b.erase(b.begin());
    }
  }
  if (b.size() == 0) {
    while (a.size() > 0 && sum <= k) {
      sum += a[0];
      if (sum > k)
        break;
      count++;
      a.erase(a.begin());
    }
  }

  if (a.size() == 0) {
    while (b.size() > 0 && sum <= k) {
      sum += b[0];
      if (sum > k)
        break;
      b.erase(b.begin());
      count++;
    }
  }

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

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

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

  vector<ll> a(n);
  vector<ll> b(m);

  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  vector<ll> sum_a(n + 1, 0);
  vector<ll> sum_b(m + 1, 0);

  ll a_max = n;
  ll b_max = m;
  for (int i = 1; i <= n; i++)
    sum_a[i] = sum_a[i - 1] + a[i - 1];
  for (int i = 1; i <= m; i++)
    sum_b[i] = sum_b[i - 1] + b[i - 1];

  for (int i = 1; i <= n; i++)
    if (sum_a[i] > k)
      a_max = i - 1;
  for (int i = 1; i <= m; i++)
    if (sum_b[i] > k)
      b_max = i - 1;

  if (sum_a[n] + sum_b[m] <= k) {
    cout << m + n << endl;
    return 0;
  }

  ll ans = 0;
  ll sb = 0;
  for (ll i = a_max; i >= 0; i--) {
    for (ll j = sb; j <= b_max; j++) {
      if (sum_a[i] + sum_b[j] > k) {
        sb = j;
        break;
      }
      ans = max(ans, i + j);
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  vector<ll> a(n);
  vector<ll> b(m);

  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  vector<ll> sum_a(n + 1, 0);
  vector<ll> sum_b(m + 1, 0);

  ll a_max = n;
  ll b_max = m;
  for (int i = 1; i <= n; i++) {
    sum_a[i] = sum_a[i - 1] + a[i - 1];
    if (sum_a[i] > k) {
      a_max = i - 1;
      break;
    }
  }
  for (int i = 1; i <= m; i++) {
    sum_b[i] = sum_b[i - 1] + b[i - 1];
    if (sum_b[i] > k) {
      b_max = i - 1;
      break;
    }
  }

  ll ans = 0;
  ll sb = 0;
  for (ll i = a_max; i >= 0; i--) {
    for (ll j = sb; j <= b_max; j++) {
      if (sum_a[i] + sum_b[j] > k) {
        sb = j;
        break;
      }
      ans = max(ans, i + j);
    }
  }

  cout << ans << endl;
}