alexjercan/bugnet huggingface dataset:
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n = 0, m = 0, k = 0;
  cin >> n >> m >> k;

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

  ll sum_a = 0;
  ll sum_b = 0;
  ll max_a = -1;
  ll max_b = -1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum_a += a[i];
    a[i] = sum_a;

    if (sum_a <= k)
      max_a = i;
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    sum_b += b[i];
    b[i] = sum_b;

    if (sum_b <= k)
      max_b = i;
  }

  int ans = 0;

  if (a[n] + b[m] <= k)
    ans = n + m;
  for (int i = 0; i <= max_a; i++) {
    ll temp = k - a[i];
    vector<ll>::iterator it;

    it = upper_bound(b.begin(), b.begin() + max_b + 1, temp);
    ans = max(ans, i + (int)(it - b.begin() - 1));
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n = 0, m = 0, k = 0;
  cin >> n >> m >> k;

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

  ll sum_a = 0;
  ll sum_b = 0;
  ll max_a = -1;
  ll max_b = -1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum_a += a[i];
    a[i] = sum_a;

    if (sum_a <= k)
      max_a = i;
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    sum_b += b[i];
    b[i] = sum_b;

    if (sum_b <= k)
      max_b = i;
  }

  int ans = 0;
  for (int i = 0; i <= max_a; i++) {
    int j = m;
    while (b[j] > k - a[i])
      j--;
    ans = max(ans, i + j);
  }

  cout << ans << endl;

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

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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); }

  long long t = 0;

  rep(i, m) { t += b.at(i); }

  int answer = 0;
  int j = m;

  rep(i, n + 1) {
    while (j > 0 && t > k) {
      j--;
      t -= b.at(j);
    }

    if (t > k)
      break;

    answer = max(answer, i + j);

    if (i == n)
      break;

    t += a.at(i);
  }

  cout << answer << endl;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

  vector<int> a(n);
  vector<int> b(n);

  rep(i, n) { cin >> a.at(i); }

  rep(i, m) { cin >> b.at(i); }

  int64_t t = 0;
  int64_t j = m;

  rep(i, m) { t += b.at(i); }

  int64_t answer = 0;

  rep(i, n + 1) {
    while (j > 0 && t > k) {
      j--;
      t -= b.at(j);
    }

    if (t > k)
      break;

    answer = max(answer, i + j);

    if (i == n)
      break;

    t += a.at(i);
  }

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

#include <bits/stdc++.h>
#include <math.h>
#include <string>
#include <unordered_map>
#define ll long long
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const ll N = 1e10;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
ll a[200005], b[200005];
int main() {
  ll n, m, k;
  int ans = 0;
  cin >> n >> m >> k;
  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];
  }

  for (int i = 0, j = m; i <= n; i++) {
    if (a[i] > k)
      break;
    while (a[i] + b[j] > k)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#include <string>
#include <unordered_map>
#define ll long long
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const ll N = 1e8;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
ll a[200005], b[200005];
int main() {
  ll n, m, k;
  ll time = 0;
  ll ans = 0;
  ll pa = 0, pb = 0;
  cin >> n >> m >> k;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < m; i++)
    cin >> b[i];
  for (ll i = 0; i < n + m; i++) {
    if (a[pa] <= b[pb]) {
      if ((time + a[pa]) <= k) {
        time += a[pa];
        pa++;
        ans++;
      } else {
        i = n + m;
      }
    } else {
      if ((time + b[pb]) <= k) {
        time += b[pb];
        pb++;
        ans++;
      } else {
        i = n + m;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

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

  // i=0,1,2...の時の合計時間が入る
  vector<long long> sumA(n + 1, 0);
  vector<long long> sumB(m + 1, 0);

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

  long long sum_min_A = 0;
  for (int i = 1; i <= n; i++) {
    sum_min_A += a.at(i - 1);
    sumA.at(i) = sum_min_A;
  }

  long long sum_min_B = 0;
  for (int i = 1; i <= m; i++) {
    sum_min_B += b.at(i - 1);
    sumB.at(i) = sum_min_B;
  }

  int best = m;
  vector<int> counts;
  int max = 0;
  for (int i = 0; i < n + 1; i++) {
    for (int j = best; j >= 0; j--) {
      if (sumA.at(i) + sumB.at(j) <= k) {
        best = j;
        counts.push_back(i + j);
        if (max < i + j)
          max = i + j;
        break;
      }
    }
  }
  /*
  int max = 0;
  for(int i = 0; i < counts.size(); i++){
          if(max < counts.at(i)) max = counts.at(i);
  }
  */
  cout << max << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

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

  // i=0,1,2...の時の合計時間が入る
  vector<long long> sumA(n + 1, 0);

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

  long long sum_min_A = 0;
  for (int i = 1; i <= n; i++) {
    sum_min_A += a.at(i - 1);
    sumA.at(i) = sum_min_A;
  }

  int best = m;
  vector<int> counts;
  for (int i = 0; i < n + 1; i++) {
    for (int j = best; j >= 0; j--) {
      long long sumB = 0;
      for (int l = 0; l < j; l++) {
        sumB += b.at(l);
      }
      if (sumA.at(i) + sumB <= k) {
        best = j;
        counts.push_back(i + j);
        break;
      }
    }
  }

  int max = 0;
  for (int i = 0; i < counts.size(); i++) {
    if (max < counts.at(i))
      max = counts.at(i);
  }

  cout << max << endl;
}
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<int> a(n);
  vector<int> b(m);
  vector<long long> suma(n + 1);
  vector<long long> sumb(m + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      suma[i + 1] = a[i];

    } else {
      suma[i + 1] = suma[i] + a[i];
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i == 0) {
      sumb[i + 1] = b[i];
    } else {
      sumb[i + 1] = sumb[i] + b[i];
    }
  }
  int ans = 0;
  for (int i = 0; i < n + 1; i++) {
    if (suma[i] > k)
      break;
    for (int j = m; j >= 0; j--) {
      if (suma[i] + sumb[j] <= k) {
        ans = max(ans, i + j);
        m = j;
        break;
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  int a[n + 5], b[m + 5];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i];
  int o = 1, i = 1, j = 1, c = 0;
  while (o <= (n + m)) {
    if (a[i] <= b[j] && i <= n && j <= m) {
      if (a[i] <= k) {
        c++;

        k -= a[i];
        i++;
        o++;
      }
    }
    if (b[j] <= a[i] && i <= n && j <= m) {
      if (b[j] <= k) {
        c++;

        k -= b[j];
        j++;
        o++;
      }
    }
    if (a[i] <= k && i <= n && j == m + 1) {
      c++;

      k -= a[i];
      i++;
      o++;
    }
    if (b[j] <= k && j <= m && i == n + 1) {
      c++;

      k -= b[j];
      j++;
      o++;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <utility>
#include <vector>

using namespace std;

int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> a(n), b(m);
  for (auto &i : a)
    cin >> i;
  for (auto &i : b)
    cin >> i;

  vector<long long> prefa(n), prefb(m);
  prefa[0] = a[0], prefb[0] = b[0];
  for (int i = 1; i < n; i++)
    prefa[i] = a[i] + prefa[i - 1];
  for (int i = 1; i < m; i++)
    prefb[i] = b[i] + prefb[i - 1];

  int ans = distance(prefb.begin(), upper_bound(prefb.begin(), prefb.end(), k));
  for (int i = 0; i < n; i++) {
    if (prefa[i] <= k) {
      ans = max(ans, i + 1 +
                         (int)distance(prefb.begin(),
                                       upper_bound(prefb.begin(), prefb.end(),
                                                   k - prefa[i])));
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (auto &i : a)
    cin >> i;
  for (auto &i : b)
    cin >> i;

  vector<vector<int>> dp(n + 1, vector<int>(m + 1, (int)1e9));
  dp[0][0] = 0;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i)
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[i - 1]);
      if (j)
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + b[j - 1]);
      if (dp[i][j] <= k)
        ans = max(ans, i + j);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

typedef pair<ll, ll> Pair;

int main() {

  ll N, M, K;
  ll ans = -1;

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

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

    ll t = K - sumA[i];

    if (t < 0)
      break;

    ll left = 0;
    ll right = M + 1;
    ll mid = 0;

    while (right - left > 1) {

      mid = (left + right) / 2;

      if (sumB[mid] <= t)
        left = mid;
      else
        right = mid;
    }

    ll res = i + left;

    ans = max(ans, res);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

typedef pair<ll, ll> Pair;

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];

  ll time = 0;
  ll ans = 0;
  ll a = 0, b = 0;

  string AB = "A";

  // for(ll i = 0;i < N;++i) cout << A[i] << endl;
  // for(ll j = 0;j < M;++j) cout << B[j] << endl;

  if (K < A[a] && K < B[b]) {

    cout << ans << endl;

  } else {

    while (K > time) {

      if (A[a] < B[b]) {
        time += A[a];
        a++;
      } else if (B[b] < A[a]) {
        time += B[b];
        b++;
      } else if (B[b] == A[a]) {

        ll sumA = 0;
        ll sumB = 0;

        ll aa = a;
        ll bb = b;

        while (sumA == sumB) {

          sumA += A[aa];
          sumB += B[bb];

          if (sumA > sumB) {
            time += sumB;
            b = bb;
          }
          if (sumB > sumA) {
            time += sumA;
            a = aa;
          }

          aa++;
          bb++;
        }
      }

      if (K >= time)
        ans++;
    }

    cout << ans << endl;
  }

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

#include <cmath>
#include <iostream>

#define ll long long

using namespace std;

int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  ll A[N + 1], B[M + 1];

  A[0] = B[0] = 0;

  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }

  for (ll i = 1; i <= M; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }

  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(i + j, ans);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <queue>

#define ll long long
#define MAX 200000

using namespace std;

int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  ll A[N + 1], B[M + 1];

  A[0] = B[0] = 0;

  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }

  for (ll i = 1; i <= M; i++) {
    cin >> B[i];
    B[i] += A[i - 1];
  }

  ll ans = 0, j = M;

  for (ll i = 1; i <= N; i++) {
    if (A[i] > K)
      break;
    while (A[i] + B[j] > K) {
      j--;
    }
    ans = max(i + j, ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define scn(n) scanf("%d", &n)
using namespace std;

int main() {

  int n, m;
  ll k;
  cin >> n >> m >> k;

  ll arr1[n + 2] = {0};
  ll arr2[m + 2] = {0};

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

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

  int j = m, ans = 0;

  for (int i = 0; i <= n; i++) {
    if (arr1[i] > k)
      break;
    while (arr2[j] > (k - arr1[i]))
      j--;

    ans = max(ans, (i + j));
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  long long k;
  cin >> n >> m >> k;
  vector<long long int> v1(n + 2, 0);
  vector<long long int> v2(n + 2, 0);
  for (int i = 1; i < n; i++) {
    cin >> v1[i];
    v1[i] += v1[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> v2[i];
    v2[i] += v2[i - 1];
  }
  int j = m, ans = 0;
  for (int i = 0; i <= n; i++) {
    if (v1[i] > k)
      break;
    while (v2[j] > (k - v1[i]))
      j--;
    ans = max(ans, (i + j));
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  int n, m;
  long long k;
  cin >> n >> m >> k;

  vector<long long> arr1(n + 2, 0);
  vector<long long> arr2(m + 2, 0);

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

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

  int j = m, ans = 0;

  for (int i = 0; i <= n; i++) {
    if (arr1[i] > k)
      break;
    while (arr2[j] > (k - arr1[i]))
      j--;

    ans = max(ans, (i + j));
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  long long k;
  cin >> n >> m >> k;
  vector<long long int> v1(n + 2, 0);
  vector<long long int> v2(n + 2, 0);
  for (int i = 1; i < n; i++) {
    cin >> v1[i];
    v1[i] += v1[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> v2[i];
    v2[i] += v2[i - 1];
  }
  int j = m, ans = 0;
  for (int i = 0; i <= n; i++) {
    if (v1[i] > k)
      break;
    while (v2[j] > (k - v1[i]))
      j--;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = (0); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define DEBUG(val) cout << #val << " : " << val << endl
#define MOD 1000000007

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = (0); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define DEBUG(val) cout << #val << " : " << val << endl
#define MOD 1000000007

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

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

#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define F first
#define S second
#define ld long double
#define rep(i, a, n) for (ll i = (a); i < (n); ++i)
#define vi vector<ll>
#define all(v) (v).begin(), (v).end()
#define M_PI 3.14159265358979323846
const ll MOD = 1e9 + 7;
const ll INF = 1e18L + 5;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vi a(n), b(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];

  int lena = 0;
  rep(i, 0, n) {
    if (i != 0)
      a[i] += a[i - 1];
    // cout<<a[i]<<" ";
    if (a[i] > k) {
      lena = i + 1;
      break;
    }
  }
  // cout<<endl;
  // cout<<lena<<endl;

  int lenb = 0;
  rep(i, 0, m) {
    if (i != 0)
      b[i] += b[i - 1];

    if (b[i] > k) {
      lenb = i + 1;
      break;
    }
  }
  if (lena == 0)
    lena = n + 1;
  if (lenb == 0)
    lenb = m + 1;

  // cout<<lena<<' '<<lenb<<endl;
  ll ans = max(lena, lenb) - 1, ca = lena - 1;

  int i = lena - 2, j = 0;

  while (i >= 0 && j < lenb - 1) {

    if (a[i] + b[j] <= k) {
      j++;
      ca++;
    } else {
      i--;
      ca--;
    }
    ans = max(ans, ca);
  }

  cout << ans << endl;
}

int main() {

  int t = 1;

  // cin >> t;
  ll k = 1;
  while (t--) {
    // cout << "Case #" << k++ << ": ";
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define F first
#define S second
#define ld long double
#define rep(i, a, n) for (ll i = (a); i < (n); ++i)
#define vi vector<ll>
#define all(v) (v).begin(), (v).end()
#define M_PI 3.14159265358979323846
const ll MOD = 1e9 + 7;
const ll INF = 1e18L + 5;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vi a(n), b(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];

  int lena = 0;
  rep(i, 0, n) {
    if (i != 0)
      a[i] += a[i - 1];
    // cout<<a[i]<<" ";
    if (a[i] > k) {
      lena = i + 1;
      break;
    }
  }
  // cout<<endl;
  // cout<<lena<<endl;

  int lenb = 0;
  rep(i, 0, n) {
    if (i != 0)
      b[i] += b[i - 1];

    if (b[i] > k) {
      lenb = i + 1;
      break;
    }
  }
  if (lena == 0)
    lena = n + 1;
  if (lenb == 0)
    lenb = m + 1;

  // cout<<lena<<' '<<lenb<<endl;
  int ans = max(lena, lenb) - 1, ca = lena - 1;

  int i = lena - 2, j = 0;

  while (i >= 0 && j < lenb - 1) {

    if (a[i] + b[j] <= k) {
      j++;
      ca++;
    } else {
      i--;
      ca--;
    }
    ans = max(ans, ca);
  }

  cout << ans << endl;
}

int main() {

  int t = 1;

  // cin >> t;
  ll k = 1;
  while (t--) {
    // cout << "Case #" << k++ << ": ";
    solve();
  }
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1000000007;
const ll mod = 1000000007;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int cans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

int main() {
  ll N, M, K;
  ll ans = 0;
  cin >> N >> M >> K;
  ll a[N];
  ll b[M];
  vector<ll> Acum(N + 1, 0);
  vector<ll> Bcum(M + 1, 0);
  Acum[0] = 0;
  Bcum[0] = 0;

  ll posb = 0;
  rep(i, N) {
    cin >> a[i];
    Acum[i + 1] += Acum[i] + a[i];
  }
  rep(i, M) {
    cin >> b[i];
    Bcum[i + 1] += Bcum[i] + b[i];
  }
  posb = M;
  for (ll posa = 0; posa < N + 1; posa++) {
    if (K < Acum[posa])
      break;
    while (Acum[posa] + Bcum[posb] > K) {
      posb--;
      if (posb == 0)
        break;
    }
    //	cout << posa << " " << posb << endl;
    ans = max(ans, posa + posb);
  }
  cout << ans << endl;

  return 0;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1000000007;
const ll mod = 1000000007;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int cans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

int main() {
  ll N, M, K;
  ll ans = 0;
  cin >> N >> M >> K;
  ll a[N];
  ll b[M];
  vector<ll> Acum(N, 0);
  vector<ll> Bcum(M, 0);
  Acum[0] = 0;
  Bcum[0] = 0;

  ll posb = 0;
  rep(i, N) {
    cin >> a[i];
    Acum[i + 1] += Acum[i] + a[i];
  }
  rep(i, M) {
    cin >> b[i];
    Bcum[i + 1] += Bcum[i] + b[i];
  }
  posb = M;
  for (ll posa = 0; posa < N + 1; posa++) {
    if (K < Acum[posa])
      break;
    while (Acum[posa] + Bcum[posb] > K) {
      posb--;
      if (posb == 0)
        break;
    }
    //	cout << posa << " " << posb << endl;
    ans = max(ans, posa + posb);
  }
  cout << ans << endl;

  return 0;
}