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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  /*#ifdef TarekHasan
      freopen("input.txt","r",stdin);
  #endif // TarekHasan*/

  int n, m, k;
  cin >> n >> m >> k;
  vector<long long int> first(n);
  vector<long long int> second(m);

  for (int i = 0; i < n; i++)
    cin >> first[i];

  for (int i = 0; i < m; i++)
    cin >> second[i];

  for (int i = 1; i < n; i++)
    first[i] = first[i] + first[i - 1];
  for (int i = 1; i < m; i++)
    second[i] = second[i] + second[i - 1];

  /*for(long long int x : first)
      cout << x << " ";
  cout << endl;

  for(long long int x : second)
      cout << x << " ";
  cout << endl;*/

  int answer = 0;

  for (int i = 0; i < n; i++) {
    if (first[i] > k)
      break;

    int t_a = i + 1;

    int t_k = k - first[i];

    int index = lower_bound(second.begin(), second.end(), t_k) - second.begin();

    if (index == m || second[index] > t_k)
      index -= 1;
    t_a += (index + 1);
    answer = max(answer, t_a);
  }

  for (int i = 0; i < m; i++) {
    if (second[i] > k)
      break;

    int t_a = i + 1;

    int t_k = k - second[i];

    int index = lower_bound(first.begin(), first.end(), t_k) - first.begin();

    if (index == m || second[index] > t_k)
      index -= 1;
    t_a += (index + 1);
    answer = max(answer, t_a);
  }

  cout << answer << endl;

#ifdef TarekHasan
  main();
#endif // TarekHasan

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  /*#ifdef TarekHasan
      freopen("input.txt","r",stdin);
  #endif // TarekHasan*/

  int n, m, k;
  cin >> n >> m >> k;
  vector<long long int> first(n);
  vector<long long int> second(m);

  for (int i = 0; i < n; i++)
    cin >> first[i];

  for (int i = 0; i < m; i++)
    cin >> second[i];

  for (int i = 1; i < n; i++)
    first[i] = first[i] + first[i - 1];
  for (int i = 1; i < m; i++)
    second[i] = second[i] + second[i - 1];

  /*for(long long int x : first)
      cout << x << " ";
  cout << endl;

  for(long long int x : second)
      cout << x << " ";
  cout << endl;*/

  int answer = 0;

  for (int i = 0; i < n; i++) {
    if (first[i] > k)
      break;

    int t_a = i + 1;

    int t_k = k - first[i];

    int index = lower_bound(second.begin(), second.end(), t_k) - second.begin();

    if (index == m || second[index] > t_k)
      index -= 1;
    t_a += (index + 1);
    answer = max(answer, t_a);
  }

  for (int i = 0; i < m; i++) {
    if (second[i] > k)
      break;

    int t_a = i + 1;

    int t_k = k - second[i];

    int index = lower_bound(first.begin(), first.end(), t_k) - first.begin();

    if (index == n || first[index] > t_k)
      index -= 1;
    t_a += (index + 1);
    answer = max(answer, t_a);
  }

  cout << answer << endl;

#ifdef TarekHasan
  main();
#endif // TarekHasan

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define ll int64_t
#define ull uint64_t
using namespace std;

ull a[200001], b[200001];

int main() {
  IOS;
  ull n, m, k, i = 0, j = 0, time = 0, count = 0;
  ;
  cin >> n >> m >> k;
  for (ull x = 0; x < n; x += 1) {
    cin >> a[x];
  }
  for (ull x = 0; x < m; x += 1) {
    cin >> b[x];
  }
  for (ull x = 0; x < n + m; x += 1) {
    ull temp = min(a[i], b[j]);
    if (temp == a[i])
      i += 1;
    else
      j += 1;
    if (time + temp < k) {
      time += temp;
      count += 1;
    } else {
      break;
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define ll int64_t
#define ull uint64_t
using namespace std;

ull a[200001], b[200001];

int main() {
  IOS;
  ull n, m, k, i = 0, j = 0, time = 0, count = 0;
  ;
  cin >> n >> m >> k;
  for (ull x = 0; x < n; x += 1) {
    cin >> a[x];
  }
  for (ull x = 0; x < m; x += 1) {
    cin >> b[x];
  }
  while (i < n && time + a[i] <= k) {
    time += a[i++];
  }
  count = i;
  while (j < m && i >= 0) {
    time += b[j++];
    while (time > k && i > 0) {
      i--;
      time -= a[i];
    }
    if (time <= k && i + j > count) {
      count = i + j;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

typedef long long ll;
typedef long double ld;

using namespace std;

ll n, m, k, arr1[N], arr2[N];

int main() {
  inlld3(n, m, k);
  for (ll a = 1; a <= n; a++) {
    inlld(arr1[a]);
    arr1[a] += arr1[a - 1];
  }
  for (ll a = 1; a <= m; a++) {
    inlld(arr2[a]);
    arr2[a] += arr2[a - 1];
  }
  ll ptr2 = m;
  ll ans = 0;
  while (arr2[ptr2] > k) {
    ptr2--;
  }
  ans = ptr2;
  for (ll ptr1 = 1; ptr1 <= n; ptr1++) {
    if (arr1[ptr1] > k)
      break;
    while (ptr2 > 0 && arr1[ptr1] + arr2[ptr2] > k) {
      ptr2--;
    }
    ans = max(ans, ptr1 + ptr2);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

typedef long long ll;
typedef long double ld;

using namespace std;

ll n, m, k, arr1[N], arr2[N];

int main() {
  inlld3(n, m, k);
  for (ll a = 1; a <= n; a++) {
    inlld(arr1[a]);
    arr1[a] += arr1[a - 1];
  }
  for (ll a = 1; a <= m; a++) {
    inlld(arr2[a]);
    arr2[a] += arr2[a - 1];
  }
  ll ptr2 = m;
  ll ans = 0;
  while (arr2[ptr2] > k) {
    ptr2--;
  }
  ans = ptr2;
  for (ll ptr1 = 1; ptr1 <= n; ptr1++) {
    if (arr1[ptr1] > k)
      break;
    while (ptr2 > 0 && arr1[ptr1] + arr2[ptr2] > k) {
      ptr2--;
    }
    ans = max(ans, ptr1 + ptr2);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
namespace IO {
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }

template <typename T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void writesp(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\n');
}
} // namespace IO
using namespace IO;
const int N = 2e5 + 5;
int n, m, k, a[N], b[N];
ll suma[N], sumb[N];
int main() {
  read(n);
  read(m);
  read(k);
  for (int i = 1; i <= n; i++)
    read(a[i]);

  for (int i = 1; i <= m; i++)
    read(b[i]);

  for (int i = 1; i <= n; i++)
    suma[i] = suma[i - 1] + a[i];
  for (int i = 1; i <= m; i++)
    sumb[i] = sumb[i - 1] + b[i];

  int ans = 0;
  int npos = m;
  for (int i = 1; i <= n; i++) {
    while (suma[i] + sumb[npos] > k)
      npos--;
    if (suma[i] + sumb[npos] <= k)
      chkmax(ans, i + npos);
  }

  writeln(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
namespace IO {
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }

template <typename T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void writesp(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\n');
}
} // namespace IO
using namespace IO;
const int N = 2e5 + 5;
int n, m, k, a[N], b[N];
ll suma[N], sumb[N];
int main() {
  read(n);
  read(m);
  read(k);
  for (int i = 1; i <= n; i++)
    read(a[i]);

  for (int i = 1; i <= m; i++)
    read(b[i]);

  for (int i = 1; i <= n; i++)
    suma[i] = suma[i - 1] + a[i];
  for (int i = 1; i <= m; i++)
    sumb[i] = sumb[i - 1] + b[i];

  int ans = 0;
  int npos = m;
  for (int i = 0; i <= n; i++) {
    while (npos >= 0 && suma[i] + sumb[npos] > k)
      npos--;
    if (suma[i] + sumb[npos] <= k)
      chkmax(ans, i + npos);
  }

  writeln(ans);
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  rep1(i, n) {
    if (i == 1)
      cin >> a[i];
    else {
      cin >> a[i];
      a[i] += a[i - 1];
    }
  }
  vector<ll> b(m + 1);
  rep1(i, m) {
    if (i == 1)
      cin >> b[i];
    else {
      cin >> b[i];
      b[i] += b[i - 1];
    }
  }
  ll ans = 0;
  rep(i, n + 1) { // aのサーチ
    rep(j, m + 1) {
      // cout << i+j << "," << a[i]+b[j] << endl;
      if (a[i] + b[j] <= k)
        ans = max(ans, i + j);
    }
  }
  cout << ans << endl;
}

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  rep1(i, n) {
    if (i == 1)
      cin >> a[i];
    else {
      cin >> a[i];
      a[i] += a[i - 1];
    }
  }
  vector<ll> b(m + 1);
  rep1(i, m) {
    if (i == 1)
      cin >> b[i];
    else {
      cin >> b[i];
      b[i] += b[i - 1];
    }
  }
  ll ans = 0;
  rep(i, n + 1) { // aのサーチ
    ll target = k - a[i];
    // cout << i << " " << target << " ";
    // for(auto p : b) cout << p << ",";
    ll b_num = upper_bound(all(b), target) - begin(b) - 1;
    ll tmp = i + b_num;
    ll val = a[i] + *(upper_bound(all(b), target) - 1);
    // cout << " " << tmp << " " << val;
    // cout << endl;
    if (val <= k)
      ans = max(ans, tmp);
  }
  cout << ans << endl;
}
Problem p02623 (C++)

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define inf 1e18

using namespace std;

int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}

int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif

  IOS;
  int n, m, k, i;
  cin >> n >> m >> k;
  vi a(n + 1), b(n + 1);
  vi suma(n + 1), sumb(m + 1);
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    suma[i] = suma[i - 1] + a[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> b[i];
    sumb[i] = sumb[i - 1] + b[i];
  }
  int ans = 0;
  for (i = 0; i <= n; i++) {
    if (suma[i] > k)
      break;
    else {
      int x = k - suma[i];
      auto it = upper_bound(sumb.begin(), sumb.end(), x);
      int pos = it - sumb.begin() - 1;
      if (pos < 0)
        pos = 0;
      ans = max(ans, i + pos);
    }
  }
  cout << ans;
}


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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define inf 1e18

using namespace std;

int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}

int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif

  IOS;
  int n, m, k, i;
  cin >> n >> m >> k;
  vi a(n + 1), b(m + 1);
  vi suma(n + 1), sumb(m + 1);
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    suma[i] = suma[i - 1] + a[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> b[i];
    sumb[i] = sumb[i - 1] + b[i];
  }
  int ans = 0;
  for (i = 0; i <= n; i++) {
    if (suma[i] > k)
      break;
    else {
      int x = k - suma[i];
      auto it = upper_bound(sumb.begin(), sumb.end(), x);
      int pos = it - sumb.begin() - 1;
      if (pos < 0)
        pos = 0;
      ans = max(ans, i + pos);
    }
  }
  cout << ans;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  long long int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<long long int> a(n); // size
  std::vector<long long int> b(m); // size

  for (int i = 0; i < n; i++)
    std::cin >> a[i];

  for (int i = 0; i < m; i++)
    std::cin >> b[i];

  // by using perfix sum
  std::vector<long long int> aSUM(n + 1); // size
  std::vector<long long int> bSUM(n + 1); // size
  int count = 0;
  for (int i = 0; i < n; i++)
    aSUM[i + 1] = aSUM[i] + a[i];

  for (int i = 0; i < m; i++)
    bSUM[i + 1] = bSUM[i] + b[i];

  // 0 60 and so on
  for (int i = 0; i <= n; i++) {
    long long int other = k - aSUM[i];
    if (other < 0)
      continue;
    // here get the position of other from bSum
    int l = lower_bound(bSUM.begin(), bSUM.end(), other) - bSUM.begin();
    if (l > m || bSUM[l] > other)
      --l;
    count = std::max(count, i + l);
  }
  std::cout << count << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  long long int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<long long int> a(n); // size
  std::vector<long long int> b(m); // size

  for (int i = 0; i < n; i++)
    std::cin >> a[i];

  for (int i = 0; i < m; i++)
    std::cin >> b[i];

  // by using perfix sum
  std::vector<long long int> aSUM(n + 1); // size
  std::vector<long long int> bSUM(m + 1); // size
  int count = 0;
  for (int i = 0; i < n; i++)
    aSUM[i + 1] = aSUM[i] + a[i];

  for (int i = 0; i < m; i++)
    bSUM[i + 1] = bSUM[i] + b[i];

  // 0 60 and so on
  for (int i = 0; i <= n; i++) {
    long long int other = k - aSUM[i];
    if (other < 0)
      continue;
    // here get the position of other from bSum
    int l = lower_bound(bSUM.begin(), bSUM.end(), other) - bSUM.begin();
    if (l > m || bSUM[l] > other)
      --l;
    count = std::max(count, i + l);
  }
  std::cout << count << '\n';
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main() {
  ll n, m, a, b, ans = 0, k;
  cin >> n >> m >> k;
  vector<ll> suma(n + 10), sumb(n + 10);
  for (int i = 0; i < n; i++) {
    cin >> a;
    suma[i + 1] = suma[i] + a;
  }
  for (int i = 0; i < m; i++) {
    cin >> b;
    sumb[i + 1] = sumb[i] + b;
  }
  for (int i = 0; i <= n; i++) {
    while (m >= 0 && suma[i] + sumb[m] > k) {
      m--;
    }
    if (m >= 0) {
      ans = max(ans, i + m);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main() {
  ll n, m, a, b, ans = 0, k;
  cin >> n >> m >> k;
  vector<ll> suma(n + 10), sumb(m + 10);
  for (int i = 0; i < n; i++) {
    cin >> a;
    suma[i + 1] = suma[i] + a;
  }
  for (int i = 0; i < m; i++) {
    cin >> b;
    sumb[i + 1] = sumb[i] + b;
  }
  for (int i = 0; i <= n; i++) {
    while (m >= 0 && suma[i] + sumb[m] > k) {
      m--;
    }
    if (m >= 0) {
      ans = max(ans, i + m);
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int N, M, K;
  cin >> N >> M >> K;
  int m = 1;
  while (m < M)
    m *= 2;
  vector<int> A(N + 1), B(M + 1);
  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 l = 0;
  for (int i = N; i >= 0; i--) {
    int j = m / 4, k = M / 2;
    while (j > 0) {
      if (A[i] + B[k] < K)
        k += j;
      else
        k -= j;
      j = j / 2;
    }
    if (0 <= k + 2 && k + 2 <= M && A[i] + B[k + 2] <= K)
      l = max(l, i + k + 1);
    else if (0 <= k + 1 && k + 1 <= M && A[i] + B[k + 1] <= K)
      l = max(l, i + k + 1);
    else if (0 <= k && k <= M && A[i] + B[k] <= K)
      l = max(l, i + k);
    else if (0 <= k - 1 && k - 1 <= M && A[i] + B[k - 1] <= K)
      l = max(l, i + k - 1);
    else if (0 <= k - 2 && k - 2 <= M && A[i] + B[k - 2] <= K)
      l = max(l, i + k - 1);
  }
  cout << l << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int N, M, K;
  cin >> N >> M >> K;
  int m = 1;
  while (m < M)
    m *= 2;
  vector<int> A(N + 1), B(M + 1);
  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 l = 0;
  for (int i = N; i >= 0; i--) {
    int j = upper_bound(B.begin(), B.end(), K - A[i]) - B.begin();
    if (j == M + 1)
      l = max(l, i + M);
    else if (j != 0 || A[i] < K)
      l = max(l, i + j - 1);
  }
  cout << l << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll sum = 0;
  int cnt = 0, itr1 = 0;
  rep(i, n) {
    if (sum + a[i] <= k)
      sum += a[i], cnt++, itr1 = i;
    else
      break;
  }
  int ans = cnt;
  rep(i, m) {
    if (sum + b[i] <= k)
      sum += b[i], cnt++;
    else {
      while (sum > 0) {
        sum -= a[itr1--];
        cnt--;
        if (sum + b[i] <= k)
          break;
      }
      if (sum + b[i] <= k)
        sum += b[i], cnt++;
      else
        break;
    }
    chmax(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll sum = 0;
  int cnt = 0, itr1 = 0;
  rep(i, n) {
    if (sum + a[i] <= k)
      sum += a[i], cnt++, itr1 = i;
    else
      break;
  }
  int ans = cnt;
  rep(i, m) {
    if (sum + b[i] <= k)
      sum += b[i], cnt++;
    else {
      while (sum > 0 && itr1 >= 0) {
        sum -= a[itr1--];
        cnt--;
        if (sum + b[i] <= k)
          break;
      }
      if (sum + b[i] <= k)
        sum += b[i], cnt++;
      else
        break;
    }
    chmax(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];

  vector<ll> va(n + 1, 0), vb(n + 1, 0);
  rep(i, 0, n) va[i + 1] = va[i] + a[i];
  rep(i, 0, m) vb[i + 1] = vb[i] + b[i];

  int ans = 0, j = m;
  rep(i, 0, n + 1) {
    if (va[i] > k)
      break;
    while (vb[j] > k - va[i])
      j -= 1;
    chmax(ans, i + j);
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];

  vector<ll> va(n + 1, 0), vb(m + 1, 0);
  rep(i, 0, n) va[i + 1] = va[i] + a[i];
  rep(i, 0, m) vb[i + 1] = vb[i] + b[i];

  int ans = 0, j = m;
  rep(i, 0, n + 1) {
    if (va[i] > k)
      break;
    while (vb[j] > k - va[i])
      j -= 1;
    chmax(ans, i + j);
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll mod = 1000000007;

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


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll mod = 1000000007;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, mod), b(m + 1, mod), A(n + 1), B(m + 1);
  rep(i, n) cin >> a[i];
  rep(j, m) cin >> b[j];
  rep(i, n) A[i + 1] = A[i] + a[i];
  rep(j, m) B[j + 1] = B[j] + b[j];
  ll ans = 0, j = m;
  rep(i, n + 1) {
    if (A[i] > k)
      break;
    while (B[j] > k - A[i]) {
      if (j == 0)
        break;
      j -= 1;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n, m;
  ll k;
  vector<ll> a, b;
  cin >> n >> m >> k;
  a.resize(n);
  b.resize(m);
  vector<ll> sum_a(n + 1, 0);
  vector<ll> sum_b(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum_a[i + 1] = sum_a[i] + a[i];
  }
  for (int j = 0; j < m; j++) {
    cin >> b[j];
    sum_b[j + 1] = sum_b[j] + b[j];
  }
  ll ans = 0;
  ll j = m;
  for (ll i = 0; i <= n; i++) {
    if (sum_a[i] > k)
      break;
    ll temp_max = k - sum_a[i];
    while (sum_b[j] > temp_max)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n, m;
  ll k;
  vector<ll> a, b;
  cin >> n >> m >> k;
  a.resize(200006);
  b.resize(200006);
  vector<ll> sum_a(200006, 0);
  vector<ll> sum_b(200006, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum_a[i + 1] = sum_a[i] + a[i];
  }
  for (int j = 0; j < m; j++) {
    cin >> b[j];
    sum_b[j + 1] = sum_b[j] + b[j];
  }
  ll ans = 0;
  ll j = m;
  for (ll i = 0; i <= n; i++) {
    if (sum_a[i] > k)
      break;
    ll temp_max = k - sum_a[i];
    while (sum_b[j] > temp_max)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << "\n";
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
/*note:- in first sight it seems that it will be solved with greddy techniqe
(but that is wrong as it will give wrong ans).*/
/*4 3 270
100 10 10 10
90 90 90
using greedy approach we will be able to read only three books in the above test
case(and it will consume entire 270 minutes) but actually we can read 4 books
(in just 130 minutes)
*/
/*
this approch uses calculation of prefix sum of both the arrays
refer editorial(and YT_vid) for more details
*/
#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);
  vector<ll> b(n);
  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 j = 0; j < m; j++)
    sumB[j + 1] = sumB[j] + b[j];
  ll total = 0;
  for (ll i = 0; i <= n; i++) {
    ll other = k - sumA[i];
    if (other < 0)
      continue;
    ll l = lower_bound(sumB.begin(), sumB.end(), other) - sumB.begin();
    if (l > m || sumB[l] > other)
      l--;
    total = max(total, i + l);
  }
  cout << total;
  return 0;
}

Fixed submission:
/*note:- in first sight it seems that it will be solved with greddy techniqe
(but that is wrong as it will give wrong ans).*/
/*4 3 270
100 10 10 10
90 90 90
using greedy approach we will be able to read only three books in the above test
case(and it will consume entire 270 minutes) but actually we can read 4 books
(in just 130 minutes)
*/
/*
this approch uses calculation of prefix sum of both the arrays
refer editorial(and YT_vid) for more details
*/
#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);
  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 j = 0; j < m; j++)
    sumB[j + 1] = sumB[j] + b[j];
  ll total = 0;
  for (ll i = 0; i <= n; i++) {
    ll other = k - sumA[i];
    if (other < 0)
      continue;
    ll l = lower_bound(sumB.begin(), sumB.end(), other) - sumB.begin();
    if (l > m || sumB[l] > other)
      l--;
    total = max(total, i + l);
  }
  cout << total;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long n, m, k;
vector<long> a, b;
vector<long long> sumA, sumB;
long getB(long inA) {
  long long passedTime = 0;
  if (inA != 0)
    passedTime = sumA[inA - 1];
  long long remainTime = k - passedTime;
  long ret = 0;
  for (long i = 0; i < m; i++) {
    if (sumB[i] <= remainTime) {
      ret++;
    } else {
      break;
    }
  }
  return ret;
}
bool isValidA(long inA) {
  if (inA == 0)
    return true;
  if (inA > n)
    return false;
  if (sumA[inA - 1] > k)
    return false; // thisPath"inA">0
  return true;
}
int main() {
  cin >> n >> m >> k;

  a = vector<long>(n);
  sumA = vector<long long>(n, 0);
  b = vector<long>(m);
  sumB = vector<long long>(m, 0);

  for (long i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      sumA[i] = a[i];
    } else {
      sumA[i] = sumA[i - 1] + a[i];
    }
  }
  for (long i = 0; i < m; i++) {
    cin >> b[i];
    if (i == 0) {
      sumB[i] = b[i];
    } else {
      sumB[i] = sumB[i - 1] + b[i];
    }
  }
  long ans = 0;
  for (long i = 0; i <= n; i++) {
    if (isValidA(i)) {
      long bnum = getB(i);
      ans = max(ans, bnum + i);
    } else {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long n, m, k;
vector<long> a, b;
vector<long long> sumA, sumB;
long getB(long inA) {
  long long passedTime = 0;
  if (inA != 0)
    passedTime = sumA[inA - 1];
  long long remainTime = k - passedTime;
  long ret = 0;
  // for(long i = 0; i < m; i++){
  //     if(sumB[i]<=remainTime){
  //         ret++;
  //     }else{
  //         break;
  //     }
  // }
  decltype(sumB)::iterator it =
      upper_bound(sumB.begin(), sumB.end(), remainTime);
  long index = it - sumB.begin();
  ret = index;
  return ret;
}
bool isValidA(long inA) {
  if (inA == 0)
    return true;
  if (inA > n)
    return false;
  if (sumA[inA - 1] > k)
    return false; // thisPath"inA">0
  return true;
}
int main() {
  cin >> n >> m >> k;

  a = vector<long>(n);
  sumA = vector<long long>(n, 0);
  b = vector<long>(m);
  sumB = vector<long long>(m, 0);

  for (long i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      sumA[i] = a[i];
    } else {
      sumA[i] = sumA[i - 1] + a[i];
    }
  }
  for (long i = 0; i < m; i++) {
    cin >> b[i];
    if (i == 0) {
      sumB[i] = b[i];
    } else {
      sumB[i] = sumB[i - 1] + b[i];
    }
  }
  long ans = 0;
  for (long i = 0; i <= n; i++) {
    if (isValidA(i)) {
      long bnum = getB(i);
      ans = max(ans, bnum + i);
    } else {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
/*
 *  Author: Arpit Bhayani
 *  https://arpitbhayani.me
 */
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long

#define MIN(a, b) a < b ? a : b
#define MAX(a, b) a > b ? a : b

using namespace std;

int readline(char *str) {
  int i = 0;
  char ch;
  while ((ch = getchar()) != '\n') {
    str[i++] = ch;
  }
  str[i] = '\0';
  return i;
}

int arr1[100001];
int arr2[100001];

int foo(int n, int m, ll int x) {
  ll int sum = 0;
  int i = 0, j = 0;
  int count = 0;

  while (i < n) {
    sum += (ll int)arr1[i];
    i++;
    if (sum > x) {
      i--;
      sum -= (ll int)arr1[i];
      break;
    }
  }
  count = i;

  while (j < m) {
    sum += (ll int)arr2[j];
    j++;
    while (sum > x && i > 0) {
      i--;
      sum -= (ll int)arr1[i];
    }
    count = (sum <= x) ? MAX((count), (i + j)) : count;
  }

  return count;
}

int main(int argc, char *argv[]) {

  ll int x;
  int n, m;
  scanf("%d%d%lld", &n, &m, &x);

  for (int i = 0; i < n; i++) {
    scanf("%d", &arr1[i]);
  }

  for (int j = 0; j < m; j++) {
    scanf("%d", &arr2[j]);
  }

  printf("%d\n", foo(n, m, x));
}

Fixed submission:
/*
 *  Author: Arpit Bhayani
 *  https://arpitbhayani.me
 */
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long

#define MIN(a, b) a < b ? a : b
#define MAX(a, b) a > b ? a : b

using namespace std;

int readline(char *str) {
  int i = 0;
  char ch;
  while ((ch = getchar()) != '\n') {
    str[i++] = ch;
  }
  str[i] = '\0';
  return i;
}

int arr1[200001];
int arr2[200001];

int foo(int n, int m, ll int x) {
  ll int sum = 0;
  int i = 0, j = 0;
  int count = 0;

  while (i < n) {
    sum += (ll int)arr1[i];
    i++;
    if (sum > x) {
      i--;
      sum -= (ll int)arr1[i];
      break;
    }
  }
  count = i;

  while (j < m) {
    sum += (ll int)arr2[j];
    j++;
    while (sum > x && i > 0) {
      i--;
      sum -= (ll int)arr1[i];
    }
    count = (sum <= x) ? MAX((count), (i + j)) : count;
  }

  return count;
}

int main(int argc, char *argv[]) {

  ll int x;
  int n, m;
  scanf("%d%d%lld", &n, &m, &x);

  for (int i = 0; i < n; i++) {
    scanf("%d", &arr1[i]);
  }

  for (int j = 0; j < m; j++) {
    scanf("%d", &arr2[j]);
  }

  printf("%d\n", foo(n, m, x));
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fast                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define int long long
#define all(c) (c).begin(), (c).end()
#define endl '\n'
using namespace std;

//                                          mdsaqib
signed main() {
  fast;
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> A(n, 0), B(m, 0);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> B[i];
  }
  vector<int> SA(n, 0), SB(m, 0);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += A[i];
    SA[i] = sum;
  }
  sum = 0;
  for (int i = 0; i < n; i++) {
    sum += B[i];
    SB[i] = sum;
  }
  int mx = 0;
  int ii = 0, jj = 0;
  for (int i = 0; i < n; i++) {
    if (SA[i] > k) {
      break;
    }
    ii = i + 1;
  }
  mx = ii;
  if (ii > 0) {
    for (int i = ii - 1; i >= 0; i--) {
      for (int j = jj; j < m; j++) {
        if (SA[i] + SB[j] > k) {
          break;
        }
        jj = j + 1;
      }
      mx = max(mx, i + jj + 1);
    }
  }
  jj = 0;
  for (int i = 0; i < m; i++) {
    if (SB[i] > k) {
      break;
    }
    jj = i + 1;
  }
  mx = max(mx, jj);
  cout << mx;
}

Fixed submission:
#include <bits/stdc++.h>
#define fast                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define int long long
#define all(c) (c).begin(), (c).end()
#define endl '\n'
using namespace std;

//                                          mdsaqib
signed main() {
  fast;
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> A(n, 0), B(m, 0);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> B[i];
  }
  vector<int> SA(n, 0), SB(m, 0);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += A[i];
    SA[i] = sum;
  }
  sum = 0;
  for (int i = 0; i < m; i++) {
    sum += B[i];
    SB[i] = sum;
  }
  int mx = 0;
  int ii = 0, jj = 0;
  for (int i = 0; i < n; i++) {
    if (SA[i] > k) {
      break;
    }
    ii = i + 1;
  }
  mx = ii;
  if (ii > 0) {
    for (int i = ii - 1; i >= 0; i--) {
      for (int j = jj; j < m; j++) {
        if (SA[i] + SB[j] > k) {
          break;
        }
        jj = j + 1;
      }
      mx = max(mx, i + jj + 1);
    }
  }
  jj = 0;
  for (int i = 0; i < m; i++) {
    if (SB[i] > k) {
      break;
    }
    jj = i + 1;
  }
  mx = max(mx, jj);
  cout << mx;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int i, ans, ia, ib = 0;
  ll n, m, k, A, B;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  for (i = 1; i < n + 1; ++i) {
    cin >> A;
    a.at(i) = a.at(i - 1) + A;
  }
  for (i = 1; i < m + 1; ++i) {
    cin >> B;
    b.at(i) = b.at(i - 1) + B;
  }
  for (i = n; i > 0; --i) {
    if (a.at(i) <= k) {
      ans = i;
      ia = i;
      break;
    }
  }
  while (ia >= 0) {
    while (a.at(ia) + b.at(ib) <= k) {
      ++ib;
      if (ib > m)
        break;
    }
    --ib;
    ans = max(ans, ia + ib);
    --ia;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int i, ans, ia, ib = 0;
  ll n, m, k, A, B;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  for (i = 1; i < n + 1; ++i) {
    cin >> A;
    a.at(i) = a.at(i - 1) + A;
  }
  for (i = 1; i < m + 1; ++i) {
    cin >> B;
    b.at(i) = b.at(i - 1) + B;
  }
  for (i = n; i >= 0; --i) {
    if (a.at(i) <= k) {
      ans = i;
      ia = i;
      break;
    }
  }
  while (ia >= 0) {
    while (a.at(ia) + b.at(ib) <= k) {
      ++ib;
      if (ib > m)
        break;
    }
    --ib;
    ans = max(ans, ia + ib);
    --ia;
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vec a(n + 1), b(m + 1);
  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];
  }
  ll ok = 0, ng = n + m + 1;
  while (ok + 1 < ng) {
    ll mid = (ng + ok) >> 1;
    ll v = inf;
    for (ll i = max(0LL, mid - m); i <= n; i++) {
      v = min(v, a[i] + b[mid - i]);
    }
    if (v <= k)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vec a(n + 1), b(m + 1);
  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];
  }
  ll ok = 0, ng = n + m + 1;
  while (ok + 1 < ng) {
    ll mid = (ng + ok) >> 1;
    ll v = inf;
    for (ll i = max(0LL, mid - m); i <= min(n, mid); i++) {
      v = min(v, a[i] + b[mid - i]);
    }
    if (v <= k)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
}
Problem p02623 (C++)

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

#include <algorithm>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

int main() {
  int n, m, k, a, cont1 = 0, cont2 = 0, r, i;
  cin >> n >> m >> k;
  int s1, s2;
  vector<long long> x(n);
  vector<long long> y(m);

  for (i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> y[i];
  }
  s1 = s2 = 0;
  for (i = 1; i <= n; i++) {
    if (s1 + x[i] > k)
      break;
    cont1++;
    s1 += x[i];
  }
  for (i = 1; i <= m; i++) {
    if (s1 + s2 + y[i] > k)
      break;
    cont2++;
    s2 += y[i];
  }

  r = cont1 + cont2;

  while (cont1 > 0) {
    s1 -= x[cont1--];
    while (s1 + s2 + y[cont2 + 1] <= k && cont2 < m) {
      cont2++;
      s2 += y[cont2];
    }
    r = max(r, cont1 + cont2);
  }

  cout << r << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>

#include <algorithm>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

int main() {
  int n, m, k, a, cont1 = 0, cont2 = 0, r, i;
  cin >> n >> m >> k;
  int s1, s2;
  vector<long long> x(200005);
  vector<long long> y(200005);

  for (i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> y[i];
  }
  s1 = s2 = 0;
  for (i = 1; i <= n; i++) {
    if (s1 + x[i] > k)
      break;
    cont1++;
    s1 += x[i];
  }
  for (i = 1; i <= m; i++) {
    if (s1 + s2 + y[i] > k)
      break;
    cont2++;
    s2 += y[i];
  }

  r = cont1 + cont2;

  while (cont1 > 0) {
    s1 -= x[cont1--];
    while (s1 + s2 + y[cont2 + 1] <= k && cont2 < m) {
      cont2++;
      s2 += y[cont2];
    }
    r = max(r, cont1 + cont2);
  }

  cout << r << endl;

  return 0;
}
Problem p02623 (C++)

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

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N), B(M);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  for (int i = 0; i < M; ++i)
    cin >> B[i];
  vector<ll> sumA(N + 1, 0), sumB(M + 1, 0);
  for (int i = 0; i < N; ++i)
    sumA[i + 1] = sumA[i] + A[i];
  for (int i = 0; i < M; ++i)
    sumB[i + 1] = sumB[i] + A[i];
  int pa = upper_bound(sumA.begin(), sumA.end(), K) - sumA.begin() - 1;
  int ans = pa;
  for (int i = pa; i >= 0; --i) {
    ll rest_t = K - sumA[i];
    int pb = upper_bound(sumB.begin(), sumB.end(), rest_t) - sumB.begin() - 1;
    pb = max(pb, 0);
    ans = max(ans, i + pb);
  }
  cout << ans << endl;
}

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

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N), B(M);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  for (int i = 0; i < M; ++i)
    cin >> B[i];
  ll sumA = 0;
  int ia = 0;
  ll sumB = 0;
  int ib = 0;
  while (ia < N && sumA + A[ia] <= K)
    sumA += A[ia++];
  int ans = ia;
  for (int i = ia; i >= 0; --i) {
    ll rt = K - sumA;
    while (ib < M && sumB + B[ib] <= rt)
      sumB += B[ib++];
    ans = max(ans, i + ib);
    if (i > 0)
      sumA -= A[i - 1];
  }
  cout << ans << endl;
}
Problem p02623 (C++)

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

using namespace std;

typedef long long ll;

const int N = 2e5 + 10;

int n, m, k;

int main() {
  scanf("%d%d%d", &n, &m, &k);
  vector<ll> a(n + 1, 0);
  vector<ll> b(n + 1, 0);
  for (int i = 1, x; i <= n; ++i)
    scanf("%d", &x), a[i] = a[i - 1] + x;
  for (int i = 1, x; i <= m; ++i)
    scanf("%d", &x), b[i] = b[i - 1] + x;

  int j = m;
  ll 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 + 0LL);
  }

  printf("%lld\n", ans);

  return 0;
}

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

using namespace std;

typedef long long ll;

const int N = 2e5 + 10;

int n, m, k;

int main() {
  scanf("%d%d%d", &n, &m, &k);
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);
  for (int i = 1, x; i <= n; ++i)
    scanf("%d", &x), a[i] = a[i - 1] + x;
  for (int i = 1, x; i <= m; ++i)
    scanf("%d", &x), b[i] = b[i - 1] + x;

  int j = m;
  ll 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 + 0LL);
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>

using namespace std;
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define INF 1e9
#define llINF 1e18
#define base10_4 10000      // 1e4
#define base10_5 100000     // 1e5
#define base10_6 1000000    // 1e6
#define base10_7 10000000   // 1e7
#define base10_8 100000000  // 1e8
#define base10_9 1000000000 // 1e9

#define MOD 1000000007
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>

// #include <stack>
// #include <queue>

string ans_Yes = "Yes";
string ans_No = "No";
string ans_yes = "yes";
string ans_no = "no";

vll A;
vll B;
vll Ai;
vll Bi;
ll C;
ll N;
ll M;
ll K;

ll ltmp;
string stmp;
double dtmp;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> N;
  cin >> M;
  cin >> K;

  ll sumA = 0;
  A.push_back(0);
  rep(ni, N) {
    cin >> ltmp;
    sumA += ltmp;
    A.push_back(sumA);
  }

  ll sumB = 0;
  B.push_back(0);
  rep(ni, M) {
    cin >> ltmp;
    sumB += ltmp;
    B.push_back(sumB);
  }

  ll bi = B.size() - 1;
  ll cnt = 0;
  rep(ai, A.size()) {
    while (A[ai] + B[bi] > K) {
      bi--;
    }
    if (cnt < ai + bi)
      cnt = ai + bi;
  }

  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>

using namespace std;
#include <vector>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define INF 1e9
#define llINF 1e18
#define base10_4 10000      // 1e4
#define base10_5 100000     // 1e5
#define base10_6 1000000    // 1e6
#define base10_7 10000000   // 1e7
#define base10_8 100000000  // 1e8
#define base10_9 1000000000 // 1e9

#define MOD 1000000007
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>

// #include <stack>
// #include <queue>

string ans_Yes = "Yes";
string ans_No = "No";
string ans_yes = "yes";
string ans_no = "no";

vll A;
vll B;
vll Ai;
vll Bi;
ll C;
ll N;
ll M;
ll K;

ll ltmp;
string stmp;
double dtmp;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> N;
  cin >> M;
  cin >> K;

  ll sumA = 0;
  A.push_back(0);
  rep(ni, N) {
    cin >> ltmp;
    sumA += ltmp;
    A.push_back(sumA);
  }

  ll sumB = 0;
  B.push_back(0);
  rep(ni, M) {
    cin >> ltmp;
    sumB += ltmp;
    B.push_back(sumB);
  }

  ll bi = B.size() - 1;
  ll cnt = 0;
  rep(ai, A.size()) {
    if (A[ai] > K)
      break;
    while (A[ai] + B[bi] > K) {
      bi--;
    }
    if (cnt < ai + bi)
      cnt = ai + bi;
  }

  cout << cnt << endl;
}