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

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  rep(i, M) {
    cin >> B[i];
    b[i + 1] = B[i];
  }

  rep(i, N) { a[i + 1] += a[i]; }
  rep(i, M) { b[i + 1] += b[i]; }

  ll ans = 0;
  rep(i, N + 1) {
    ll kk = K - a[i];
    if (kk < 0)
      continue;
    ll itr = upper_bound(all(b), kk) - b.begin() - 1;
    ans = max(ans, itr + i);
  }

  cout << ans << endl;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(N), a(N), b(N);
  rep(i, N) {
    cin >> A[i];
    a[i] = A[i];
  }

  rep(i, M) {
    cin >> B[i];
    b[i] = B[i];
  }

  rep(i, N - 1) { a[i + 1] += a[i]; }
  rep(i, M - 1) { b[i + 1] += b[i]; }

  ll aitr = -1, bitr = -1, ans = 0;
  rep(i, N) {
    if (a[i] <= K)
      aitr = i;
    else
      break;
  }

  ans += (aitr + 1);
  K -= a[aitr];

  rep(i, M) {
    if (b[i] <= K)
      bitr = i;
    else
      break;
  }

  ans += (bitr + 1);

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

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

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] = a[i] + a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] = b[i] + b[i - 1];
  }
  ll ans = 0, j = m;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k) {
      break;
    }
    while (b[j] > k - a[i]) {
      j = j - 1;
    }
    ans = max(ans, (ll)i + j);
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int j = 1; j <= m; j++) {
    cin >> b[j];
  }
  ll dp[n + 1][m + 1], ans = 0;
  memset(dp, 0, sizeof dp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = max(dp[i - 1][j] + a[i], dp[i][j - 1] + b[j]);
      if (dp[i][j] <= k) {
        ans = max(ans, (ll)i + j);
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll a[200005];
ll b[200005];
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll t, ans = 0;
  ll tmp = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> t;
    a[i] = a[i - 1] + t;
    if (a[i] <= k)
      tmp = i;
  }
  for (ll i = 1; i <= m; i++) {
    cin >> t;
    b[i] = b[i - 1] + t;
  }
  for (ll i = 0, j = m; i <= tmp; i++) {
    while (a[i] + b[j] > k)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll a[200005];
ll b[200005];
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll t, ans = 0;
  ll tmp = 1;
  for (ll i = 1; i <= n; i++) {
    cin >> t;
    a[i] = a[i - 1] + t;
    if (a[i] <= k)
      tmp = i;
  }
  for (ll i = 1; i <= m; i++) {
    cin >> t;
    b[i] = b[i - 1] + t;
  }
  for (ll i = 1, j = m; i <= tmp; i++) {
    while (a[i] + b[j] > k)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;

int main(void) {
  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 j = 0; j < m; j++) {
    cin >> b[j];
  }

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

  int j = m;
  int ans = 0;
  for (int i = 0; i < n + 1; 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 <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

#include <queue>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
ll a[200005];
ll b[200005];
int main(void) {
  ll n, m, k;
  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  ll sum = 0;
  ll count = 0;
  ll i = 0, j = 0;
  while (1) {
    ll x = a[i];
    ll y = b[j];
    if (x < y) {
      sum += x;
      i++;
    } else {
      sum += y;
      j++;
    }
    if (sum > k) {
      break;
    }

    count++;
    if (count == n + m) {
      cout << n + m << endl;
      return 0;
    }
  }
  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() {
  int i, j, N, M, K, ans = 0;

  cin >> N >> M >> K;
  vector<int> a(N);
  vector<int> b(M);
  for (i = 0; i < N; i++) {
    cin >> a[i];
  }

  ll t = 0;
  for (i = 0; i < M; i++) {
    cin >> b[i];
    t += b[i];
  }

  j = M;

  for (i = 0; i < N + 1; 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: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, N, M, K, count = 0;

  vector<int> a(N), b(M);

  cin >> N >> M >> K;
  for (i = 0; i < N; i++) {
    cin >> a[i];
  }

  for (i = 0; i < M; i++) {
    cin >> b[i];
  }
  i = 0, j = 0;
  while (1) {
    K -= min(a[i], b[j]);
    if (K < 0) {
      break;
    }
    count++;
    if (a[i] < b[j]) {
      i -= 1;
    } else {
      j -= 1;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N);
  vector<long long> B(M);
  vector<long long> C(N + 1, 0);
  vector<long long> D(M + 1, 0);
  for (int a = 0; a < N; a++) {
    cin >> A[a];
    if (a == 0)
      C[1] = A[0];
    if (a != 0)
      C[a + 1] = C[a] + A[a];
  }
  for (int a = 0; a < M; a++) {
    cin >> B[a];
    if (a == 0)
      D[1] = B[0];
    if (a != 0)
      D[a + 1] = D[a] + B[a];
  }
  long long result = 0;
  long long n = 0;
  for (int a = N; a >= 0; a--) {
    for (int b = n; b <= M && C[a] + D[b] <= K; b++) {
      if (result < a + b) {
        result = a + b;
      }
      n = b;
    }
  }
  cout << result << endl;
}

Error: TLE

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);
  vector<long long> B(M);
  vector<long long> C(N + 1, 0);
  vector<long long> D(M + 1, 0);
  for (int a = 0; a < N; a++) {
    cin >> A[a];
    if (a == 0)
      C[1] = A[0];
    if (a != 0)
      C[a + 1] = C[a] + A[a];
  }
  for (int a = 0; a < M; a++) {
    cin >> B[a];
    if (a == 0)
      D[1] = B[0];
    if (a != 0)
      D[a + 1] = D[a] + B[a];
  }
  long long result = 0;
  long long n = 0;
  for (int a = N; a >= 0; a--) {
    for (int b = n; b <= M && C[a] + D[b] <= K; b++) {
      if (result < a + b) {
        result = a + b;
        n = b;
      }
    }
  }
  cout << result << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  rep(i, n) {
    cin >> a[i + 1];
    if (i != 0)
      a[i + 1] += a[i];
  }
  rep(i, m) {
    cin >> b[i + 1];
    if (i != 0)
      b[i + 1] += b[i];
  }
  ll ng = n + m + 1, ok = 0;
  while (abs(ng - ok) > 1) {
    ll mid = (ng + ok) / 2;
    bool c = false;
    rep(i, mid + 1) {
      if (i <= n && mid - i <= m) {
        if (a[i] + b[mid - i] <= k)
          c = true;
      }
    }
    if (c)
      ok = mid;
    else
      ng = mid;
  }
  ans = ok;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1);
  a[n] = 1e10;
  b[m] = 1e10;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll ng = n + m + 1, ok = -1;
  while (abs(ng - ok) > 1) {
    ll mid = (ng + ok) / 2;
    ll total = 0;
    rep(i, mid) {
      total = 0;
      rep(k, i) total += a[k];
      rep(k, mid - i) total += b[k];
      if (total <= k)
        i = mid;
    }
    if (total <= k)
      ok = mid;
    else
      ng = mid;
  }
  ans = ok;
  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 < (n); ++i)
#define ll long long
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);

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

  ll ans = 0, j = m;
  for (ll i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    while (a[i] + b[j] > k)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << 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 ll long long
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);

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

  ll ans = 0, tmp = 0;
  queue<ll> aa, bb;
  rep(i, n) aa.push(a[i]);
  rep(i, m) bb.push(b[i]);

  while (true) {
    if (aa.empty() && bb.empty())
      break;
    else if (aa.empty())
      aa.push(INF);
    else if (bb.empty())
      bb.push(INF);
    tmp += min(aa.front(), bb.front());

    if (aa.front() == bb.front()) {
      ll na = aa.front(), nb = bb.front();
      aa.pop();
      bb.pop();
      if (aa.front() > bb.front()) {
        bb.pop();
        aa.push(na);
      } else {
        aa.pop();
        bb.push(nb);
      }

    } else if (aa.front() > bb.front())
      bb.pop();
    else
      aa.pop();

    if (tmp > k)
      break;
    ans++;
    if (ans > n + m) {
      ans = n + m;
      break;
    }
  }
  cout << ans << 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;
  ll arr[n], sum[n] = {0};
  ll arr1[m], sum1[m] = {0};
  for (ll i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  for (ll i = 0; i < m; ++i) {
    cin >> arr1[i];
  }
  sum[0] = arr[0];
  for (ll i = 1; i < n; ++i) {
    sum[i] = arr[i] + sum[i - 1];
  }
  sum1[0] = arr1[0];
  for (ll i = 1; i < m; ++i) {
    sum1[i] = arr1[i] + sum1[i - 1];
  }

  ll ans = upper_bound(sum1, sum1 + m, k) - sum1;
  for (ll i = 0; i < n; ++i) {
    if (sum[i] > k)
      break;
    ll br = upper_bound(sum1, sum1 + m, k - sum[i]) - sum1;
    ans = max(ans, i + br + 1);
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {

  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  deque<ll> q, q1;
  for (ll i = 0; i < n; ++i) {
    ll tmp;
    cin >> tmp;
    q.push_back(tmp);
  }
  for (ll i = 0; i < m; ++i) {
    ll tmp;
    cin >> tmp;
    q1.push_back(tmp);
  }
  if (q.front() > k && q1.front() > k) {
    cout << 0;
    return 0;
  }
  ll cnt = 0;
  while (k > ans && (q.size() > 0 || q1.size() > 0)) {
    if (q.front() < q1.front() || q1.size() == 0) {
      ans += q.front();
      q.pop_front();
    } else if (q1.size() > 0) {
      ans += q1.front();
      q1.pop_front();
    }
    cnt++;
  }
  cout << cnt;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int this_max = 0;

void dfs(long long sum, long long k, int an, int bn, long long a[],
         long long b[], int num) {
  if (sum > k) {
    if (this_max < (num - 1))
      this_max = num - 1;
    return;
  }
  dfs(sum + a[an], k, an + 1, bn, a, b, num + 1);
  dfs(sum + b[bn], k, an, bn + 1, a, b, num + 1);
}
int bs(long long a[], int key, int n) {
  long long l = 0, r = n, mid;
  while (l < r) {
    mid = (l + r) / 2;
    if (r - l <= 1)
      return mid;
    else if (key < a[mid]) {
      r = mid;
    } else if (a[mid] < key) {
      l = mid + 1;
    }
  }
  return -1;
}
int main() {
  int n, m;
  long long k;
  cin >> n >> m >> k;
  long long a[n], b[m], sum;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  // dfs(0,k,0,0,a,b,0);
  sum = b[0];
  rep(i, m - 1) sum += b[i + 1];
  int j = m;
  rep(i, n + 1) {
    while (j > 0 && sum > k) {
      j--;
      sum -= b[j];
    }
    if (sum > k)
      break;
    this_max = max(this_max, i + j);
    if (i == n)
      break;
    sum += a[i];
  }
  cout << this_max << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int this_max = 0;

void dfs(long long sum, long long k, int an, int bn, long long a[],
         long long b[], int num) {
  if (sum > k) {
    if (this_max < (num - 1))
      this_max = num - 1;
    return;
  }
  dfs(sum + a[an], k, an + 1, bn, a, b, num + 1);
  dfs(sum + b[bn], k, an, bn + 1, a, b, num + 1);
}

int main() {
  int n, m;
  long long k;
  cin >> n >> m >> k;
  long long a[n], b[m];
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  dfs(0, k, 0, 0, a, b, 0);
  cout << this_max << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1);
  for (int i = 1; i <= n; i++) {
    ll A;
    cin >> A;
    a[i] = a[i - 1] + A;
  }
  for (int i = 1; i <= m; i++) {
    ll B;
    cin >> B;
    b[i] = b[i - 1] + B;
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    ll res = k - a[i];
    ll itre = upper_bound(b.begin(), b.end(), res) - b.begin() - 1;
    ans = max(ans, i + itre);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

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> da(n), db(m);
  da[0] = a[0];
  db[0] = b[0];
  ll ai, bi;
  for (int i = 1; i < n; i++) {
    da[i] = da[i - 1] + a[i];
    if (da[i] > k) {
      ai = i;
      break;
    }
  }
  for (int i = 1; i < m; i++) {
    db[i] = db[i - 1] + b[i];
    if (db[i] > k) {
      bi = i;
      break;
    }
  }

  ll ans = 0;
  for (int i = 0; i <= ai; i++) {
    for (int j = 0; j <= bi; j++) {
      if (da[i] + db[j] > k)
        break;
      ll x = i + j + 2;
      ans = max(ans, x);
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector<pair<ll, ll>>
#define plli pair<ll, ll>
#define vps vector<pair<string, string>>
#define vpi vector<pair<int, int>>
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define forn(i, n) for (ll i = 0; i < n; i++)
#define print(arr, a, n)                                                       \\
  for (ll i = (a); i < (n); i++)                                               \\
    cout << (arr)[i] << " ";

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ll n, m, k;
  cin >> n >> m >> k;
  vlli a(n), b(m), pa(n + 2, 0), pb(m + 2, 0);
  forn(i, n) {
    cin >> a[i];
    pa[i + 1] = pa[i] + a[i];
  }
  forn(i, m) {
    cin >> b[i];
    pb[i + 1] = pb[i] + b[i];
  }
  ll ans = 0, j = m;
  for (ll i = 0; i <= n; i++) {
    if (pa[i] > k) {
      break;
    }
    while (pb[j] > k - pa[i] && j >= 0) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector<pair<ll, ll>>
#define plli pair<ll, ll>
#define vps vector<pair<string, string>>
#define vpi vector<pair<int, int>>
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define forn(i, n) for (ll i = 0; i < n; i++)
#define print(arr, a, n)                                                       \\
  for (ll i = (a); i < (n); i++)                                               \\
    cout << (arr)[i] << " ";

int main() {
  fast;
  ll n, m, k;
  cin >> n >> m >> k;
  vlli a(n), b(m), pa(n + 1, 0), pb(m + 1, 0);
  forn(i, n) {
    cin >> a[i];
    pa[i + 1] = pa[i] + a[i];
  }
  forn(i, m) {
    cin >> b[i];
    pb[i + 1] = pb[i] + b[i];
  }
  ll ans = 0, j = 1;
  for (ll i = 1; i <= n; i++) {
    if (pa[i] > k) {
      break;
    }
    while (pb[j] <= k - a[i]) {
      j++;
    }
    ans = max(ans, i + j - 1);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 2e5 + 9;
const int mod = 1e9 + 7;

ll a[N], b[N];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, k, x, ans = 0;
  cin >> n >> m >> k;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    sum += x;
    a[i] = sum;
  }
  sum = 0;
  for (int i = 1; i <= m; i++) {
    cin >> x;
    sum += x;
    b[i] = sum;
  }
  ans = 0;
  for (int i = 0; i <= n; i++) {
    ll c = k;
    if (a[i] > c)
      continue;
    c -= a[i];
    ll l = 0, r = m, ok;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (b[mid] <= c) {
        ok = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    ans = max(ans, i + ok);
  }
  cout << ans << '\\n';
}


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;
typedef long long int ll;
const int N = 2e5 + 9;
const int mod = 1e9 + 7;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, k, x, ans = 0;
  vector<ll> v, v1;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  for (int i = 0; i < m; i++) {
    cin >> x;
    v1.push_back(x);
  }
  int i = 0;
  int j = 0;
  while (1) {
    if (v[i] <= v[j] && i < n) {
      if (v[i] <= k) {
        k -= v[i];
        i++;
        ans++;
      } else
        break;
    } else {
      if (v1[j] <= k && j < m) {
        k -= v1[j];
        j++;
        ans++;
      } else
        break;
    }
  }
  cout << ans << '\\n';
}