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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll n, m, k;
  std::cin >> n >> m >> k;
  vll a(n), b(m);
  re(i, n) std::cin >> a[i];
  re(i, m) std::cin >> b[i];

  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] += b[i - 1];
  ll ans = 0;

  for (int i = 0; i <= n; i++) {
    if (i != 0 && a[i - 1] > k)
      break;
    ll l = 0, r = m + 1;
    while (r - l > 1) {
      ll mid = (l + r) / 2;
      if (b[mid - 1] + (i == 0 ? 0 : a[i - 1]) > k)
        r = mid;
      else
        l = mid;
    }
    ans = max(ans, i + l);
  }
  std::cout << ans << '\\n';
  return 0;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll n, m, k;
  std::cin >> n >> m >> k;
  vll a(n), b(m);
  re(i, n) std::cin >> a[i];
  re(i, m) std::cin >> b[i];

  for (int i = 1; i <= n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i <= m; i++)
    b[i] += b[i - 1];
  ll ans = 0;

  for (int i = 0; i <= n; i++) {
    if (i != 0 && a[i - 1] > k)
      break;
    ll l = 0, r = m + 1;
    while (r - l > 1) {
      ll mid = (l + r) / 2;
      if (b[mid - 1] + (i == 0 ? 0 : a[i - 1]) > k)
        r = mid;
      else
        l = mid;
    }
    // std::cout << i << " " << l << '\\n';
    ans = max(ans, i + l);
  }
  std::cout << ans << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  ll temp = 0;
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
    temp += b[i];
    if (temp > k)
      break;
    ll resto = k - temp;
    ll in = upper_bound(a, a + n, resto) - a;
    ans = max(ans, in + i + 1LL);
  }

  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define M 20005
using namespace std;
typedef long long ll;

int main() {

  fastio;
  ll n, m, k;
  cin >> n >> m >> k;
  int ib = 0, r;
  ll a[M], b[M], sa[M], sb[M];
  sa[0] = 0;
  sb[0] = 0;

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

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

  for (int i = 1; i <= m; i++) {
    if (sb[i] > k) {
      ib = i - 1;
      break;
    }
    if (i == m)
      ib = m;
  }

  r = ib;

  for (int i = 1; i <= n; i++) {
    if (k >= sa[i]) {
      for (int j = ib; j >= 0; j--)
        if (sa[i] + sb[j] <= k) {
          ib = j;
          r = max(r, ib + i);
          break;
        }
    }
  }

  cout << r << "\\n";

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

#include <bits/stdc++.h>
#define pi 3.14159
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
const int N = 1e5 + 7, M = 1e7, OO = 0x3f3f3f3f;
#define AC                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cin.sync_with_stdio(0);
template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int main() {
  long long n, m, array1[200005], array2[200005], k, i, counter = 0, maxi = 0,
                                                        sum = 0;
  ordered_set<long long> s, s2;
  cin >> n >> m >> k;
  for (i = 0; i < n; ++i) {
    cin >> array1[i];
    sum += array1[i];
    s2.insert(sum);
  }
  sum = 0;
  for (i = 0; i < m; ++i) {
    cin >> array2[i];
    sum += array2[i];
    s.insert(sum);
  }
  sum = 0;
  for (i = 0; i < n; ++i) {
    counter = i + 1;
    sum += array1[i];
    if (sum > k) {
      break;
    }
    counter += s.order_of_key(k - sum);
    if (s.find(k - sum) != s.end()) {
      counter++;
    }
    maxi = max(maxi, counter);
  }
  sum = 0;
  for (i = 0; i < m; ++i) {
    counter = i + 1;
    sum += array2[i];
    if (sum > k) {
      break;
    }
    counter += s2.order_of_key(k - sum);
    if (s2.find(k - sum) != s2.end()) {
      counter++;
    }
    maxi = max(maxi, counter);
  }
  cout << maxi << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ll n, m, k, x;
  vector<ll> v1, v2;
  cin >> n >> m >> k;
  for (ll i = 0; i < n; i++) {
    cin >> x;
    if (i == 0)
      v1.push_back(x);
    else {
      v1.push_back(v1[i - 1] + x);
    }
  }
  ll ans = 0;
  for (ll i = 0; i < m; i++) {
    cin >> x;
    if (i == 0)
      v2.push_back(x);
    else {
      v2.push_back(v2[i - 1] + x);
    }
    x = upper_bound(v1.begin(), v1.end(), k - v2[i]) - v1.begin() - 1;
    if (v2[i] <= k) {
      ans = max(ans, x + 1 + i + 1);
    } else
      break;
  }
  for (ll i = 0; i < m; i++) {
    x = upper_bound(v2.begin(), v2.end(), k - v1[i]) - v2.begin() - 1;
    if (v1[i] <= k) {
      ans = max(ans, x + 1 + i + 1);
    } else
      break;
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int max_n = 200005;
int a[max_n], b[max_n];
long long sum1[max_n], sum2[max_n];
long long max(long long a, long long b) { return a >= b ? a : b; }
int main() {
  int ans = 0;
  long long n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum1[i] = sum1[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    sum2[i] = sum2[i - 1] + b[i];
  }
  for (int i = 0; i <= n; i++) {
    if (sum1[i] <= k)
      ans = max(ans, i + upper_bound(sum2 + 1, sum2 + 1 + m, k - sum1[i]) -
                         sum2 - 1);
    else
      break;
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

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

int main() {
  int n, m, i, num = 0;
  long long k, x;
  cin >> n >> m >> k;
  vector<long long> v1, v2;
  vector<long long>::iterator it1, it2;
  for (i = 0; i < n; i++) {
    cin >> x;
    v1.push_back(x);
  }
  for (i = 0; i < m; i++) {
    cin >> x;
    v2.push_back(x);
  }
  it1 = v1.begin();
  it2 = v2.begin();
  for (it1 = v1.begin(), it1 = v1.begin(); it1 != v1.end(), it2 != v2.end();) {
    if (*it1 <= k || *it2 <= k) {
      if (*it1 <= *it2) {
        num++;
        it1++;
        k -= *it1;
      } else {
        num++;
        it2++;
        k -= *it2;
      }
    } else
      break;
  }
  /*
  while (*it1 <=k || *it2 <=k)
  {
      if (*it1 <= k && *it1<=*it2)
      {
          num ++;
          v1.erase(it1);
      }
      else if (*it2 <=k && *it2 <=*it1)
      {
          num ++;
          v2.erase(it2);
      }
  }
  */
  cout << num << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int binary(long long Arr[], long long left, long long right, long long target) {
  while (left <= right) {
    long long mid = int((left + right) / 2);
    if (Arr[mid] == target)
      return mid;
    else if (Arr[mid] < target)
      left = mid + 1;
    else
      right = mid - 1;
  }
  return right;
}
int main() {
  int N, M;
  long long X;
  cin >> N >> M >> X;
  long long ArrA[N + 1] = {0};
  long long ArrB[M + 1] = {0};
  int bookA = 0, bookB = 0;
  for (int i = 1; i <= N; i++) {
    long long a;
    cin >> a;
    ArrA[i] = ArrA[i - 1] + a;
  }
  for (int i = 1; i <= M; i++) {
    long long b;
    cin >> b;
    ArrB[i] = ArrB[i - 1] + b;
  }
  int total = 0;
  for (int i = 0; i <= N; i++) {
    long long timeB = X - ArrA[i];
    if (timeB < 0) {
      continue;
    }
    int tempB = binary(ArrB, 0, M, timeB);
    total = max(total, tempB + i);
  }
  cout << total << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
unsigned long long int *readStack(int length) {
  unsigned long long int *stack = new unsigned long long int[length];
  for (int i = 0; i < length; i++) {
    cin >> stack[i];
    // cout << stack[i] << " ";
  }
  // cout << endl;
  return stack;
}
unsigned long long int getSum(unsigned long long int *s, int l) {
  unsigned long long int sum = 0;
  for (int i = 0; i < l; i++)
    sum += s[i];
  return sum;
}
int main() {
  int n, m;
  unsigned long long int k;
  cin >> n >> m >> k;
  unsigned long long int *aStack = readStack(n);
  unsigned long long int *bStack = readStack(m);
  // check if time sum of stack is <= k
  if (getSum(aStack, n) + getSum(bStack, m) <= k)
    cout << m + n;
  else {
    int numOfBooks = 0;
    unsigned long long int aTime = 0;
    unsigned long long int bTime = 0;
    for (int i = -1; i < n; i++) {
      if (i >= 0)
        aTime += aStack[i];
      if (aTime > k)
        break;
      for (int j = -1; j < m; j++) {
        // cout << i << " " << j << endl;
        if (i == -1 && j == -1)
          continue;
        if (j >= 0)
          bTime += bStack[j];
        // cout << aTime + bTime << endl;
        if (aTime + bTime > k) {
          break;
        }
        if (aTime + bTime <= k && i + j + 2 > numOfBooks)
          numOfBooks = i + j + 2;
      }
      bTime = 0;
    }
    cout << numOfBooks;
  }
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \\
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> as(n);
  cin >> as;
  vector<ll> bs(m);
  cin >> bs;
  vector<ll> acca(n + 1), accb(m + 1);
  for (int i = 0; i < n; i++) {
    acca[i + 1] = acca[i] + as[i];
  }
  for (int i = 0; i < m; i++) {
    accb[i + 1] = accb[i] + bs[i];
  }
  printde(acca);
  printde(accb);
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (acca[i] > k) {
      break;
    }
    ans =
        max(ans, i + (int)(upper_bound(accb.begin(), accb.end(), k - acca[i]) -
                           accb.begin() - 1));
  }
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \\
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> as(n);
  cin >> as;
  vector<ll> bs(m);
  cin >> bs;
  vector<ll> acca(n + 1), accb(m + 1);
  for (int i = 0; i < n; i++) {
    acca[i + 1] = acca[i] + as[i];
  }
  for (int i = 0; i < m; i++) {
    accb[i + 1] = acca[i] + bs[i];
  }
  printde(acca);
  printde(accb);
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (acca[i] > k) {
      break;
    }
    ans =
        max(ans, i + (int)(upper_bound(accb.begin(), accb.end(), k - acca[i]) -
                           accb.begin() - 1));
  }
  cout << ans << '\\n';
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using Graph = vector<vector<int>>;
const int INF = 1000000007;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), as(n + 1), bs(m + 1);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  rep(i, n) as[i + 1] += a[i] + as[i];
  rep(i, m) bs[i + 1] += b[i] + bs[i];
  int ans = 0;
  rep(i, n + 1) {
    ll ok = k - as[i];
    if (ok < 0)
      continue;
    ans = max(ans, i + (int)(upper_bound(all(bs), ok) - bs.begin()));
  }
  cout << ans - 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using Graph = vector<vector<int>>;
const int INF = 1000000007;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> A(n), B(m);
  rep(i, n) cin >> A[i];
  rep(i, m) cin >> B[i];
  int a = 0, b = 0;
  ll now = 0;
  ll ans = 0;
  while (now <= k) {
    ll ok = k - now;
    if (a == n && b == m)
      break;
    if (a == n) {
      if (ok >= B[b])
        now += B[b];
      else
        break;
      b++;
      ans++;
    } else if (b == m) {
      if (ok >= A[a])
        now += A[a];
      else
        break;
      a++;
      ans++;
    } else {
      if (A[a] == B[b])
        return -1;
      if (A[a] >= B[b] && ok >= B[b]) {
        ans++;
        now += B[b];
        b++;
      } else if (ok >= A[a]) {
        ans++;
        now += A[a];
        a++;
      } else
        break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef pair<string, string> Pstring;
typedef pair<double, double> Pdouble;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define Precision13 cout << fixed << setprecision(13)
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const int MOD = 1000000007;
const int INF = 1 << 29;
using Graph = vector<vector<int>>;

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

  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  REP1(i, n) cin >> a[i];
  REP1(i, m) cin >> b[i];

  vector<ll> asum(n + 1, 0);
  vector<ll> bsum(m + 1, 0);

  for (ll i = 1; i <= n; i++) {
    asum[i] = asum[i - 1] + a[i];
  }

  for (ll i = 1; i <= m; i++) {
    bsum[i] = bsum[i - 1] + b[i];
  }

  ll ans = 0;
  ll j = m;
  for (ll i = 0; i <= n; i++) {
    if (asum[i] > k) {
      break;
    }
    while (asum[i] + bsum[j] > k) {
      j--;
    }
    ans = max(ans, i + j);
  }

  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef pair<string, string> Pstring;
typedef pair<double, double> Pdouble;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define Precision13 cout << fixed << setprecision(13)
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const int MOD = 1000000007;
const int INF = 1 << 29;
using Graph = vector<vector<int>>;

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

  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  REP1(i, n) cin >> a[i];
  REP1(i, m) cin >> b[i];

  vector<ll> asum(n + 1, 0);
  vector<ll> bsum(m + 1, 0);

  for (ll i = 1; i <= n; i++) {
    asum[i] = asum[i - 1] + a[i];
  }

  for (ll i = 1; i <= m; i++) {
    bsum[i] = bsum[i - 1] + b[i];
  }

  ll ans = 0;
  ll j = m;
  for (ll i = 1; i <= n; i++) {
    if (a[i] > k) {
      break;
    }
    while (asum[i] + bsum[j] > k) {
      j--;
    }
    ans = max(ans, i + j);
  }

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

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

int main() {
  int n, m;
  int64_t k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1), b(m + 1);
  a[0] = 0LL;
  b[0] = 0LL;
  for (int i = 1; i <= n; i++) {
    int64_t ai;
    cin >> ai;
    a[i] = a[i - 1] + ai;
  }
  for (int i = 1; i <= m; i++) {
    int64_t bi;
    cin >> bi;
    b[i] = b[i - 1] + bi;
  }
  int dokuha = 0;
  int j = b.size() - 1;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] > k)
      break;
    while (a[i] + b[j] > k) {
      j--;
    }
    dokuha = max(dokuha, i + j);
  }
  cout << dokuha;
}

Error: -11

Fixed submission:

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

int main() {
  int n, m;
  int64_t k, sumA = 0LL, sumB = 0LL;
  cin >> n >> m >> k;
  vector<int> a, b; // 要素数はn,mか単体で超さない冊数
  for (int i = 0; i < n; i++) {
    int ai;
    cin >> ai;
    sumA += ai;
    if (sumA <= k) {
      a[i] = ai;
    }
  }
  for (int i = 0; i < m; i++) {
    int bi;
    cin >> bi;
    sumB += bi;
    if (sumB <= k) {
      b[i] = bi;
    }
  }
  int64_t jikan = 0; //
  int cnt = 0;       //
  int dokuha = 0;
  for (int i = 0; i < a.size(); i++) {
    if (jikan + a[i] <= k) {
      jikan += a[i];
      cnt++;
      for (int j = 0; j < b.size(); j++) {
        if (jikan + b[j] <= k) {
          jikan += b[j];
          cnt++;
        } else { // bのj冊目を読むと時間超えるなら
          break;
        }
      }
      dokuha = max(dokuha, cnt);
    } else {
      dokuha = b.size();
    }
    break;
  }
  cout << dokuha;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<long int> a(n), b(m);
  int p = m - 1;
  rep(i, n) {
    int x;
    cin >> x;
    if (i == 0)
      a[i] = x;
    else
      a[i] = a[i - 1] + x;
  }
  rep(i, m) {
    int x;
    cin >> x;
    if (i == 0)
      b[i] = x;
    else
      b[i] = b[i - 1] + x;

    if (b[i] > k) {
      p = i - 1;
      break;
    }
  }

  int cnt = p + 1;
  // cout << cnt << endl;
  int j = -1;
  for (int i = p; i >= -1; i--) {
    long int num;
    if (i == -1)
      num = 0;
    else
      num = b[i];
    while (j < n - 1) {
      if (num + a[j + 1] > k)
        break;
      else
        j++;
    }
    cnt = max(cnt, i + j + 2);
    if (j == n - 1 || a[j + 1] > k)
      break;
  }

  cout << cnt << endl;

  return 0;
}


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


Fixed submission:

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

int n, m, k;
vector<int> a, b;
vector<vector<int>> dp(200001, vector<int>(200001, -1));

int rec(int i, int j, int w) {
  if (dp[i][j] >= 0) {
    return dp[i][j];
  }

  int res;
  if (w > k) {
    res = -1;
  } else if (i >= n || j >= m) {
    if (i < n)
      res = rec(i + 1, j, w + a[i]) + 1;
    else if (j < m)
      res = rec(i, j + 1, w + b[j]) + 1;
    else
      res = 0;
  } else {
    res = max(rec(i + 1, j, w + a[i]) + 1, rec(i, j + 1, w + b[j]) + 1);
  }

  dp[i][j] = res;
  return res;
}

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

  rep(i, n) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  rep(i, m) {
    int x;
    cin >> x;
    b.push_back(x);
  }

  cout << rec(0, 0, 0) << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> a(n + 1, 0), b(m + 1, 0);
  for (int i = 1; i < n + 1; ++i) {
    long long t;
    cin >> t;
    a[i] = a[i - 1] + t;
  }
  for (int i = 1; i < m + 1; ++i) {
    long long t;
    cin >> t;
    b[i] = b[i - 1] + t;
  }
  if (k >= a[n] + b[m]) {
    cout << n + m << endl;
    return 0;
  }
  a.push_back(a[n] + k);
  b.push_back(b[m] + k);
  int ai = upper_bound(a.begin(), a.end(), k) - a.begin() - 1;
  int bi = upper_bound(b.begin(), b.end(), k - a[ai]) - b.begin() - 1;
  int u = ai + bi;
  while (ai > 0) {
    --ai;
    bi = upper_bound(b.begin(), b.end(), k - a[ai]) - b.begin() - 1;
    u = max(u, ai + bi);
  }
  cout << u << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  int sa = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sa += a[i];
  }
  int sb = 0;
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
    sb += b[i];
  }
  if (k >= sa + sb) {
    cout << n + m << endl;
  } else {
    int ja = 0, jb = 0;
    int u = 0;
    while (k > 0) {
      int p, cab;
      if (ja == m) {
        cab = 1;
        p = b[jb];
      } else if (jb == n) {
        cab = 0;
        p = a[ja];
      } else if (b[jb] < a[ja]) {
        cab = 1;
        p = b[jb];
      } else {
        cab = 0;
        p = a[ja];
      }
      if (k < p) {
        break;
      } else {
        k -= p;
        if (cab) {
          ++jb;
        } else {
          ++ja;
        }
        ++u;
      }
    }
    cout << u << endl;
  }
  return 0;
}