prithivMLmods/Coder-Stat huggingface dataset:
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define maxn 500005
#define inf int(21e8)
#define mod int(1e9 + 7)
#define db double
#define ll long long
#define si stack<int>
#define qi queue<int>
#define vi vector<int>
#define pi pair<int, int>
#define pq priority_queue<int, vector<int>, greater<int>>
using namespace std;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  for (; b; b >>= 1)
    res = (res * ((b & 1) ? a : 1)) % mod, a = a * a % mod;
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(a, b) : a; }
ll a[500005], b[500005];
ll sa[500005], sb[500005];
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  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];
  int now = 1, ans = 0;
  for (int i = n; i >= 1; i--) {
    while (sa[i] + sb[now] <= k && now <= m)
      now++;
    now--;
    ans = max(ans, i + now);
  }
  cout << ans << endl;
  system("pause");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define maxn 500005
#define inf int(21e8)
#define mod int(1e9 + 7)
#define db double
#define ll long long
#define si stack<int>
#define qi queue<int>
#define vi vector<int>
#define pi pair<int, int>
#define pq priority_queue<int, vector<int>, greater<int>>
using namespace std;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  for (; b; b >>= 1)
    res = (res * ((b & 1) ? a : 1)) % mod, a = a * a % mod;
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(a, b) : a; }
ll a[500005], b[500005];
ll sa[500005], sb[500005];
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  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];
  int now = m, ans = 0;
  for (int i = 0; i <= n; i++) {
    while (sa[i] + sb[now] > k && now > 0)
      now--;
    if (sa[i] + sb[now] <= k)
      ans = max(ans, i + now);
  }
  cout << ans << endl;
  system("pause");
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  long long K;
  cin >> K;
  long long a;
  vector<long long> A(N + 1);
  vector<long long> B(M + 1);
  A[0] = 0;
  B[0] = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i < M + 1; i++) {
    cin >> a;
    B[i] = B[i - 1] + a;
  }
  int ans = 0;
  int J = M;
  for (int i = 0; i < N + 1; i++) {
    if (A[i] > K)
      break;
    for (int j = 0; j < J + 1; j++) {
      if ((A[i] + B[j]) > K) {
        J = j;
        break;
      }
      ans = max(ans, i + j);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  long long K;
  cin >> K;
  long long a;
  vector<long long> A(N + 1);
  vector<long long> B(M + 1);
  A[0] = 0;
  B[0] = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i < M + 1; i++) {
    cin >> a;
    B[i] = B[i - 1] + a;
  }
  int ans = 0;
  int J = M;
  for (int i = 0; i < N + 1; i++) {
    if (A[i] > K)
      break;
    auto it = upper_bound(B.begin(), B.end(), K - A[i]);
    int j = it - B.begin();
    ans = max(ans, i + j - 1);
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define SZ(c) int(c.size())
#define pb push_back
#define loop(i, start, n) for (int i = start; i < n; i++)
#define rloop(i, start, n) for (int i = start; i > n; i--)

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
const ll inf = 2e9 + 3;

void solve() {
  int tc = 1;
  // cin >> tc;
  while (tc--) {
    ll n, m, k, res = 0;
    cin >> n >> m >> k;
    vll va(n + 1), vb(m + 1);
    ll A = 0, B = 0;
    loop(i, 1, n + 1) {
      cin >> va[i];
      va[i] = va[i] + va[i - 1];
    }
    loop(i, 1, m + 1) {
      cin >> vb[i];
      vb[i] = vb[i] + vb[i - 1];
    }
    ll b = m, a = n;
    loop(i, 1, n + 1) if (va[i] <= k) a = i;
    loop(i, 1, m + 1) if (vb[i] <= k) b = i;
    // res = a;
    rloop(i, a, -1) {
      loop(j, 0, b + 1) {
        if (va[i] + vb[j] <= k)
          res = max(res, (ll)(i + j));
      }
    }
    cout << res;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define SZ(c) int(c.size())
#define pb push_back
#define loop(i, start, n) for (int i = start; i < n; i++)
#define rloop(i, start, n) for (int i = start; i > n; i--)

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
const ll inf = 2e9 + 3;

void solve() {
  int tc = 1;
  // cin >> tc;
  while (tc--) {
    ll n, m, k, res = 0;
    cin >> n >> m >> k;
    vll va(n + 1), vb(m + 1);
    ll A = 0, B = 0;
    loop(i, 1, n + 1) {
      cin >> va[i];
      va[i] = va[i] + va[i - 1];
    }
    loop(i, 1, m + 1) {
      cin >> vb[i];
      vb[i] = vb[i] + vb[i - 1];
    }
    ll b = m, a = n;
    loop(i, 1, n + 1) if (va[i] <= k) a = i;
    loop(i, 1, m + 1) if (vb[i] <= k) b = i;
    loop(i, 0, a + 1) {
      if (vb[b] <= k - va[i])
        res = max(res, i + b);
      else
        b--;
      /*loop(j,0,b+1) {
          if (va[i] + vb[j] <= k) res = max(res,(ll)(i+j));
      }*/
    }
    cout << res;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, K, ans;
long long a[100004], b[100004];
int main() {
  cin >> n >> m >> K;
  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];
  for (int i = 0; i <= n; i++)
    if (K >= a[i]) {
      int res = upper_bound(b + 1, b + 1 + m, K - a[i]) - b - 1;
      ans = max(ans, res + i);
    }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, K, ans;
long long a[200004], b[200004];
int main() {
  cin >> n >> m >> K;
  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];
  for (int i = 0; i <= n; i++)
    if (K >= a[i]) {
      int res = upper_bound(b + 1, b + 1 + m, K - a[i]) - b - 1;
      ans = max(ans, res + i);
    }
  cout << ans;
}
Problem p02623 (C++)

Buggy submission (status: 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, 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;
}


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 + 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;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF 1000000007
#define mod 1005
#define ll long long int
const double mx = 2e6 + 5;

void solve() {
  ll n, m, k;
  ll maxPos = 0;
  cin >> n >> m >> k;
  //    cout<<"nmk\n";
  vector<ll> a(n + 1), b(m + 1);
  a[0] = b[0] = 0;
  //    cout<<"workng\n";
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    //		cout<<a[i]<<" ";
  }
  //    cout<<"working\n";
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
    //		cout<<b[i]<<" ";
  }
  //	cout<<"\n";
  for (int i = 0; i <= n; i++) {
    ll diff = k - a[i];
    ll ans = 0;
    if (diff < 0)
      break;
    ll low = 1, high = m;
    while (low <= high) {
      ll mid = low + (high - low) / 2;
      if (b[mid] <= diff) {
        ans = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    maxPos = max(maxPos, ans + i);
  }
  cout << maxPos << "\n";
}
int main() {
  //    ios_base::sync_with_stdio(false);
  //    cin.tie(NULL);
  freopen("abc172dInput.txt", "r", stdin);
  int t = 1;
  //	cin>>t;
  while (t--) {
    //    	cout<<"ENTER\n";
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF 1000000007
#define mod 1005
#define ll long long int
const double mx = 2e6 + 5;

void solve() {
  ll n, m, k;
  ll maxPos = 0;
  cin >> n >> m >> k;
  //    cout<<"nmk\n";
  vector<ll> a(n + 1), b(m + 1);
  a[0] = b[0] = 0;
  //    cout<<"workng\n";
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    //		cout<<a[i]<<" ";
  }
  //    cout<<"working\n";
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
    //		cout<<b[i]<<" ";
  }
  //	cout<<"\n";
  for (int i = 0; i <= n; i++) {
    ll diff = k - a[i];
    ll ans = 0;
    if (diff < 0)
      break;
    ll low = 1, high = m;
    while (low <= high) {
      ll mid = low + (high - low) / 2;
      if (b[mid] <= diff) {
        ans = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    maxPos = max(maxPos, ans + i);
  }
  cout << maxPos << "\n";
}
int main() {
  //    ios_base::sync_with_stdio(false);
  //    cin.tie(NULL);
  //    freopen("abc172dInput.txt", "r", stdin);
  int t = 1;
  //	cin>>t;
  while (t--) {
    //    	cout<<"ENTER\n";
    solve();
  }
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: 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(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;
}

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(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;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

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

  vector<ll> c(n + 1), d(n + 1);
  rep(i, n) c[i + 1] = c[i] + a[i];
  rep(j, m) d[j + 1] = d[j] + b[j];

  ll ans = 0;
  rep(i, n + 1) {
    ll time = c[i];
    if (time > k)
      continue;
    int p = upper_bound(d.begin(), d.end(), k - time) - d.begin();
    if (p == m && time + d[m] <= k)
      ans = max(ans, (ll)p + i);
    else
      ans = max(ans, (ll)p + i - 1);
  }
  cout << ans << endl;
}

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

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

  vector<ll> c(n + 1), d(m + 1);
  rep(i, n) c[i + 1] = c[i] + a[i];
  rep(j, m) d[j + 1] = d[j] + b[j];

  ll ans = 0;
  rep(i, n + 1) {
    ll time = c[i];
    if (time > k)
      continue;
    int p = upper_bound(d.begin(), d.end(), k - time) - d.begin();
    if (p == m && time + d[m] <= k)
      ans = max(ans, (ll)p + i);
    else
      ans = max(ans, (ll)p + i - 1);
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int N, M, K;
  std::cin >> N >> M >> K;
  std::vector<unsigned long long> A, B;
  A.resize(N + 1);
  for (int i = 0; i < N; i++) {
    int tmp;
    std::cin >> tmp;
    A[i + 1] = A[i] + tmp;
  }
  B.resize(M + 1);
  for (int i = 0; i < M; i++) {
    int tmp;
    std::cin >> tmp;
    B[i + 1] = B[i] + tmp;
  }
  int max = 0;
  int an = std::upper_bound(A.begin(), A.end(), K) - A.begin();
  for (int i = 0; i < an; i++) {
    int j;
    for (j = 0; j < B.size(); j++) {
      if (A[i] + B[j] > K)
        break;
    }
    max = std::max(max, i + j - 1);
  }
  std::cout << max << std::endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int N, M, K;
  std::cin >> N >> M >> K;
  std::vector<unsigned long long> A, B;
  A.resize(N + 1);
  for (int i = 0; i < N; i++) {
    int tmp;
    std::cin >> tmp;
    A[i + 1] = A[i] + tmp;
  }
  B.resize(M + 1);
  for (int i = 0; i < M; i++) {
    int tmp;
    std::cin >> tmp;
    B[i + 1] = B[i] + tmp;
  }
  int max = 0;
  int an = std::upper_bound(A.begin(), A.end(), K) - A.begin() - 1;
  for (int i = an; i >= 0; i--) {
    int j = std::upper_bound(B.begin(), B.end(), K - A[i]) - B.begin();
    max = std::max(max, i + j - 1);
  }
  std::cout << max << std::endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<long long> vl;

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vl a(N + 2), b(M + 2);
  a[0] = 0;
  b[0] = 0;
  a[N + 1] = 1e12;
  b[N + 1] = 1e12;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= M; i++) {
    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];
  }
  long long ans = 0;
  for (int i = 0; i <= M; i++) {
    if (b[i] > K)
      break;
    long long index = upper_bound(a.begin(), a.end(), K - b[i]) - a.begin();
    // cout << i << "  " << index-1 << endl;
    if (index != 0)
      index--;
    ans = max(ans, i + index);
  }
  cout << ans << endl;
  return 0;
}

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

typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<long long> vl;

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vl a(N + 2), b(M + 2);
  a[0] = 0;
  b[0] = 0;
  a[N + 1] = 1e12;
  b[M + 1] = 1e12;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= M; i++) {
    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];
  }
  long long ans = 0;
  for (int i = 0; i <= M; i++) {
    if (b[i] > K)
      break;
    long long index = upper_bound(a.begin(), a.end(), K - b[i]) - a.begin();
    // cout << i << "  " << index-1 << endl;
    if (index != 0)
      index--;
    ans = max(ans, i + index);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: 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> a(n + 1, 0);
  vector<long> b(m + 1, 0);

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

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

  cout << max << endl;
}

Fixed submission:
#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> a(n + 1, 0);
  vector<long> b(m + 1, 0);

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

  int b_sum = m, max = 0;
  for (int i = 0; i <= n && a[i] <= k; i++) {
    while (a[i] + b[b_sum] > k)
      b_sum--;
    if (i + b_sum > max)
      max = i + b_sum;
  }

  cout << max << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):

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

int A[100002], B[100002];

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, m, ms;
    scanf("%d%d%d", &n, &m, &ms);

    for (int A_i = 0; A_i < n; A_i++) {
      scanf("%d", &A[A_i]);
    }
    for (int B_i = 0; B_i < m; B_i++) {
      scanf("%d", &B[B_i]);
    }

    long long sum = 0;
    int x = 0, y = 0;

    while (x < n && sum + A[x] <= ms) {
      sum += A[x++];
    }

    int ans = x;

    while (y < m && x >= 0) {
      sum += B[y++];
      while (sum > ms && x > 0) {
        sum -= A[--x];
      }

      if (sum <= ms && ans < x + y) {
        ans = x + y;
      }
    }

    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int A[N], B[N];

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, m, ms;
    scanf("%d%d%d", &n, &m, &ms);

    for (int A_i = 0; A_i < n; A_i++) {
      scanf("%d", &A[A_i]);
    }
    for (int B_i = 0; B_i < m; B_i++) {
      scanf("%d", &B[B_i]);
    }

    long long sum = 0;
    int x = 0, y = 0;

    while (x < n && sum + A[x] <= ms) {
      sum += A[x++];
    }

    int ans = x;

    while (y < m && x >= 0) {
      sum += B[y++];
      while (sum > ms && x > 0) {
        sum -= A[--x];
      }

      if (sum <= ms && ans < x + y) {
        ans = x + y;
      }
    }

    printf("%d\n", ans);
  }
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
template <class T = ll> inline T in(istream &is = cin) {
  T ret;
  is >> ret;
  return ret;
}
template <class T>
inline auto vec2(class vector<T>::size_type h, class vector<T>::size_type w,
                 T v = T()) {
  return vector<vector<T>>(h, vector<T>(w, v));
}
template <class RandomAccessIterator>
inline void psortb(RandomAccessIterator first, RandomAccessIterator last) {
  using P = pair<decltype(first->first), decltype(first->second)>;
  sort(first, last, [](const P &p1, const P &p2) {
    return p1.second < p2.second ||
           (p1.second == p2.second && p1.first < p2.first);
  });
}

int main() {
  ll n = in(), m = in(), k = in();
  vector<ll> sa(n + 1), sb(m + 1);
  for (ll i = 0; i < n; ++i)
    sa[i + 1] = in() + sa[i];
  for (ll i = 0; i < m; ++i)
    sb[i + 1] = in() + sb[i];

  ll ans = 0;

  ll idxa = n, idxb = 0;
  while (idxa >= 0 && idxb <= m) {
    if (sa[idxa] > k) {
      --idxa;
    } else if (sa[idxa] + sb[idxb] <= k) {
      ans = max(ans, idxa + idxb);
      idxb++;
    } else {
      --idxa;
    }
  }

  idxa = 0, idxb = m;
  while (idxa <= n && idxb >= 0) {
    if (sa[idxb] > k) {
      --idxb;
    } else if (sa[idxa] + sb[idxb] <= k) {
      ans = max(ans, idxa + idxb);
      idxa++;
    } else {
      --idxb;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
template <class T = ll> inline T in(istream &is = cin) {
  T ret;
  is >> ret;
  return ret;
}
template <class T>
inline auto vec2(class vector<T>::size_type h, class vector<T>::size_type w,
                 T v = T()) {
  return vector<vector<T>>(h, vector<T>(w, v));
}
template <class RandomAccessIterator>
inline void psortb(RandomAccessIterator first, RandomAccessIterator last) {
  using P = pair<decltype(first->first), decltype(first->second)>;
  sort(first, last, [](const P &p1, const P &p2) {
    return p1.second < p2.second ||
           (p1.second == p2.second && p1.first < p2.first);
  });
}

int main() {
  ll n = in(), m = in(), k = in();
  vector<ll> sa(n + 1), sb(m + 1);
  for (ll i = 0; i < n; ++i)
    sa[i + 1] = in() + sa[i];
  for (ll i = 0; i < m; ++i)
    sb[i + 1] = in() + sb[i];

  ll ans = 0;

  ll idxa = n, idxb = 0;
  while (idxa >= 0 && idxb <= m) {
    if (sa[idxa] > k) {
      --idxa;
    } else if (sa[idxa] + sb[idxb] <= k) {
      ans = max(ans, idxa + idxb);
      idxb++;
    } else {
      --idxa;
    }
  }

  idxa = 0, idxb = m;
  while (idxa <= n && idxb >= 0) {
    if (sb[idxb] > k) {
      --idxb;
    } else if (sa[idxa] + sb[idxb] <= k) {
      ans = max(ans, idxa + idxb);
      idxa++;
    } else {
      --idxb;
    }
  }

  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, r, n) for (int i = (int)(r); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define INF 2147483647
#define INF64 9223372036854775807
#define MOD 1000000007

int main() {

  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0), b(m + 1, 0);
  FOR(i, 1, n) {
    int64_t x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  FOR(i, 1, m) {
    int64_t x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }

  int64_t ans = 0;

  FOR(i, 0, n) {
    if (a[i] > k)
      break;
    int64_t x = n / 2;
    int64_t best = x;
    while (x > 1) {
      x /= 2;
      if (a[i] + b[best] > k)
        best -= x;
      if (a[i] + b[best] == k)
        break;
      if (a[i] + b[best] < k)
        best += x;
    }
    if (a[i] + b[best] > k) {
      while (a[i] + b[best] > k)
        best--;
    }
    if (a[i] + b[best] < k) {
      while (a[i] + b[best] <= k) {
        best++;
        if (best > m)
          break;
      }
      best--;
    }
    if (i + best > ans)
      ans = i + best;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, r, n) for (int i = (int)(r); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define INF 2147483647
#define INF64 9223372036854775807
#define MOD 1000000007

int main() {

  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0), b(m + 1, 0);
  FOR(i, 1, n) {
    int64_t x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  FOR(i, 1, m) {
    int64_t x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }

  int64_t ans = 0;

  FOR(i, 0, n) {
    if (a[i] > k)
      break;
    int64_t x = m / 2;
    int64_t best = x;
    while (x > 1) {
      x /= 2;
      if (a[i] + b[best] > k)
        best -= x;
      if (a[i] + b[best] == k)
        break;
      if (a[i] + b[best] < k)
        best += x;
    }
    if (a[i] + b[best] > k) {
      while (a[i] + b[best] > k)
        best--;
    }
    if (a[i] + b[best] < k) {
      while (a[i] + b[best] <= k) {
        best++;
        if (best > m)
          break;
      }
      best--;
    }
    if (i + best > ans)
      ans = i + best;
  }

  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int mxN = 2e5 + 10;
int n, m;
ll k, a[mxN], b[mxN];

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

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


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

#define ll long long

const int mxN = 2e5 + 10;
int n, m;
ll k, a[mxN], b[mxN];

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

  cin >> n >> m >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> b[i + 1];
    b[i + 1] += b[i];
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (a[i] > k)
      break;
    // int j = m;
    // while (a[i] + b[j] > k)
    //   j--;
    int j = upper_bound(b, b + m + 1, k - a[i]) - b;
    ans = max(i + j - 1, ans);
  }
  cout << ans << "\n";
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define llint long long int

using namespace std;

int main() {
  int N, M, K;

  cin >> N >> M >> K;

  vector<llint> A(N), B(M);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 1; i < N; i++) {
    A[i] += A[i - 1];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }
  for (int i = 1; i < M; i++) {
    B[i] += B[i - 1];
  }
  auto it = upper_bound(B.begin(), B.end(), K);

  int ans = (it - B.begin());

  for (int i = 0; A[i] < K; i++) {
    auto it = upper_bound(B.begin(), B.end(), K - A[i]);

    int temp = (i + 1) + (it - B.begin());

    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

#define llint long long int

using namespace std;

int main() {
  int N, M, K;

  cin >> N >> M >> K;

  vector<llint> A(N), B(M);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 1; i < N; i++) {
    A[i] += A[i - 1];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }
  for (int i = 1; i < M; i++) {
    B[i] += B[i - 1];
  }
  auto it = upper_bound(B.begin(), B.end(), K);

  int ans = (it - B.begin());

  for (int i = 0; i < N; i++) {
    if (A[i] > K) {
      break;
    }
    it = upper_bound(B.begin(), B.end(), K - A[i]);

    int temp = (i + 1) + (it - B.begin());

    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;

// int a[200001];
// int b[200001];
ll a[10];
ll b[10];
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  a[0] = 0;
  b[0] = 0;
  rep2(i, N) {
    int x;
    cin >> x;
    a[i] = i == 1 ? x : x + a[i - 1];
  }
  rep2(i, M) {
    int x;
    cin >> x;
    b[i] = i == 1 ? x : x + b[i - 1];
  }
  int j = M, ans = 0;
  for (int 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;
}

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

ll a[200001];
ll b[200001];
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  a[0] = 0;
  b[0] = 0;
  rep2(i, N) {
    int x;
    cin >> x;
    a[i] = i == 1 ? x : x + a[i - 1];
  }
  rep2(i, M) {
    int x;
    cin >> x;
    b[i] = i == 1 ? x : x + b[i - 1];
  }
  int j = M, ans = 0;
  for (int 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;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void YN(bool ans) {
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  v A(N), B(M);
  rep(i, 0, N) cin >> A[i];
  rep(i, 0, M) cin >> B[i];
  ll sum = 0, books = 0, mb = 0;

  while (books < N) {
    if (A[books] + sum <= K) {
      sum += A[books];
      books++;
    } else {
      break;
    }
  }
  mb = books;
  ll i = books - 1;
  bool ok = true;

  rep(j, 0, M) {
    while (B[j] + sum > K) {
      if (i == -1) {
        ok = false;
      }
      sum -= A[i];
      i--;
      books--;
    }
    sum += B[j];
    books++;
    if (ok)
      mb = max(mb, books);
  }
  cout << mb;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void YN(bool ans) {
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  v A(N), B(M);
  rep(i, 0, N) cin >> A[i];
  rep(i, 0, M) cin >> B[i];
  ll sum = 0, books = 0, mb = 0;

  while (books < N) {
    if (A[books] + sum <= K) {
      sum += A[books];
      books++;
    } else {
      break;
    }
  }
  mb = books;
  ll i = books - 1;
  bool ok = true;

  rep(j, 0, M) {
    while (B[j] + sum > K) {
      if (i == -1) {
        ok = false;
        break;
      }
      sum -= A[i];
      i--;
      books--;
    }
    sum += B[j];
    books++;
    if (ok)
      mb = max(mb, books);
  }
  cout << mb;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define _                                                                      \
  ios::sync_with_stdio(false);                                                 \
  cout.tie(NULL);                                                              \
  cin.tie(NULL);
using namespace std;

int main() {
  _ ll n, m, k, sum = 0, ans = 0, max1 = 0, l = 0, r = 0;
  cin >> n >> m >> k;
  deque<ll> dq1, dq2;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    dq1.push_back(x);
  }
  for (ll i = 0; i < m; i++) {
    ll x;
    cin >> x;
    dq2.push_back(x);
  }
  while (l < n) {
    if (sum + dq1[l] <= k) {
      sum += dq1[l];
      ans++;
      l++;
    } else {
      l--;
      break;
    }
  }
  if (l == n)
    l--;
  max1 = max(max1, ans);
  while (l >= -1 && r < m) {
    if (sum + dq2[r] <= k) {
      sum += dq2[r];
      ans++;
      r++;
    } else {
      sum -= dq1[l];
      l--;
      ans--;
    }
    max1 = max(ans, max1);
  }
  max1 = max(ans, max1);
  cout << max1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define _                                                                      \
  ios::sync_with_stdio(false);                                                 \
  cout.tie(NULL);                                                              \
  cin.tie(NULL);
using namespace std;

int main() {
  _ ll n, m, k, sum = 0, ans = 0, max1 = 0, l = 0, r = 0;
  cin >> n >> m >> k;
  deque<ll> dq1, dq2;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    dq1.push_back(x);
  }
  for (ll i = 0; i < m; i++) {
    ll x;
    cin >> x;
    dq2.push_back(x);
  }
  while (l < n) {
    if (sum + dq1[l] <= k) {
      sum += dq1[l];
      ans++;
      l++;
    } else {
      l--;
      break;
    }
  }
  if (l == n)
    l--;
  max1 = max(max1, ans);
  while (l >= -1 && r < m) {
    if (sum + dq2[r] <= k) {
      sum += dq2[r];
      ans++;
      r++;
    } else {
      if (l < 0)
        break;
      sum -= dq1[l];
      l--;
      ans--;
    }
    max1 = max(ans, max1);
  }
  max1 = max(ans, max1);
  cout << max1 << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const double PI = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

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

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const double PI = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

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

  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 5e5 + 1;
const ll INF = 1e10;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  for (ll &i : a)
    cin >> i;
  for (ll &i : b)
    cin >> i;
  int ans = 0, aptr = 0, bptr = 0;
  ll sum = 0;
  while (aptr < n and sum + a[aptr] <= k)
    sum += a[aptr++];
  ans = aptr;
  while (aptr) {
    while (bptr < m and sum + b[bptr] <= k)
      sum += b[bptr++];
    ans = max(ans, aptr + bptr);
    sum -= a[--aptr];
  }
  bptr = 0, sum = 0;
  while (sum + b[bptr] <= k)
    sum += b[bptr++];
  ans = max(ans, bptr);
  cout << ans;
}


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

typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 5e5 + 1;
const ll INF = 1e10;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  for (ll &i : a)
    cin >> i;
  for (ll &i : b)
    cin >> i;
  int ans = 0, aptr = 0, bptr = 0;
  ll sum = 0;
  while (aptr < n and sum + a[aptr] <= k)
    sum += a[aptr++];
  ans = aptr;
  while (aptr) {
    while (bptr < m and sum + b[bptr] <= k)
      sum += b[bptr++];
    ans = max(ans, aptr + bptr);
    sum -= a[--aptr];
  }
  bptr = 0, sum = 0;
  while (bptr < m and sum + b[bptr] <= k)
    sum += b[bptr++];
  ans = max(ans, bptr);
  cout << ans;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<long long> A, B;
  long long K;
  long long N, M;
  cin >> N >> M >> K;
  vector<long long> Sum_A(N + 1, 0), Sum_B(M + 1, 0);
  int tmp;
  Sum_A.push_back(0);
  for (int i = 1; i <= N; i++) {
    cin >> tmp;
    A.push_back(tmp);
    Sum_A[i] = Sum_A[i - 1] + tmp;
  }
  Sum_B.push_back(0);
  for (int i = 1; i <= M; i++) {
    cin >> tmp;
    B.push_back(tmp);
    Sum_B[i] = Sum_B[i - 1] + tmp;
  }
  int ans = 0;
  long long p = M;
  for (int i = 0; i <= N; i++) {
    for (int j = p; j >= 0; j--) {
      if (Sum_A[i] + Sum_B[j] <= K) {
        if (ans < i + j) {
          ans = i + j;
        }
        // p = j;
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<long long> A, B;
  long long K;
  long long N, M;
  cin >> N >> M >> K;
  vector<long long> Sum_A(N + 1, 0), Sum_B(M + 1, 0);
  int tmp;
  Sum_A.push_back(0);
  for (int i = 1; i <= N; i++) {
    cin >> tmp;
    A.push_back(tmp);
    Sum_A[i] = Sum_A[i - 1] + tmp;
  }
  Sum_B.push_back(0);
  for (int i = 1; i <= M; i++) {
    cin >> tmp;
    B.push_back(tmp);
    Sum_B[i] = Sum_B[i - 1] + tmp;
  }
  int ans = 0;
  long long p = M;
  for (int i = 0; i <= N; i++) {
    for (int j = p; j >= 0; j--) {
      if (Sum_A[i] + Sum_B[j] <= K) {
        if (ans < i + j) {
          ans = i + j;
        }
        p = j;
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll arr1[n], arr2[m];
    for (int i = 0; i < n; i++)
      cin >> arr1[i];
    for (int i = 0; i < m; i++)
      cin >> arr2[i];
    ll dp[n + 1];
    dp[0] = arr1[0];
    ll max_ = 0, run = 0, i = 0;
    for (i = 0; i < n && run + arr1[i] <= k; i++) {
      run += arr1[i];
    }
    max_ = i;
    run = 0;
    for (int i = 1; i < m; i++)
      dp[i] = dp[i - 1] + arr1[i];
    for (int i = 0; i < m && run <= k; i++) {
      // i books from second pile
      //  costed run
      run += arr2[i];
      if (run > k)
        break;
      ll idx = upper_bound(dp, dp + n, k - run) - dp;
      // cout<<" for run ="<<run<<" "<<i+1<<" + "<<idx<<endl;
      max_ = max(max_, i + 1 + idx);
    }
    cout << max_;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll arr1[n], arr2[m];
    for (int i = 0; i < n; i++)
      cin >> arr1[i];
    for (int i = 0; i < m; i++)
      cin >> arr2[i];
    ll dp[n + 1];
    dp[0] = arr1[0];
    ll max_ = 0, run = 0, i = 0;
    for (i = 0; i < n && run + arr1[i] <= k; i++) {
      run += arr1[i];
    }
    max_ = i;
    run = 0;
    for (int i = 1; i < n; i++)
      dp[i] = dp[i - 1] + arr1[i];
    for (int i = 0; i < m && run <= k; i++) {
      // i books from second pile
      //  costed run
      run += arr2[i];
      if (run > k)
        break;
      ll idx = upper_bound(dp, dp + n, k - run) - dp;
      // cout<<" for run ="<<run<<" "<<i+1<<" + "<<idx<<endl;
      max_ = max(max_, i + 1 + idx);
    }
    cout << max_;
  }
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  int ans = 0, j = m;
  ll tot = 0;
  for (int i = 0; i < m; i++)
    tot += b[i];

  for (int i = 0; i < n + 1; i++) {
    while (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;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  int ans = 0, j = m;
  ll tot = 0;
  for (int i = 0; i < m; i++)
    tot += b[i];

  for (int i = 0; i < n + 1; i++) {
    while (tot > k && j > 0) {
      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;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"

using namespace std;
using ll = long long;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);

  // dp[a][b]
  vector<vector<ll>> dp(n, vector<ll>(m, 0));

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

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

  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (k - a[i] < 0)
      continue;
    auto b_iter = upper_bound(b.begin(), b.end(), k - a[i]);
    int b_ind = distance(b.begin(), b_iter);
    // cout << i << " " << b_ind << endl;
    ans = max(ans, i + b_ind - 1);
  }
  cout << ans;
}

int main() {
  fastio;
  solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"

using namespace std;
using ll = long long;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve() {
  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 i = 1; i <= m; ++i) {
    cin >> b[i];
  }

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

  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (k - a[i] < 0)
      continue;
    auto b_iter = upper_bound(b.begin(), b.end(), k - a[i]);
    int b_ind = distance(b.begin(), b_iter);
    // cout << i << " " << b_ind << endl;
    ans = max(ans, i + b_ind - 1);
  }
  cout << ans;
}

int main() {
  fastio;
  solve();

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ll long long
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n];
  ll B[n];
  ll preA[n + 1];
  ll preB[m + 1];
  preA[0] = 0;
  preB[0] = 0;
  for (ll i = 0; i < n; ++i) {
    cin >> A[i];
    preA[i + 1] = preA[i] + A[i];
  }
  for (ll i = 0; i < m; ++i) {
    cin >> B[i];
    preB[i + 1] = preB[i] + B[i];
  }
  ll max = 0;
  ll index = m;
  for (ll i = 0; i <= n; ++i) {
    ll totalLeft = k - preA[i];
    if (totalLeft < 0) {
      break;
    }
    while (preB[index] > totalLeft) {
      index--;
    }
    if (index + i > max) {
      max = index + i;
    }
  }
  cout << max;
}


Fixed submission:
#include <bits/stdc++.h>

#define ll long long
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n];
  ll B[m];
  ll preA[n + 1];
  ll preB[m + 1];
  preA[0] = 0;
  preB[0] = 0;
  for (ll i = 0; i < n; ++i) {
    cin >> A[i];
    preA[i + 1] = preA[i] + A[i];
  }
  for (ll i = 0; i < m; ++i) {
    cin >> B[i];
    preB[i + 1] = preB[i] + B[i];
  }
  ll max = 0;
  ll index = m;
  for (ll i = 0; i <= n; ++i) {
    ll totalLeft = k - preA[i];
    if (totalLeft < 0) {
      break;
    }
    while (preB[index] > totalLeft) {
      index--;
    }
    if (index + i > max) {
      max = index + i;
    }
  }
  cout << max;
}