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

/*note:- in first sight it seems that it will be solved with greddy techniqe
(but that is wrong as it will give wrong ans).*/
/*4 3 270
100 10 10 10
90 90 90
using greedy approach we will be able to read only three books in the above test
case(and it will consume entire 270 minutes) but actually we can read 4 books
(in just 130 minutes)
*/
/*
this approch uses calculation of prefix sum of both the arrays
refer editorial(and YT_vid) for more details
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll j = 0; j < m; j++)
    cin >> b[j];
  vector<ll> sumA(n + 1, 0);
  vector<ll> sumB(m + 1, 0);
  for (ll i = 0; i < n; i++)
    sumA[i + 1] = sumA[i] + a[i];
  for (ll j = 0; j < m; j++)
    sumB[j + 1] = sumB[j] + b[j];
  ll total = 0;
  for (ll i = 0; i <= n; i++) {
    ll other = k - sumA[i];
    if (other < 0)
      continue;
    ll l = lower_bound(sumB.begin(), sumB.end(), other) - sumB.begin();
    if (l > m || sumB[l] > other)
      l--;
    total = max(total, i + l);
  }
  cout << total;
  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:

/*note:- in first sight it seems that it will be solved with greddy techniqe
(but that is wrong as it will give wrong ans).*/
/*4 3 270
100 10 10 10
90 90 90
using greedy approach we will be able to read only three books in the above test
case(and it will consume entire 270 minutes) but actually we can read 4 books
(in just 130 minutes)
*/
/*
this approch uses calculation of prefix sum of both the arrays
refer editorial(and YT_vid) for more details
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll j = 0; j < m; j++)
    cin >> b[j];
  vector<ll> sumA(n + 1, 0);
  vector<ll> sumB(m + 1, 0);
  for (ll i = 0; i < n; i++)
    sumA[i + 1] = sumA[i] + a[i];
  for (ll j = 0; j < m; j++)
    sumB[j + 1] = sumB[j] + b[j];
  ll total = 0;
  for (ll i = 0; i <= n; i++) {
    ll other = k - sumA[i];
    if (other < 0)
      continue;
    ll l = lower_bound(sumB.begin(), sumB.end(), other) - sumB.begin();
    if (l > m || sumB[l] > other)
      l--;
    total = max(total, i + l);
  }
  cout << total;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;

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

  int pos = 0;
  int ans = 0;
  rep(i, n) { sum_a[i + 1] = sum_a[i] + a[i]; }
  rep(i, m) {
    sum_b[i + 1] = b[i] + sum_b[i];
    if (k < sum_b[i + 1] && pos == 0) {
      pos = i;
    }
  }
  if (pos == 0)
    pos = m;
  if (a[0] > k)
    pos = 0;
  ans = pos;

  reps(i, 1, n + 1) {
    ll tmp = k - sum_a[i];
    if (tmp < 0)
      break;
    while (tmp < sum_b[pos]) {
      if (pos > 0)
        pos--;
      else
        break;
    }

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

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


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;

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

  int pos = 0;
  int ans = 0;
  rep(i, n) { sum_a[i + 1] = sum_a[i] + a[i]; }
  rep(i, m) {
    sum_b[i + 1] = b[i] + sum_b[i];
    if (k < sum_b[i + 1] && pos == 0) {
      pos = i;
    }
  }
  if (pos == 0)
    pos = m;
  ans = pos;
  reps(i, 1, n + 1) {
    ll tmp = k - sum_a[i];
    if (tmp < 0)
      break;
    while (tmp < sum_b[pos]) {
      if (pos > 0)
        pos--;
      else
        break;
    }
    ans = max(ans, pos + i);
  }

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

#include <bits/stdc++.h>
using namespace std;
long n, m, k;
vector<long> a, b;
vector<long long> sumA, sumB;
long getB(long inA) {
  long long passedTime = 0;
  if (inA != 0)
    passedTime = sumA[inA - 1];
  long long remainTime = k - passedTime;
  long ret = 0;
  // for(long i = 0; i < m; i++){
  //     if(sumB[i]<=remainTime){
  //         ret++;
  //     }else{
  //         break;
  //     }
  // }
  decltype(sumB)::iterator it =
      upper_bound(sumB.begin(), sumB.end(), remainTime);
  long index = it - sumB.begin();
  ret = index;
  return ret;
}
bool isValidA(long inA) {
  if (inA == 0)
    return true;
  if (inA > n)
    return false;
  if (sumA[inA - 1] > k)
    return false; // thisPath"inA">0
  return true;
}
int main() {
  cin >> n >> m >> k;

  a = vector<long>(n);
  sumA = vector<long long>(n, 0);
  b = vector<long>(m);
  sumB = vector<long long>(m, 0);

  for (long i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      sumA[i] = a[i];
    } else {
      sumA[i] = sumA[i - 1] + a[i];
    }
  }
  for (long i = 0; i < m; i++) {
    cin >> b[i];
    if (i == 0) {
      sumB[i] = b[i];
    } else {
      sumB[i] = sumB[i - 1] + b[i];
    }
  }
  long ans = 0;
  for (long i = 0; i <= n; i++) {
    if (isValidA(i)) {
      long bnum = getB(i);
      ans = max(ans, bnum + i);
    } else {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long n, m, k;
vector<long> a, b;
vector<long long> sumA, sumB;
long getB(long inA) {
  long long passedTime = 0;
  if (inA != 0)
    passedTime = sumA[inA - 1];
  long long remainTime = k - passedTime;
  long ret = 0;
  for (long i = 0; i < m; i++) {
    if (sumB[i] <= remainTime) {
      ret++;
    } else {
      break;
    }
  }
  return ret;
}
bool isValidA(long inA) {
  if (inA == 0)
    return true;
  if (inA > n)
    return false;
  if (sumA[inA - 1] > k)
    return false; // thisPath"inA">0
  return true;
}
int main() {
  cin >> n >> m >> k;

  a = vector<long>(n);
  sumA = vector<long long>(n, 0);
  b = vector<long>(m);
  sumB = vector<long long>(m, 0);

  for (long i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      sumA[i] = a[i];
    } else {
      sumA[i] = sumA[i - 1] + a[i];
    }
  }
  for (long i = 0; i < m; i++) {
    cin >> b[i];
    if (i == 0) {
      sumB[i] = b[i];
    } else {
      sumB[i] = sumB[i - 1] + b[i];
    }
  }
  long ans = 0;
  for (long i = 0; i <= n; i++) {
    if (isValidA(i)) {
      long bnum = getB(i);
      ans = max(ans, bnum + i);
    } else {
      break;
    }
  }
  cout << ans << endl;
  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;
  vector<long long> A(N);
  vector<long long> B(M);
  vector<long long> C(N);
  vector<long long> D(M);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (i == 0) {
      C.at(i) = A.at(i);
    } else {
      C.at(i) += C.at(i - 1) + A.at(i);
    }
  }
  int counta = 0;
  for (int i = 0; i < N; i++) {
    C.at(i) = K - C.at(i);
    if (C.at(i) < 0) {
      break;
    }
    counta++;
  }

  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
    if (i == 0) {
      D.at(i) = B.at(i);
    } else {
      D.at(i) += D.at(i - 1) + B.at(i);
    }
  }
  int countb = 0;
  for (int i = 0; i < M; i++) {
    if (D.at(i) > K) {
      break;
    }
    countb++;
  }

  int MAX = max(counta, countb);
  for (int i = 0; i < counta; i++) {
    for (int j = countb - 1; j >= 0; j--) {
      if (C.at(i) >= D.at(j)) {
        if (i + j + 2 > MAX) {
          MAX = i + j + 2;
        }
        countb = j + 1;
        break;
      }
    }
  }
  cout << MAX << 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> A(N);
  vector<long long> B(M);
  vector<long long> C(N);
  vector<long long> D(M);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (i == 0) {
      C.at(i) = A.at(i);
    } else {
      C.at(i) += C.at(i - 1) + A.at(i);
    }
  }
  int counta = 0;
  for (int i = 0; i < N; i++) {
    C.at(i) = K - C.at(i);
    if (C.at(i) < 0) {
      break;
    }
    counta++;
  }

  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
    if (i == 0) {
      D.at(i) = B.at(i);
    } else {
      D.at(i) += D.at(i - 1) + B.at(i);
    }
  }
  int countb = 0;
  for (int i = 0; i < M; i++) {
    if (D.at(i) > K) {
      break;
    }
    countb++;
  }
  int MAX = max(counta, countb);
  for (int i = 0; i < counta; i++) {
    for (int j = countb - 1; j >= 0; j--) {
      if (C.at(i) >= D.at(j)) {
        if (i + j + 2 > MAX) {
          MAX = i + j + 2;
          countb = j + 1;
          break;
        }
      }
    }
  }
  cout << MAX << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

const int mxN = 2e5;
int n, m;
ll k;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);

  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a[i + 1] = a[i] + x;
  }

  for (ll i = 0; i < m; i++) {
    ll x;
    cin >> x;
    b[i + 1] = b[i] + x;
  }

  ll ans = 0;

  for (ll i = 0; i <= n; i++) {

    ll left = k - a[i];

    if (left < 0)
      continue;

    ll j = lower_bound(b.begin(), b.end(), left) - b.begin();

    if (j > m || b[j] > left) {
      j--;
    }

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

  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

/// https://codeforces.com/contest/400
// Matrix problems
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int mxN = 2e5;
int n, m;
ll k;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

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

  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a[i + 1] = a[i] + x;
  }

  for (ll i = 0; i < m; i++) {
    ll x;
    cin >> x;
    b[i + 1] = b[i] + x;
  }

  ll ans = 0;

  for (ll i = 0; i <= n; i++) {

    ll left = k - a[i];

    if (left < 0)
      continue;

    ll j = lower_bound(b.begin(), b.end(), left) - b.begin();

    if (j > m || b[j] > left) {
      j--;
    }

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

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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const double PI = acos(-1);
const ll MOD = 1000000007;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N);
  vector<ll> A_sum(N + 1);
  vector<ll> B(M);
  vector<ll> B_sum(M + 1);
  A_sum[0] = 0;
  B_sum[0] = 0;
  rep(i, N) {
    cin >> A[i];
    A_sum[i + 1] = A[i] + A_sum[i];
  }
  rep(i, M) {
    cin >> B[i];
    B_sum[i + 1] = B[i] + B_sum[i];
  }

  ll ans = 0;
  ll j = M;
  rep(i, N + 1) {
    if (K >= A_sum[i]) {
      ll nokori = K - A_sum[i];
      while (B_sum[j] > nokori) {
        j--;
      }
      ans = max(ans, i + j);
    } else
      break;
  }

  cout << ans << endl;
}


Error: 0

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>;
const double PI = acos(-1);
const ll MOD = 1000000007;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N);
  vector<ll> B(M);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];
  vector<vector<ll>> dp(K + 1, vector<ll>(3, 0));
  priority_queue<ll> q;

  q.push(0);
  if (A[0] < K + 1) {
    dp[A[0]][0]++;
    dp[A[0]][2]++;
  }
  if (B[0] < K + 1) {
    dp[B[0]][0]++;
    dp[B[0]][2]++;
  }

  rep(i, K + 1) {
    if (dp[i][0] == 0 && dp[i][1] == 0)
      continue;

    if (dp[i][0] < A.size()) {
      if (i + A[dp[i][0]] < K + 1 && dp[i][2] + 1 > dp[i + A[dp[i][0]]][2]) {
        dp[i + A[dp[i][0]]][0] = dp[i][0] + 1;
        dp[i + A[dp[i][0]]][1] = dp[i][1];
        dp[i + A[dp[i][0]]][2] = dp[i][2] + 1;
        q.push(dp[i + A[dp[i][0]]][2]);
      }
    }
    if (dp[i][1] < B.size()) {
      if (i + B[dp[i][1]] < K + 1 && dp[i][2] + 1 > dp[i + B[dp[i][1]]][2]) {
        dp[i + B[dp[i][1]]][0] = dp[i][0];
        dp[i + B[dp[i][1]]][1] = dp[i][1] + 1;
        dp[i + B[dp[i][1]]][2] = dp[i][2] + 1;
        q.push(dp[i + B[dp[i][1]]][2]);
      }
    }
  }

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

#include <bits/stdc++.h>
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define int long long
#define all(c) (c).begin(), (c).end()
#define endl '\\n'
using namespace std;

//                                          mdsaqib
signed main() {
  fast;
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> A(n, 0), B(m, 0);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> B[i];
  }
  vector<int> SA(n, 0), SB(m, 0);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += A[i];
    SA[i] = sum;
  }
  sum = 0;
  for (int i = 0; i < m; i++) {
    sum += B[i];
    SB[i] = sum;
  }
  int mx = 0;
  int ii = 0, jj = 0;
  for (int i = 0; i < n; i++) {
    if (SA[i] > k) {
      break;
    }
    ii = i + 1;
  }
  mx = ii;
  if (ii > 0) {
    for (int i = ii - 1; i >= 0; i--) {
      for (int j = jj; j < m; j++) {
        if (SA[i] + SB[j] > k) {
          break;
        }
        jj = j + 1;
      }
      mx = max(mx, i + jj + 1);
    }
  }
  jj = 0;
  for (int i = 0; i < m; i++) {
    if (SB[i] > k) {
      break;
    }
    jj = i + 1;
  }
  mx = max(mx, jj);
  cout << mx;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define int long long
#define all(c) (c).begin(), (c).end()
#define endl '\\n'
using namespace std;

//                                          mdsaqib
signed main() {
  fast;
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> A(n, 0), B(m, 0);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> B[i];
  }
  vector<int> SA(n, 0), SB(m, 0);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += A[i];
    SA[i] = sum;
  }
  sum = 0;
  for (int i = 0; i < n; i++) {
    sum += B[i];
    SB[i] = sum;
  }
  int mx = 0;
  int ii = 0, jj = 0;
  for (int i = 0; i < n; i++) {
    if (SA[i] > k) {
      break;
    }
    ii = i + 1;
  }
  mx = ii;
  if (ii > 0) {
    for (int i = ii - 1; i >= 0; i--) {
      for (int j = jj; j < m; j++) {
        if (SA[i] + SB[j] > k) {
          break;
        }
        jj = j + 1;
      }
      mx = max(mx, i + jj + 1);
    }
  }
  jj = 0;
  for (int i = 0; i < m; i++) {
    if (SB[i] > k) {
      break;
    }
    jj = i + 1;
  }
  mx = max(mx, jj);
  cout << mx;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int i, ans, ia, ib = 0;
  ll n, m, k, A, B;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  for (i = 1; i < n + 1; ++i) {
    cin >> A;
    a.at(i) = a.at(i - 1) + A;
  }
  for (i = 1; i < m + 1; ++i) {
    cin >> B;
    b.at(i) = b.at(i - 1) + B;
  }
  for (i = n; i >= 0; --i) {
    if (a.at(i) <= k) {
      ans = i;
      ia = i;
      break;
    }
  }
  while (ia >= 0) {
    while (a.at(ia) + b.at(ib) <= k) {
      ++ib;
      if (ib > m)
        break;
    }
    --ib;
    ans = max(ans, ia + ib);
    --ia;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int i, ans, ia, ib = 0;
  ll n, m, k, A, B;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  for (i = 1; i < n + 1; ++i) {
    cin >> A;
    a.at(i) = a.at(i - 1) + A;
  }
  for (i = 1; i < m + 1; ++i) {
    cin >> B;
    b.at(i) = b.at(i - 1) + B;
  }
  for (i = n; i > 0; --i) {
    if (a.at(i) <= k) {
      ans = i;
      ia = i;
      break;
    }
  }
  while (ia >= 0) {
    while (a.at(ia) + b.at(ib) <= k) {
      ++ib;
      if (ib > m)
        break;
    }
    --ib;
    ans = max(ans, ia + ib);
    --ia;
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const unsigned int M = 1000000007;
int main() {
  int n, m;
  long long k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);
  long long t = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    t += b[i];
  }
  int ans = 0;
  int j = m;
  for (int i = 0; i <= n; i++) {
    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: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const unsigned int M = 1000000007;
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> c(n + 1);
  vector<long long> d(n + 1);
  c[0] = 0;
  d[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i - 1];
    c[i] = c[i - 1] + a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i - 1];
    d[i] = d[i - 1] + b[i - 1];
  }
  int ans = 0;
  int j = m;
  for (int i = 0; i <= n; i++) {
    if (c[i] > k) {
      break;
    }
    while (c[i] + d[j] > k) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vll a(n), b(m);
  for (auto &x : a)
    cin >> x;
  for (auto &x : b)
    cin >> x;
  ll p = 0, t = 0;
  while (p < a.size() && t + a[p] <= k)
    t += a[p++];
  int p2 = 0;
  while (p2 < b.size() && t + b[p2] <= k)
    t += b[p2++];
  int ans = p + p2;
  ROF(i, p - 1, 0) {
    t -= a[i];
    while (p2 < b.size() && t + b[p2] <= k)
      t += b[p2++];
    ans = max(ans, i + p2);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vll a(n), b(m);
  for (auto &x : a)
    cin >> x;
  for (auto &x : b)
    cin >> x;
  ll p = 0, t = 0;
  while (p < a.size() && t + a[p] <= k)
    t += a[p++];
  int p2 = 0;
  while (p < b.size() && t + b[p2] <= k)
    t += b[p2++];
  int ans = p + p2;
  ROF(i, ans - 1, 0) {
    t -= a[i];
    while (p < b.size() && t + b[p2] <= k)
      t += b[p2++];
    ans = max(ans, i + p2);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vec a(n + 1), b(m + 1);
  for (ll i = 0; i < n; i++) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i + 1];
    b[i + 1] += b[i];
  }
  ll ok = 0, ng = n + m + 1;
  while (ok + 1 < ng) {
    ll mid = (ng + ok) >> 1;
    ll v = inf;
    for (ll i = max(0LL, mid - m); i <= min(n, mid); i++) {
      v = min(v, a[i] + b[mid - i]);
    }
    if (v <= k)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vec a(n + 1), b(m + 1);
  for (ll i = 0; i < n; i++) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i + 1];
    b[i + 1] += b[i];
  }
  ll ok = 0, ng = n + m + 1;
  while (ok + 1 < ng) {
    ll mid = (ng + ok) >> 1;
    ll v = inf;
    for (ll i = max(0LL, mid - m); i <= n; i++) {
      v = min(v, a[i] + b[mid - i]);
    }
    if (v <= k)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
}