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

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

void readVec(vector<ll> &v, int n) {
  for (int i = 0; i < n; ++i)
    cin >> v[i];
}

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

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

  readVec(a, n);
  readVec(b, m);

  for (int i = 1; i < m; ++i) {
    b[i] += b[i - 1];
    // cout << b[i] << endl;
  }

  int count = upper_bound(b.begin(), b.end(), k) - b.begin();
  assert(count >= 0);
  for (int i = 0; i < n; ++i) {
    k -= a[i];
    if (k >= 0) {
      int picksFromB = upper_bound(b.begin(), b.end(), k) - b.begin();
      assert(picksFromB >= 0);
      count = max(count, i + 1 + picksFromB);
    } else {
      break;
    }
  }

  cout << count;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

void readVec(vector<ll> &v, int n) {
  for (int i = 0; i < n; ++i)
    cin >> v[i];
}

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

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

  readVec(a, n);
  readVec(b, m);

  for (int i = 1; i < n; ++i) {
    b[i] += b[i - 1];
  }

  int count = upper_bound(b.begin(), b.end(), k) - b.begin();
  assert(count >= 0);

  for (int i = 0; i < n; ++i) {
    k -= a[i];
    if (k >= 0) {
      int picksFromB = upper_bound(b.begin(), b.end(), k) - b.begin();
      assert(picksFromB >= 0);
      count = max(count, i + 1 + picksFromB);
    } else {
      break;
    }
  }

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

#include <bits/stdc++.h>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const long double PI = acos(-1);
// }}} End Header
int main() {
  int 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];
  ll t = 0;
  rep(i, m) t += b[i];
  int j = m;
  int ans = 0;
  rep(i, n + 1) {
    while (j > 0 && t > k) {
      --j;
      t -= b[j];
    }
    if (t > k)
      break;
    if (ans < i + j)
      ans = i + j;
    // ans = max(ans, i+j);
    if (i >= n)
      break;
    t += 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>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const long double PI = acos(-1);
// }}} End Header
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll t = 0;
  rep(i, m) t += b[i];
  int j = m;
  int ans = 0;
  rep(i, n + 1) {
    while (j > 0 && t > k) {
      --j;
      t -= b[j];
    }
    if (t > k)
      break;
    if (ans < i + j)
      ans = i + j;
    // ans = max(ans, i+j);
    if (i >= n)
      break;
    t += a[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;

  long long int a[n];
  long long int 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 int> prea(n + 1, 0);
  vector<long long int> preb(m + 1, 0);

  for (int i = 0; i < n; i++) {
    prea[i + 1] = prea[i] + a[i];
  }

  for (int i = 0; i < m; i++) {
    preb[i + 1] = preb[i] + b[i];
  }

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    long long int left = k - prea[i];
    if (left < 0)
      continue;

    int searchb = lower_bound(preb.begin(), preb.end(), left) - preb.begin();
    if (searchb > m || preb[searchb] > left)
      searchb--;

    ans = max(ans, i + searchb);
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;

  int a[n];
  int b[m];

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

  vector<int> prea(n + 1, 0);
  vector<int> preb(m + 1, 0);

  for (int i = 0; i < n; i++) {
    prea[i + 1] = prea[i] + a[i];
  }

  for (int i = 0; i < n; i++) {
    preb[i + 1] = preb[i] + b[i];
  }

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    int left = k - prea[i];
    if (left < 0)
      continue;

    int searchb = lower_bound(preb.begin(), preb.end(), left) - preb.begin();
    if (searchb > m || preb[searchb] > left)
      searchb--;

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

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  long long apsum[200001] = {0}, bpsum[200001] = {0};
  for (int i = 1; i <= n; i++) {
    cin >> apsum[i];
    apsum[i] += apsum[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> bpsum[i];
    bpsum[i] += bpsum[i - 1];
  }
  long long ans = -1e9, j = m;
  for (int i = 0; i <= n; i++) {
    if (apsum[i] > k) {
      break;
    }
    while (bpsum[j] > k - apsum[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  long long apsum[200001] = {0}, bpsum[200001] = {0};
  for (int i = 1; i <= n; i++) {
    cin >> apsum[i];
    apsum[i] += apsum[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> bpsum[i];
    bpsum[i] += bpsum[i - 1];
  }
  long long ans = -1e9, best = m;
  for (int i = 0; i <= n; i++) {
    for (int j = best; j >= 0; j--) {
      if (i + j > ans && apsum[i] + bpsum[j] <= k) {
        ans = i + j;
        best = j;
        break;
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  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 i = 0; i < M; i++)
    cin >> B[i];

  int A_nBook = 0;

  unsigned long long A_time = 0;
  unsigned long long B_time = 0;

  int B_nBook = 0;

  for (int j = 0; j < M; j++) {
    B_time += B[j];
    //      cout << B_time << " " << B_nBook << endl;
    if (B_time > K) {
      B_time -= B[j];
      break;
    } else {
      B_nBook++;
    }
  }

  int max_nBook = B_nBook;

  while (1) {
    unsigned long long time = 0;

    if (A_nBook > 0)
      A_time += A[A_nBook - 1];
    if (A_time > K)
      break;

    if (A_time + B_time > K) {
      while (1) {
        B_time -= B[B_nBook - 1];
        B_nBook--;
        if (A_time + B_time <= K)
          break;
      }
    }

    //    cout << A_nBook <<" "<< B_nBook <<endl;
    if (A_nBook + B_nBook > max_nBook) {
      max_nBook = A_nBook + B_nBook;
    }

    if (A_nBook == N) {
      break;
    } else {
      A_nBook++;
    }
  }
  cout << max_nBook << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  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 i = 0; i < M; i++)
    cin >> B[i];

  int max_nBook = 0;
  int A_nBook = 0;

  while (1) {
    unsigned long long time = 0;
    int B_nBook = 0;

    for (int i = 0; i < A_nBook; i++)
      time += A[i];
    if (time > K)
      break;

    for (int j = 0; j < M; j++) {
      time += B[j];
      if (time > K) {
        break;
      } else {
        B_nBook++;
      }
    }

    if (A_nBook + B_nBook > max_nBook) {
      //      cout << A_nBook <<" "<< B_nBook <<endl;
      max_nBook = A_nBook + B_nBook;
    }

    if (A_nBook == N) {
      break;
    } else {
      A_nBook++;
    }
  }
  cout << max_nBook << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

bool check(unsigned long long middle, std::vector<unsigned long long> &A,
           std::vector<unsigned long long> &B, unsigned long long k,
           unsigned long long n, unsigned long long m) {
  unsigned long long time = 4294967295;
  for (unsigned long long i = 0; i <= n; i++) {
    if (middle - i <= m)
      time = std::min(time, A[i] + B[middle - i]);
    if (time <= k)
      return true;
  }
  if (time > k)
    return false;
  return true;
}

int main() {
  unsigned long long n, m, k;
  std::cin >> n >> m >> k;
  std::vector<unsigned long long> A(n + 1);
  std::vector<unsigned long long> B(m + 1);
  A[0] = 0;
  B[0] = 0;
  for (int i = 1; i < n + 1; i++) {
    unsigned long long a;
    std::cin >> a;
    A[i] = a + A[i - 1];
  }
  for (int i = 1; i < m + 1; i++) {
    unsigned long long b;
    std::cin >> b;
    B[i] = b + B[i - 1];
  }
  unsigned long long bottom = 0;
  unsigned long long top = m + n;
  while (bottom + 1 < top) {
    unsigned long long middle = (bottom + top) / 2;
    if (check(middle, A, B, k, n, m))
      bottom = middle;
    else
      top = middle;
  }
  if (check(top, A, B, k, n, m))
    std::cout << top << '\\n';
  else
    std::cout << bottom << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

bool check(long long middle, std::vector<long long> &A,
           std::vector<long long> &B, long long k, long long n, long long m) {
  long long time = 4294967295;
  for (long long i = 0; i <= n; i++) {
    if (middle - i <= m)
      time = std::min(time, A[i] + B[middle - i]);
    if (time <= k)
      return true;
  }
  if (time > k)
    return false;
  return true;
}

int main() {
  long long n, m, k;
  std::cin >> n >> m >> k;
  std::vector<long long> A(n + 1);
  std::vector<long long> B(m + 1);
  A[0] = 0;
  B[0] = 0;
  for (int i = 1; i < n + 1; i++) {
    long long a;
    std::cin >> a;
    A[i] = a + A[i - 1];
  }
  for (int i = 1; i < m + 1; i++) {
    long long b;
    std::cin >> b;
    B[i] = b + B[i - 1];
  }
  long long bottom = 0;
  long long top = m + n;
  while (bottom + 1 < top) {
    long long middle = (bottom + top) / 2;
    if (check(middle, A, B, k, n, m))
      bottom = middle;
    else
      top = middle;
  }
  if (check(top, A, B, k, n, m))
    std::cout << top << '\\n';
  else
    std::cout << bottom << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

inline void Main() {
  int N, M;
  long long K;
  cin >> N >> M >> K;

  vector<long long> A(N + 1, 0);
  repi(i, 1, N + 1) {
    cin >> A[i];
    A[i] += A[i - 1];
  }

  vector<long long> B(M + 1, 0);
  repi(i, 1, M + 1) {
    cin >> B[i];
    B[i] += B[i - 1];
  }

  int r = 0, j = M;
  rep(i, N + 1) {
    if (A[i] > K)
      break;
    while (B[j] > K - A[i]) {
      j -= 1;
    }
    // cout << i << " , " << j << endl;
    r = max(i + j, r);
  }

  // rep(i, N) { cout << A[i] << " "; } cout << endl;
  // rep(i, M) { cout << B[i] << " "; } cout << endl;

  cout << r << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(20);
  Main();
  return 0;
}


Error: 0

Fixed submission:

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

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

inline void Main() {
  int N, M, K;
  cin >> N >> M >> K;

  vector<long long> A(N, 0), A_SUM(N, 0);
  rep(i, N) {
    cin >> A[i];
    if (i == 0)
      A_SUM[i] = A[i];
    else
      A_SUM[i] = A[i] + A_SUM[i - 1];
  }

  vector<long long> B(M, 0), B_SUM(M, 0);
  rep(i, N) {
    cin >> B[i];
    if (i == 0)
      B_SUM[i] = B[i];
    else
      B_SUM[i] = B[i] + B_SUM[i - 1];
  }

  int R = 0;
  rep(i, N) {
    auto TARGET_IT = upper_bound(all(B_SUM), K - A_SUM[i]);
    int TARGET_POS = distance(B_SUM.begin(), TARGET_IT) - 1;
    if (TARGET_POS >= 0) {
      int S = (i + 1) + (TARGET_POS + 1);
      R = max(R, S);
    }
  }

  cout << R << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(20);
  Main();
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> A(n), B(m);
  for (int i = 0; i < n; i++)
    cin >> A.at(i);
  for (int i = 0; i < m; i++)
    cin >> B.at(i);
  int i, j, sum = 0;
  for (i = 0; i < n; i++) {
    sum += A.at(i);
    if (sum > k) {
      sum -= A.at(i);
      break;
    }
    // i冊の本が読める
  }
  int maxij = i;
  for (j = 0; j < m; j++) {
    sum += B.at(j);
    while (sum > k && i > 0) {
      i--;
      sum -= A.at(i);
    }
    if (sum > k) {
      break;
    }

    if (maxij < i + j + 1) {
      maxij = i + j + 1;
    }
  }

  cout << maxij << endl;
}


Error: WA

Fixed submission:

#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.at(i);
  for (int i = 0; i < m; i++)
    cin >> B.at(i);
  long long i, sum = 0;
  for (i = 0; i < n; i++) {
    sum += A.at(i);
    if (sum > k) {
      break;
    }
  }
  long long maxi = i;
  long long j;
  for (j = 0; j < m; j++) {
    sum += B.at(j);
    while (sum > k and i >= 0) {
      sum -= A.at(i);
      i--;
    }
    if (sum > k) {
      i++;
      break;
    }
    if (maxi < i + j) {
      maxi = i + j;
    }
  }
  cout << i + j;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define INF 1e9
#define ALL(v) v.begin(), v.end()
#include <algorithm>

using namespace std;
typedef long long ll;

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

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

  vector<ll> aSum(n + 1, 0);
  vector<ll> bSum(m + 1, 0);
  aSum[0] = bSum[0] = 0;

  FOR(i, 1, n) {
    cin >> a[i];
    aSum[i] = aSum[i - 1] + a[i];
  }
  FOR(i, 1, m) {
    cin >> b[i];
    bSum[i] = bSum[i - 1] + b[i];
  }

  ll maxBooks = 0;
  ll j = m;

  FOR(i, 0, n) {
    if (aSum[i] > k)
      break;

    while (j >= 0 && aSum[i] + bSum[j] > k)
      j--;
    maxBooks = max(maxBooks, i + j);
  }

  cout << maxBooks << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define INF 1e9
#define ALL(v) v.begin(), v.end()
#include <algorithm>

using namespace std;
typedef long long ll;

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

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

  vector<ll> aSum(n + 1, 0);
  vector<ll> bSum(m + 1, 0);
  aSum[0] = bSum[0] = 0;

  FOR(i, 1, n) {
    cin >> a[i];
    aSum[i] = aSum[i - 1] + a[i];
  }
  FOR(i, 1, m) {
    cin >> b[i];
    bSum[i] = bSum[i - 1] + b[i];
  }

  ll maxBooks = 0;

  FOR(i, 0, n) {
    if (aSum[i] > k)
      break;
    ll j = m;

    while (j >= 0 && aSum[i] + bSum[j] > k)
      j--;
    maxBooks = max(maxBooks, i + j);
  }

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

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

#define ms(s, n) memset(s, n, sizeof(s))
#define all(a) a.begin(), a.end()
#define present(t, x) (t.find(x) != t.end())
#define sz(a) int((a).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;

const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 1;
inline ll gcd(ll a, ll b) {
  ll r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll n, m, k;

void inp() {
  cin >> n >> m >> k;
  int a[n], b[m], ans = 0, j = 0;
  ll suma = 0, sumb = 0;
  FOR(i, 0, n) {
    cin >> a[i];
    suma += a[i];
  }
  FOR(i, 0, m)
  cin >> b[i];
  FORd(i, n + 1, 0) {
    if (i < n)
      suma -= a[i];
    if (suma <= k) {
      while (j < m && suma + b[j] + sumb <= k) {
        sumb += b[j];
        j++;
      }
      ans = max(ans, i + j);
    }
  }
  cout << ans << endl;
}

int main() {
  inp();
  return 0;
}


Error: 0

Fixed submission:

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

#define ms(s, n) memset(s, n, sizeof(s))
#define all(a) a.begin(), a.end()
#define present(t, x) (t.find(x) != t.end())
#define sz(a) int((a).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;

const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 1;
inline ll gcd(ll a, ll b) {
  ll r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int n, m, a[200000], b[200000], k;

int inp() {
  cin >> n >> m >> k;
  FOR(i, 0, n)
  cin >> a[i];
  FOR(i, 0, m)
  cin >> b[i];
  int cnt = 0;
  int i = 0, j = 0;
  while (i < n && j < m) {
    int tmp = min(a[i], b[j]);
    if (k >= tmp) {
      cnt++;
      k -= tmp;
    } else {
      return cnt;
    }
    if (tmp == a[i])
      i++;
    else
      j++;
  }
  while (i < n) {
    if (k >= a[i]) {
      k -= a[i];
      cnt++;
      i++;
    } else {
      return cnt;
    }
  }
  while (j < m) {
    if (k >= b[j]) {
      k -= b[j];
      cnt++;
      j++;
    } else {
      return cnt;
    }
  }
}

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

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, n) for (ll i = 1; i <= (n); ++i)
#define _rep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define _reps(i, n) for (ll i = n; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

  vector<ll> a(n + 1);
  reps(i, n) {
    cin >> a[i];
    a[i] += a[i - 1];
  }

  vector<ll> b(m + 1);
  reps(i, m) {
    cin >> b[i];
    b[i] += b[i - 1];
  }

  ll cnt = 0;
  ll j = m;
  for (ll i = 0; i <= n; i++) {
    if (a[i] > k) {
      break;
    }

    while ((a[i] + b[j]) > k) {
      j--;
    }
    cnt = max(cnt, i + j);
  }

  cout << cnt << 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>

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, n) for (ll i = 1; i <= (n); ++i)
#define _rep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define _reps(i, n) for (ll i = n; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }

  vector<ll> b(n);
  rep(i, m) { cin >> b[i]; }

  ll ca = 0;
  ll cb = 0;
  ll cnt = 0;
  while (k > 0) {
    if ((a[ca] < b[cb]) && (ca < n)) {
      if (k < a[ca]) {
        break;
      }
      k -= a[ca];
      ca++;
    } else if (cb < m) {
      if (k < b[cb]) {
        break;
      }
      k -= b[cb];
      cb++;
    } else {
      if (k < a[ca]) {
        break;
      }
      k -= a[ca];
      ca++;
    }
    cnt++;
  }

  cout << cnt << endl;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 50;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int HASH = 131;

ll a[maxn];
ll b[maxn];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    int tmp;
    cin >> tmp;
    a[i] = a[i - 1] + tmp;
  }
  for (int i = 1; i <= m; i++) {
    int tmp;
    cin >> tmp;
    b[i] = b[i - 1] + tmp;
  }
  int ans = 0;
  for (int i = 0; i <= n && a[i] <= k; i++) {
    int l = 0, r = m;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (a[i] + b[mid] > k)
        r = mid - 1;
      else {
        l = mid + 1;
        ans = max(ans, i + mid);
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 50;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int HASH = 131;

int a[maxn];
int b[maxn];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    int tmp;
    cin >> tmp;
    b[i] = b[i - 1] + tmp;
  }
  int ans = 0;
  for (int i = 0; i <= m; i++) {
    int tmp = i;
    int need = b[i];
    if (need > k)
      break;
    for (int j = 1; j <= n; j++) {
      need += a[j];
      if (need > k)
        break;
      tmp++;
    }
    ans = max(ans, tmp);
  }
  printf("%d\\n", ans);
  return 0;
}