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

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

int N, M, K, A[300006], B[300006], ans;
int main() {
  scanf("%d %d %d", &N, &M, &K);
  int k = K;
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  for (int i = 0; i < M; i++) {
    scanf("%d", &B[i]);
  }
  for (int i = 0; i < N; i++) {
    if (K >= A[i]) {
      K -= A[i];
      ans++;
    } else {
      break;
    }
  }
  int pos = 0;
  /*if (ans == N) {
          for (int i = 0; i < M; i++) {
                  if (K >= B[i]) {
                          K -= B[i];
                          ans++;
                  } else {
                          pos = i;
                          break;
                  }
                  if (i == M - 1) {
                          pos = i;
                  }
          }
  }*/
  int cou = ans;
  for (int i = ans - 1; i >= 0; i--) {
    // printf("%d\\n", ans);
    // if (pos == M - 1) {
    //	break;
    // }
    for (int j = pos; j < M; j++) {
      if (K >= B[j]) {
        K -= B[j];
        cou++;
      } else {
        pos = j;
        break;
      }
      if (j == M - 1) {
        pos = M;
      }
    }
    ans = max(ans, cou);
    cou--;
    K += A[i];
  }
  cou = 0;
  K = k;
  for (int i = 0; i < M; i++) {
    if (K >= B[i]) {
      K -= B[i];
      cou++;
    } else {
      ans = max(ans, cou);
      break;
    }
    if (i == M - 1) {
      ans = max(ans, cou);
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

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

int N, M, K, A[300006], B[300006], ans;
int main() {
  scanf("%d %d %d", &N, &M, &K);
  int k = K;
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  for (int i = 0; i < M; i++) {
    scanf("%d", &B[i]);
  }
  for (int i = 0; i < N; i++) {
    if (K >= A[i]) {
      K -= A[i];
      ans++;
    } else {
      break;
    }
  }
  int pos = 0;
  if (ans == N) {
    for (int i = 0; i < M; i++) {
      if (K >= B[i]) {
        K -= B[i];
        ans++;
      } else {
        pos = i;
        break;
      }
      if (i == M - 1) {
        pos = i;
      }
    }
  }
  int cou = ans;
  for (int i = ans - 1; i >= 0; i--) {
    // printf("%d\\n", ans);
    if (pos == M - 1) {
      break;
    }
    for (int j = pos; j < M; j++) {
      if (K >= B[j]) {
        K -= B[j];
        cou++;
      } else {
        pos = j;
        break;
      }
      if (j == M - 1) {
        pos = j;
      }
    }
    ans = max(ans, cou);
    cou--;
    K += A[i];
  }
  cou = 0;
  K = k;
  for (int i = 0; i < M; i++) {
    if (K >= B[i]) {
      K -= B[i];
      cou++;
    } else {
      ans = max(ans, cou);
      break;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
#define ll long long
int vectPrint(const vector<ll> &A) {
  int size = A.size();
  for (int i = 0; i < size; i++) {
    cout << A[i] << " ";
  }
  cout << endl;
  return 0;
}
ll max2val(ll a, ll b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

int main(void) {
  // Your code here!
  ll N, M, K;
  ll ans, j;

  std::cin >> N;
  std::cin >> M;
  std::cin >> K;
  vector<ll> vecN(N, 0), vecM(M, 0);
  vector<ll> a(1), b(1);

  for (ll i = 0; i < N; i++) {
    cin >> vecN[i];
  }
  for (ll i = 0; i < M; i++) {
    cin >> vecM[i];
  }
  // 単純増加列
  for (ll i = 0; i < N; i++) {
    a.push_back(a[i] + vecN[i]);
  }
  for (ll i = 0; i < M; i++) {
    b.push_back(b[i] + vecM[i]);
  }

  ans = 0;
  j = M;
  for (ll i = 0; i < N + 1; i++) {
    if (a[i] > K)
      break;
    while (b[j] > K - a[i]) {
      j -= 1;
    }
    ans = max2val(ans, i + j);
  }

  cout << ans;

  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  // Your code here!
  int N, M, K;
  std::cin >> N;
  std::cin >> M;
  std::cin >> K;
  int cnt = 0;
  int bookite = 0;
  int bookcnt;
  int Nsum = 0;

  // cout <<N<<M<<K;

  vector<int> vecN(N, 0), vecM(M, 0);
  for (int i = 0; i < N; i++) {
    cin >> vecN[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> vecM[i];
  }

  // Nの方が小さい
  if (M > N) {
    int tmp;
    tmp = N;
    N = M;
    M = tmp;
  }
  for (int i = 0; i < N; i++) {
    Nsum += vecN[i] + vecM[i];
  }
  if (K < Nsum) {
    while (K > cnt) {
      cnt += vecN[bookite];
      cnt += vecM[bookite];
      bookite++;
    }
    // cout <<cnt<<" ";

    // cout << bookite<< " ";
    if (K >= cnt - vecM[bookite - 1]) {
      bookcnt = (bookite - 1) * 2 + 1;
    } else if (K >= cnt - vecN[bookite - 1] - vecM[bookite - 1]) {
      bookcnt = (bookite - 1) * 2;
    }
  } else {
    cnt = Nsum;
    bookite = N;
    if (vecN.size() < vecM.size()) {
      while (K > cnt) {
        cnt += vecM[bookite];
        bookite++;
      }
    } else {
      while (K > cnt) {
        cnt += vecN[bookite];
        bookite++;
      }
    }
    bookite += -1;
    bookcnt = 2 * N + bookite - N - 1;
  }
  cout << bookcnt;

  // cout <<cnt<<" ";
  // for(int i =0;i<N;i++){
  //     cout << vecN[i]<<" ";
  // }
  // for(int i =0;i<M;i++){
  //     cout << vecM[i]<<" ";
  // }

  // cout <<N <<" "<<M<<" "<<K;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#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 + 1), b(m + 1);
  vector<ll> A(n + 1), B(m + 1);
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    a[i] = a[i - 1] + A[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> B[i];
    b[i] = b[i - 1] + B[i];
  }
  int j = m, ans = 0;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    while (b[j] > k - a[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#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), b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  a.insert(a.begin(), 0);
  b.insert(b.begin(), 0);
  for (int i = 0; i < n; i++)
    a[i + 1] += a[i];
  for (int i = 0; i < n; i++)
    b[i + 1] += b[i];
  int j = m, ans = 0;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    while (a[i] + b[j] > k && j > 0) {
      j--;
    }
    if (a[i] + b[j] <= k)
      ans = max(ans, i + j);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define GET_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define LL long long
#define VI vector<int>
#define VL vector<LL>
#define VII vector<vector<int>>
#define VLL vector<vector<LL>>
#define VS vector<string>
#define PII pair<int, int>
#define PIS pair<int, string>
#define PSI pair<string, int>
#define PLL pair<ll, ll>
LL facctorialMethod(int k);
int main() {
  LL n, m, k, rem, a_num, b_num, count;
  LL a_tmp_count, b_tmp_count, a_tmp_sum, b_tmp_sum;
  cin >> n >> m >> k;
  VL a(n);
  VL b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a.push_back(10e10);
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  b.push_back(10e10);

  b_tmp_sum = 0;
  rep(i, m) b_tmp_sum += b[i];
  int last = m - 1;
  LL ans = 0;
  a_tmp_sum = 0;
  while (b_tmp_sum > k) {
    b_tmp_sum -= b[last];
    last--;
  }
  ans = last + 1;
  rep(i, n) {
    a_tmp_sum += a[i];
    while (a_tmp_sum + b_tmp_sum > k) {
      if (last == -1)
        break;
      b_tmp_sum -= b[last];
      last--;
    }
    if (a_tmp_sum + b_tmp_sum <= k) {
      if (ans < (i + 1 + last + 1)) {
        ans = (i + 1 + last + 1);
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define GET_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define LL long long
#define VI vector<int>
#define VL vector<LL>
#define VII vector<vector<int>>
#define VLL vector<vector<LL>>
#define VS vector<string>
#define PII pair<int, int>
#define PIS pair<int, string>
#define PSI pair<string, int>
#define PLL pair<ll, ll>
LL facctorialMethod(int k);
int main() {
  LL n, m, k, rem, a_num, b_num, count;
  LL a_tmp_count, b_tmp_count, a_tmp_rem, b_tmp_rem;
  cin >> n >> m >> k;
  VL a(n);
  VL b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a.push_back(10e10);
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  b.push_back(10e10);

  rem = k;
  a_num = 0;
  b_num = 0;
  count = 0;
  //    cout << a[3] << endl;

  while (rem > 0 && k >= a[0] && k >= b[0]) {
    a_tmp_count = 0;
    b_tmp_count = 0;
    a_tmp_rem = rem;
    b_tmp_rem = rem;
    for (int i = a_num; i < n; i++) {
      if (a_tmp_rem >= a[i]) {
        a_tmp_rem = a_tmp_rem - a[i];
        a_tmp_count++;
      } else {
        break;
      }
    }
    for (int i = b_num; i < m; i++) {
      if (b_tmp_rem >= b[i]) {
        b_tmp_rem = b_tmp_rem - b[i];
        b_tmp_count++;
      } else {
        break;
      }
    }

    if (a_tmp_rem < b_tmp_rem) {
      //            cout << "a[a_num] :" << a[a_num] << endl;
      rem = rem - a[a_num];
      a_num++;
      count++;
    } else {
      //            cout << "b[b_num] :" << b[b_num] << endl;
      rem = rem - b[b_num];
      b_num++;
      count++;
    }
    //        cout << "rem :" << rem << endl;
  }
  cout << count << endl;

  return 0;
}

LL facctorialMethod(int k) {
  int sum = 1;
  for (int i = 1; i <= k; ++i) {
    sum *= i;
  }
  return sum;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;

ll ans;
vector<ll> sa, sb;
ll a[200005], b[200005];

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

  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 1, m + 1) cin >> b[i];

  sa.pb(0);
  sb.pb(0);
  rep(i, 1, n + 1) sa.pb(sa[i - 1] + a[i]);
  rep(i, 1, m + 1) sb.pb(sb[i - 1] + b[i]);

  for (ll i = 0; i <= n; i++) {
    ll sum = sa[i];
    if (sum > k)
      break;
    ll x = upper_bound(sb.bg, sb.en, k - sum) - sb.bg;
    x--;

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

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


Error: -6
free(): invalid size


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;

ll ans;

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

  vector<ll> sa(n + 2), sb(m + 2);
  rep(i, 1, n + 1) sa[i] = sa[i - 1] + a[i];
  rep(i, 1, m + 1) sb[i] = sb[i - 1] + a[i];

  for (ll i = 0; i <= n; i++) {
    ll sum = sa[i];
    ll j = 0;
    while (sum + sb[j] <= k && j <= m)
      j++;
    j--;
    sum += sb[j];
    if (sum <= k)
      ans = max(ans, i + j);
  }

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

#include <bits/stdc++.h>
using namespace std;
long long a[200005], b[200005], n, m, k, l, r, sum, f, mx;
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    if (f == 1)
      continue;
    sum += b[i];
    r++;
    if (sum > k) {
      sum -= b[i];
      r--;
      f = 1;
    }
  }
  mx = r;
  while (l <= n && r >= 0) {
    if (sum > k)
      sum -= b[r--];
    else {
      mx = max(mx, l + r);
      sum += a[++l];
    }
  }
  cout << mx;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[200005], b[200005], n, m, k, l, r, sum, f, mx;
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    if (f == 1)
      continue;
    sum += b[i];
    r++;
    if (sum > k) {
      sum -= b[i];
      r--;
      f = 1;
    }
  }
  mx = r;
  while (l <= n) {
    if (sum > k)
      sum -= b[r--];
    else {
      mx = max(mx, l + r);
      sum += a[++l];
    }
  }
  cout << mx;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

void insertVal(vector<long long> &v, int elements) {
  for (int i = 1; i <= elements; i++) {
    int x;
    cin >> x;
    v.push_back(v[i - 1] + x);
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long> a;
  vector<long long> b;
  a.push_back(0);
  b.push_back(0);
  insertVal(a, n);
  insertVal(b, m);
  int max = 0, check = b.size() - 1;
  int s1 = a.size();
  for (int i = 0; i < s1; i++) {
    for (int j = check; j >= 0; j--) {
      if (b[j] + a[i] <= k) {
        max = ((i + j) > max) ? (i + j) : max;
        check = j;
        break;
      }
    }
  }

  cout << max;
  return 0;
}

Error: WA

Fixed submission:

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

int totalBooks(vector<int> a, vector<int> b, int ai, int bi, int k) {
  if (k == 0 || (k < a[ai] && k < b[bi])) {
    return 0;
  } else {
    if (k >= a[ai] && k >= b[bi]) {
      int ac, bc;
      ac = totalBooks(a, b, ai + 1, bi, k - a[ai]);
      bc = totalBooks(a, b, ai, bi + 1, k - b[bi]);
      return (ac > bc) ? ac + 1 : bc + 1;
    } else if (k >= a[ai]) {
      int ac;
      ac = totalBooks(a, b, ai + 1, bi, k - a[ai]);
      return ac + 1;
    } else if (k >= b[bi]) {
      int bc;
      bc = totalBooks(a, b, ai, bi + 1, k - b[bi]);
      return bc + 1;
    }
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a;
  vector<int> b;
  while (n > 0) {
    int ka;
    cin >> ka;
    a.push_back(ka);
    n--;
  }
  while (m > 0) {
    int kb;
    cin >> kb;
    b.push_back(kb);
    m--;
  }
  int ai = 0, bi = 0;
  int result = totalBooks(a, b, ai, bi, k);
  cout << result;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  int N, M;
  ll r, l;
  ll K;
  ll ans = 0;
  ll sum = 0;

  cin >> N >> M >> K;
  vector<ll> A(N + 1);
  vector<ll> B(M + 1);
  rep(i, N) cin >> A[i + 1];
  rep(i, M) cin >> B[i + 1];

  for (l = 0; sum + B[l] <= K && l < M + 1; l++)
    sum += B[l];
  l--;

  for (r = 0; r < N + 1; r++) {
    sum += A[r];
    while (sum > K && l > 0) {
      sum -= B[l];
      l--;
    }

    // cout << r << " " << l << " " << sum << endl;
    if (sum > K)
      break;
    ans = max(ans, r + l);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

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

  int N, M;
  int i, j;
  ll r, l;
  ll K;
  ll ans = 0;
  ll sum = 0;
  ll counter = 0;

  cin >> N >> M >> K;
  vector<ll> A(N + 1);
  vector<ll> B(M + 1);
  rep(i, N) cin >> A[i + 1];
  rep(i, M) cin >> B[i + 1];

  for (l = 0; sum + B[l] <= K; l++)
    sum += B[l];
  l--;

  for (r = 0; r < N + 1; r++) {
    sum += A[r];
    while (sum > K && l > 0) {
      sum -= B[l];
      l--;
    }

    // cout << r << " " << l << " " << sum << endl;
    if (sum > K)
      break;
    ans = max(ans, r + l);
  }

  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 a;
  cin >> a;
  ll b;
  cin >> b;
  ll c;
  cin >> c;
  ll f = -1;
  vector<ll> vec(a);
  vector<ll> veco(0);
  for (ll i = 0; i < a; i++) {
    if (i == 0) {
      cin >> vec.at(i);
    } else {
      ll d;
      cin >> d;
      vec.at(i) = vec.at(i - 1) + d;
    }
    if (vec.at(i) > c && f == -1) {
      f = max(f, i);
    }
    if (i == a - 1 && f == -1 && vec.at(i) <= c) {
      f = max(f, i + 1);
    }
  }

  for (ll i = 0; i < b; i++) {
    if (i == 0) {
      ll e;
      cin >> e;
      veco.push_back(e);
    } else {
      ll e;
      cin >> e;
      veco.push_back(e + veco.at(i - 1));
    }
    if (veco.at(i) > c) {
      f = max(f, i);
      break;
    }
    if (i == b - 1) {
      f = max(f, i + 1);
    }
  }

  ll x = veco.size() - 1;
  for (ll i = 0; i < vec.size() && vec.at(i) <= c; i++) {
    ll g = c - vec.at(i);
    if (f > i + 2) {
      if (f - 2 - i >= veco.size()) {
        continue;
      } else if (veco.at(f - 2 - i) > g) {
        continue;
      }
    }

    for (ll j = x; j >= 0; j--) {
      if (veco.at(j) <= g) {
        f = max(i + j + 2, f);
        x = j;
        break;
      }
      if (j == 0) {
        f = max(f, i + 1);
      }
    }
  }
  cout << f << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll a;
  cin >> a;
  ll b;
  cin >> b;
  ll c;
  cin >> c;
  ll f = 0;
  vector<ll> vec(a);
  vector<ll> veco(0);
  for (ll i = 0; i < a; i++) {
    if (i == 0) {
      cin >> vec.at(i);
    } else {
      ll d;
      cin >> d;
      vec.at(i) = vec.at(i - 1) + d;
      if (vec.at(i) > c && f == 0) {
        f = max(f, i);
      }
    }
  }

  for (ll i = 0; i < b; i++) {
    if (i == 0) {
      ll e;
      cin >> e;
      veco.push_back(e);
    } else {
      ll e;
      cin >> e;
      veco.push_back(e + veco.at(i - 1));
      if (veco.at(i) > c) {
        f = max(f, i);
        break;
      }
    }
  }
  ll x = veco.size() - 1;
  for (ll i = 0; i < vec.size() && vec.at(i) <= c; i++) {
    ll g = c - vec.at(i);
    if (f > i + 1) {
      if (f - 2 - i >= b) {
        continue;
      } else if (veco.at(f - 2 - i) > g) {
        continue;
      }
    }

    for (ll j = x; j >= 0; j--) {
      if (veco.at(j) <= g) {
        f = max(i + j + 2, f);
        x = j;
        break;
      }
      if (j == 0) {
        f = max(f, i + 1);
      }
    }
  }
  cout << f << endl;

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

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll tot = 0;
  rep(i, m) tot += b[i];
  int j = m;
  int ans = 0;
  rep(i, n + 1) {
    while (j > 0 && tot > k) {
      j--;
      tot -= b[j];
    }
    if (tot > k)
      break;
    ans = max(ans, i + j);
    if (i == n)
      break;
    tot += a[i];
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

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

typedef long long ll;

int main(void) {
  int N, M;
  ll K;
  cin >> N >> M >> K;

  queue<ll> A;
  queue<ll> B;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A.push(tmp);
  }
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    B.push(tmp);
  }

  int cnt = 0;
  ll sum = min(A.front(), B.front());
  while (sum <= K) {
    if (A.empty() && B.empty()) {
      break;
    }
    if (cnt == 0) {
      if (A.front() == min(A.front(), B.front())) {
        cnt++;
        A.pop();
      } else {
        cnt++;
        B.pop();
      }
    }

    if (sum <= K && A.empty()) {
      while (sum <= K) {
        sum += B.front();
        cnt++;
        B.pop();
        if (B.empty())
          break;
      }
    } else if (sum <= K && B.empty()) {
      while (sum <= K && !A.empty()) {
        sum += A.front();
        cnt++;
        A.pop();
        if (A.empty())
          break;
      }
    }

    sum += min(A.front(), B.front());

    if (A.front() == min(A.front(), B.front())) {
      cnt++;
      A.pop();
    } else {
      cnt++;
      B.pop();
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using vll = vector<ll>;
#define all(v) v.begin(), v.end()

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vll A(N + 1), B(M + 1);
  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }
  ll ans = 0;
  vll acum(N + 1, 0), bcum(M + 1, 0);
  acum[0] = 0;
  bcum[0] = 0;
  for (int i = 0; i < N; ++i) {
    acum[i + 1] = A[i] + acum[i];
  }
  for (int i = 0; i < M; ++i) {
    bcum[i + 1] = B[i] + bcum[i];
  }
  for (ll i = 0; i < N + 1; ++i) {
    ll tmp = acum[i];
    if (acum[i] > K)
      continue;
    long it = distance(bcum.begin(), upper_bound(all(bcum), K - tmp));
    if (it == 0)
      ans = max(ans, i);
    else
      ans = max(ans, i + it - 1);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using vll = vector<ll>;
#define all(v) v.begin(), v.end()

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vll A(N + 1), B(M + 1);
  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }
  ll ans = 0;
  vll acum(N + 1, 0), bcum(N + 1, 0);
  acum[0] = 0;
  bcum[0] = 0;
  for (int i = 0; i < N; ++i) {
    acum[i + 1] = A[i] + acum[i];
  }
  for (int i = 0; i < M; ++i) {
    bcum[i + 1] = B[i] + bcum[i];
  }
  for (ll i = 0; i < N + 1; ++i) {
    ll tmp = acum[i];
    if (acum[i] > K)
      continue;
    auto it = upper_bound(all(bcum), K - tmp) - bcum.begin();
    if (it == 0)
      ans = max(ans, i);
    if (it == M)
      ans = max(ans, i + it);
    else
      ans = max(ans, i + it - 1);
    // cout <<"i " << i << "it " << it << "ans " << ans << endl;
  }
  cout << ans << endl;
}