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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define size_of_array(array) (sizeof(array) / sizeof(array[0]))
#define DEBUG 0
#define LIMIT (LONG_MAX / 10)
#define POSITIVE_LAST_D (LONG_MAX % 10)
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;

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

  ll ans = 0;
  rep(i, n + 1) {
    ll j = m;
    while (1) {
      if (sa[i] + sb[j] <= k) {
        ans = max(i + j, ans);
        break;
      } else {
        --j;
        --m;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define size_of_array(array) (sizeof(array) / sizeof(array[0]))
#define DEBUG 0
#define LIMIT (LONG_MAX / 10)
#define POSITIVE_LAST_D (LONG_MAX % 10)
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;

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

  ll ans = 0;
  rep(i, n + 1) {
    ll j = m;
    while (j >= 0) {
      if (sa[i] + sb[j] <= k) {
        ans = max(i + j, ans);
        break;
      } else {
        --j;
        --m;
      }
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull B = 100000007;
int mod = 1000000007;

class Solution {
public:
  int c(int n, int m, const vector<int> &t1, const vector<int> &t2, int k) {
    int res = 0;

    vector<ll> s1(n + 1, 0), s2(m + 1, 0);
    for (int i = 1; i < n + 1; ++i) {
      s1[i] = s1[i - 1] + t1[i - 1];
    }
    for (int i = 1; i < m + 1; ++i) {
      s2[i] = s2[i - 1] + t2[i - 1];
    }

    ll l = 0, r = n + m;
    while (l <= r) {
      ll mid = (l + r) / 2;

      ll tres = INT64_MAX;
      for (int k1 = 0; k1 <= mid; ++k1) {
        tres = min(tres, s1[k1] + s2[mid - k1]);
      }
      if (tres <= k) {
        res = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }

    return res;
  }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> t1(n), t2(m);
  for (int i = 0; i < n; ++i)
    cin >> t1[i];
  for (int i = 0; i < m; ++i)
    cin >> t2[i];
  Solution zhy;
  cout << zhy.c(n, m, t1, t2, k) << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull B = 100000007;
int mod = 1000000007;

class Solution {
public:
  int c(int n, int m, const vector<int> &t1, const vector<int> &t2, int k) {
    int res = 0;

    vector<ll> s1(n + 1, 0), s2(m + 1, 0);
    for (int i = 1; i < n + 1; ++i) {
      s1[i] = s1[i - 1] + t1[i - 1];
    }
    for (int i = 1; i < m + 1; ++i) {
      s2[i] = s2[i - 1] + t2[i - 1];
    }

    ll l = 0, r = n + m;
    while (l <= r) {
      ll mid = (l + r) / 2;

      ll tres = INT64_MAX;
      for (int k1 = 0; k1 <= mid; ++k1) {
        if (k1 > n || mid - k1 > m) {
          continue;
        }
        tres = min(tres, s1[k1] + s2[mid - k1]);
      }
      if (tres <= k) {
        res = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }

    return res;
  }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> t1(n), t2(m);
  for (int i = 0; i < n; ++i)
    cin >> t1[i];
  for (int i = 0; i < m; ++i)
    cin >> t2[i];
  Solution zhy;
  cout << zhy.c(n, m, t1, t2, k) << endl;
  return 0;
}
Problem p02623 (C++)

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

typedef long long ll;

using namespace std;

string s, t;
ll a[100500], b[100500], pa[100500], pbb[100500];

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int n, m, x, i;
  ll tests = 1;
  // cin >> tests;
  while (tests--) {
    cin >> n >> m >> x;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      pa[i + 1] = pa[i] + a[i];
    }
    for (i = 0; i < m; i++) {
      cin >> b[i];
      pbb[i + 1] = pbb[i] + b[i];
    }
    int ans = 0;
    for (i = 0; i <= n; i++) {
      if (pa[i] <= x)
        ans = max(
            ans, i + (int)(upper_bound(pbb, pbb + m + 1, x - pa[i]) - pbb - 1));
      else
        break;
    }
    cout << ans << endl;
  }
  return 0;
}


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

typedef long long ll;

using namespace std;

string s, t;
ll a[200005], b[200005], pa[200005], pbb[200005];

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int n, m, x, i;
  ll tests = 1;
  // cin >> tests;
  while (tests--) {
    cin >> n >> m >> x;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      pa[i + 1] = pa[i] + a[i];
    }
    for (i = 0; i < m; i++) {
      cin >> b[i];
      pbb[i + 1] = pbb[i] + b[i];
    }
    int ans = 0;
    for (i = 0; i <= n; i++) {
      if (pa[i] <= x)
        ans = max(
            ans, i + (int)(upper_bound(pbb, pbb + m + 1, x - pa[i]) - pbb - 1));
      else
        break;
    }
    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 rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using p = pair<int, int>;
using ll = long long;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);
  rep(i, n) cin >> a.at(i);
  rep(i, m) cin >> b.at(i);

  ll t = 0;
  rep(i, m) t += b.at(i);

  int j = m;
  int ans = 0;
  rep(i, n + 1) {

    while (j > 0 && t > k) {
      --j;
      t -= b.at(i);
    }

    if (t > k)
      break;
    ans = max(ans, i + j);

    if (i == n)
      break;
    t += a.at(i);
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);
  rep(i, n) cin >> a.at(i);
  rep(i, m) cin >> b.at(i);

  ll t = 0;
  rep(i, m) t += b.at(i);

  int j = m;
  int ans = 0;
  rep(i, n + 1) {

    while (j > 0 && t > k) {
      --j;
      t -= b.at(j);
    }

    if (t > k)
      break;
    ans = max(ans, i + j);

    if (i == n)
      break;
    t += a.at(i);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

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

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

  return 0;
}

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

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

  return 0;
}
Problem p02623 (C++)

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  vector<llint> asum(n + 1);
  vector<llint> bsum(m + 1);
  llint atmp, btmp;
  for (int in = 0; in < n; in++) {
    cin >> atmp;
    asum.at(in + 1) = asum.at(in) + atmp;
  }
  for (int im = 0; im < m; im++) {
    cin >> btmp;
    bsum.at(im + 1) = bsum.at(im) + btmp;
  }

  int ans = 0;
  for (int in = 0; in <= n; in++) {
    for (int im = 0; im <= m; im++) {
      if (asum.at(in) + bsum.at(im) <= k && ans <= in + im)
        ans = in + im;
    }
  }
  cout << ans << endl;
  return 0;
}


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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  vector<llint> asum(n + 1);
  vector<llint> bsum(m + 1);
  llint atmp, btmp;
  for (int in = 0; in < n; in++) {
    cin >> atmp;
    asum.at(in + 1) = asum.at(in) + atmp;
  }
  for (int im = 0; im < m; im++) {
    cin >> btmp;
    bsum.at(im + 1) = bsum.at(im) + btmp;
  }

  llint ans = 0, anstmp, bmax = m;
  for (llint in = 0; in <= n; in++) {
    for (llint ib = bmax; ib >= 0; ib--) {
      if (asum.at(in) + bsum.at(ib) <= k) {
        bmax = ib;
        anstmp = in + ib;
        if (ans < anstmp)
          ans = anstmp;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

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

using namespace std;

int main() {
#ifdef LOCAL_EXEC
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif // LOCAL_EXEC
  long long int n, i, j, k, m, ans = 0, a, b, mid;
  cin >> n >> m >> k;
  long long int A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }
  for (i = 1; i <= m; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }
  for (i = 1; i <= n; i++) {
    if (A[i] > k)
      break;
    a = 1;
    b = m;
    while (a <= b) {
      mid = a + (b - a) / 2;
      if (B[mid] == k - A[i]) {
        a = mid + 1;
        break;
      } else if (B[mid] > k - A[i])
        b = mid - 1;
      else
        a = mid + 1;
    }
    ans = max(ans, i + a - 1);
  }
  for (i = 1; i <= m; i++) {
    if (B[i] > k)
      break;
    a = 1;
    b = m;
    while (a <= b) {
      mid = a + (b - a) / 2;
      if (A[mid] == k - B[i]) {
        a = mid + 1;
        break;
      } else if (A[mid] > k - B[i])
        b = mid - 1;
      else
        a = mid + 1;
    }
    ans = max(ans, i + a - 1);
  }
  cout << ans;
  return 0;
}


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

using namespace std;

int main() {
#ifdef LOCAL_EXEC
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif // LOCAL_EXEC
  long long int n, i, j, k, m, ans = 0, a, b, mid;
  cin >> n >> m >> k;
  long long int A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> A[i];
    A[i] += A[i - 1];
  }
  for (i = 1; i <= m; i++) {
    cin >> B[i];
    B[i] += B[i - 1];
  }
  for (i = 1; i <= n; i++) {
    if (A[i] > k)
      break;
    a = 1;
    b = m;
    while (a <= b) {
      mid = a + (b - a) / 2;
      if (B[mid] == k - A[i]) {
        a = mid + 1;
        break;
      } else if (B[mid] > k - A[i])
        b = mid - 1;
      else
        a = mid + 1;
    }
    ans = max(ans, i + a - 1);
  }
  for (i = 1; i <= m; i++) {
    if (B[i] > k)
      break;
    a = 1;
    b = n;
    while (a <= b) {
      mid = a + (b - a) / 2;
      if (A[mid] == k - B[i]) {
        a = mid + 1;
        break;
      } else if (A[mid] > k - B[i])
        b = mid - 1;
      else
        a = mid + 1;
    }
    ans = max(ans, i + a - 1);
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *    author:  yuya1234
 *    created: 30.06.2020 10:57:25
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

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

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

  ll a[200001], b[200001];
  ll aMax = 1, bMax = 1, tmp, sum = 0;
  a[0] = b[0] = 0;
  FOR(i, 1, n) {
    cin >> tmp;
    sum += tmp;
    a[i] = sum;
    if (sum <= k) {
      aMax = i + 1;
    }
  }

  sum = 0;
  FOR(i, 1, m) {
    cin >> tmp;
    sum += tmp;
    b[i] = sum;
    if (sum <= k) {
      bMax = i + 1;
    }
  }

  ll ans = 0;

  REP(i, aMax) {
    REPD(j, bMax) {
      if (k >= a[i] + b[j]) {
        ans = max(ans, i + j);
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
/**
 *    author:  yuya1234
 *    created: 30.06.2020 10:57:25
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

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

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

  ll a[200001], b[200001];
  ll aMax = 1, bMax = 1, tmp, sum = 0;
  a[0] = b[0] = 0;
  FOR(i, 1, n) {
    cin >> tmp;
    sum += tmp;
    a[i] = sum;
    if (sum <= k) {
      aMax = i + 1;
    }
  }

  sum = 0;
  FOR(i, 1, m) {
    cin >> tmp;
    sum += tmp;
    b[i] = sum;
    if (sum <= k) {
      bMax = i + 1;
    }
  }

  ll ans = 0;

  REP(i, aMax) {
    REPD(j, bMax) {
      if (k >= a[i] + b[j]) {
        ans = max(ans, i + j);
        bMax = j + 1;
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long n, m, k;
  cin >> n >> m >> k;
  vector<long> a(n);
  vector<long> b(m);
  for (long i = 0; i < n; i++)
    cin >> a[i];
  for (long i = 0; i < m; i++)
    cin >> b[i];
  long total = 0;
  long c = 0;
  long cnt = 0;
  while (true) {
    if (total + a[c] <= k) {
      total += a[c];
      c++;
      cnt++;
      if (c == n)
        break;
    } else
      break;
  }
  long A = cnt;
  long e = 0;
  while (true) {
    if (total + b[e] <= k) {
      total += b[e];
      e++;
      cnt++;
      if (e == m)
        break;
    } else
      break;
  }
  long B = cnt - A;
  long ans = cnt;
  for (int i = 0; i < A; i++) {
    total -= a[A - 1 - i];
    cnt--;
    while (true) {
      if (total + b[B] <= k) {
        total += b[B];
        B++;
        cnt++;
      } else
        break;
    }
    if (cnt > ans)
      ans = cnt;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long n, m, k;
  cin >> n >> m >> k;
  vector<long> a(n);
  vector<long> b(m);
  for (long i = 0; i < n; i++)
    cin >> a[i];
  for (long i = 0; i < m; i++)
    cin >> b[i];
  long total = 0;
  long c = 0;
  long cnt = 0;
  while (true) {
    if (total + a[c] <= k) {
      total += a[c];
      c++;
      cnt++;
      if (c == n)
        break;
    } else
      break;
  }
  long A = cnt;
  long e = 0;
  while (true) {
    if (total + b[e] <= k) {
      total += b[e];
      e++;
      cnt++;
      if (e == m)
        break;
    } else
      break;
  }
  long B = cnt - A;
  long ans = cnt;
  for (int i = 0; i < A; i++) {
    total -= a[A - 1 - i];
    cnt--;
    while (true) {
      if (B == m)
        break;
      if (total + b[B] <= k) {
        total += b[B];
        B++;
        cnt++;
      } else
        break;
    }
    if (cnt > ans)
      ans = cnt;
  }
  cout << ans << endl;
}
Problem p02623 (C++)

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

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

  ll *a = new ll[n];
  ll *b = new ll[m];
  rep(i, 0, n - 1) { cin >> a[i]; }
  rep(i, 0, m - 1) { cin >> b[i]; }

  ll as = 0;
  ll bs = 0;
  ll bp = 0;
  ll ans = 0;
  rep(i, 0, n - 1) { as += a[i]; }
  repr(i, n, 0) {
    if (i < n) {
      as -= a[i];
    }
    // as -= a[i];
    if (as <= k) {
      while (bp < m && (as + bs <= k)) {
        bs += b[i];
        bp++;
      }
      ans = max(ans, bp + i);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
// #define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)

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

  ll *a = new ll[n];
  ll *b = new ll[m];
  rep(i, 0, n - 1) { cin >> a[i]; }
  rep(i, 0, m - 1) { cin >> b[i]; }

  ll as = 0;
  ll bs = 0;
  ll bp = 0;
  ll ans = 0;
  rep(i, 0, n - 1) { as += a[i]; }
  repr(i, n, 0) {
    if (i < n) {
      as -= a[i];
    }
    // as -= a[i];
    if (as <= k) {
      while (bp < m && (as + bs + b[bp] <= k)) {
        bs += b[bp];
        bp++;
      }
      ans = max(ans, bp + i);
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), aa(n + 1), bb(m + 1), time(n + m + 1);
  aa[0] = 0;
  bb[0] = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (ll i = 1; i <= n; i++) {
    aa[i] = aa[i - 1] + a[i - 1];
  }
  for (ll i = 1; i <= m; i++) {
    bb[i] = bb[i - 1] + b[i - 1];
  }

  ll max = 0;

  for (ll j = 0; j <= m; j++) {
    if (bb[j] <= k) {
      if (j > max) {
        max = j;
      }
    } else {
      break;
    }
  }

  ll maxmax = max;

  for (ll i = 1; i <= n; i++) {
    if (aa[i] > k)
      break;
    for (ll j = max; j >= 0; j--) {
      if (aa[i] + bb[j] <= k) {
        if (i + j > maxmax) {
          maxmax = j + i;
        }
        j = max;
        break;
      }
    }
  }

  cout << maxmax << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), aa(n + 1), bb(m + 1), time(n + m + 1);
  aa[0] = 0;
  bb[0] = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (ll i = 1; i <= n; i++) {
    aa[i] = aa[i - 1] + a[i - 1];
  }
  for (ll i = 1; i <= m; i++) {
    bb[i] = bb[i - 1] + b[i - 1];
  }

  ll max = 0;

  for (ll j = 0; j <= m; j++) {
    if (bb[j] <= k) {
      if (j > max) {
        max = j;
      }
    } else {
      break;
    }
  }

  ll maxmax = max;

  for (ll i = 1; i <= n; i++) {
    if (aa[i] > k)
      break;
    for (ll j = max; bb[j] > k - aa[i]; j--) {
      max--;
    }
    if (i + max > maxmax) {
      maxmax = max + i;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <time.h>
using namespace std;
#include <math.h>
#define ll long long
// to increase precision
// std::cout<<std::fixed;
// 	std::cout<<std::setprecision(6)<<z;
// clock_t tStart = clock();
// printf("Time taken: %.9fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
#define maxn 100001

#define ll long long

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long int> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[i + 1] = a[i] + t;
  }
  for (int i = 0; i < m; ++i) {
    int t;
    cin >> t;
    b[i + 1] = b[i] + t;
  }
  // for(auto x:a)
  // 	cout<<x<<" ";
  // cout<<endl;
  // for(auto x:b)
  // 	cout<<x<<" ";
  // cout<<endl;
  int ans = 0;
  int i = 0;
  while (b[i] <= k) {
    long long int res = k - b[i];
    // cout<<res<<endl;
    int ind = upper_bound(a.begin(), a.end(), res) - a.begin();
    // cout<<a[ind]<<endl;
    ans = max(ans, i + ind - 1);
    // cout<<i<<" "<<ind<<endl;
    i++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <time.h>
using namespace std;
#include <math.h>
#define ll long long
// to increase precision
// std::cout<<std::fixed;
// 	std::cout<<std::setprecision(6)<<z;
// clock_t tStart = clock();
// printf("Time taken: %.9fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
#define maxn 100001

#define ll long long

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long int> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[i + 1] = a[i] + t;
  }
  for (int i = 0; i < m; ++i) {
    int t;
    cin >> t;
    b[i + 1] = b[i] + t;
  }
  // for(auto x:a)
  // 	cout<<x<<" ";
  // cout<<endl;
  // for(auto x:b)
  // 	cout<<x<<" ";
  // cout<<endl;
  int ans = 0;
  int i = 0;
  while (b[i] <= k && i <= m) {
    long long int res = k - b[i];
    // cout<<res<<endl;
    int ind = upper_bound(a.begin(), a.end(), res) - a.begin();
    // cout<<a[ind]<<endl;
    ans = max(ans, i + ind - 1);
    // cout<<i<<" "<<ind<<endl;
    i++;
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> as(n);
  cin >> as;
  vector<ll> bs(m);
  cin >> bs;
  vector<ll> acca(n + 1), accb(m + 1);
  for (int i = 0; i < n; i++) {
    acca[i + 1] = acca[i] + as[i];
  }
  for (int i = 0; i < m; i++) {
    accb[i + 1] = acca[i] + bs[i];
  }
  printde(acca);
  printde(accb);
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (acca[i] > k) {
      break;
    }
    ans =
        max(ans, i + (int)(upper_bound(accb.begin(), accb.end(), k - acca[i]) -
                           accb.begin() - 1));
  }
  cout << ans << '\n';
}


Fixed submission:
//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> as(n);
  cin >> as;
  vector<ll> bs(m);
  cin >> bs;
  vector<ll> acca(n + 1), accb(m + 1);
  for (int i = 0; i < n; i++) {
    acca[i + 1] = acca[i] + as[i];
  }
  for (int i = 0; i < m; i++) {
    accb[i + 1] = accb[i] + bs[i];
  }
  printde(acca);
  printde(accb);
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (acca[i] > k) {
      break;
    }
    ans =
        max(ans, i + (int)(upper_bound(accb.begin(), accb.end(), k - acca[i]) -
                           accb.begin() - 1));
  }
  cout << ans << '\n';
}
Problem p02623 (C++)

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

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define Precision13 cout << fixed << setprecision(13)
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const int MOD = 1000000007;
const int INF = 1 << 29;
using Graph = vector<vector<int>>;

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

  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  REP1(i, n) cin >> a[i];
  REP1(i, m) cin >> b[i];

  vector<ll> asum(n + 1, 0);
  vector<ll> bsum(m + 1, 0);

  for (ll i = 1; i <= n; i++) {
    asum[i] = asum[i - 1] + a[i];
  }

  for (ll i = 1; i <= m; i++) {
    bsum[i] = bsum[i - 1] + b[i];
  }

  ll ans = 0;
  ll j = m;
  for (ll i = 1; i <= n; i++) {
    if (a[i] > k) {
      break;
    }
    while (asum[i] + bsum[j] > k) {
      j--;
    }
    ans = max(ans, i + j);
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef pair<string, string> Pstring;
typedef pair<double, double> Pdouble;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define Precision13 cout << fixed << setprecision(13)
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const int MOD = 1000000007;
const int INF = 1 << 29;
using Graph = vector<vector<int>>;

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

  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  REP1(i, n) cin >> a[i];
  REP1(i, m) cin >> b[i];

  vector<ll> asum(n + 1, 0);
  vector<ll> bsum(m + 1, 0);

  for (ll i = 1; i <= n; i++) {
    asum[i] = asum[i - 1] + a[i];
  }

  for (ll i = 1; i <= m; i++) {
    bsum[i] = bsum[i - 1] + b[i];
  }

  ll ans = 0;
  ll j = m;
  for (ll i = 0; i <= n; i++) {
    if (asum[i] > k) {
      break;
    }
    while (asum[i] + bsum[j] > k) {
      j--;
    }
    ans = max(ans, i + j);
  }

  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 repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n, m, k;
  cin >> n >> m >> k;
  vl sa(n + 1), sb(n + 1);
  rep(i, n) {
    int a;
    cin >> a;
    sa[i + 1] = sa[i] + a;
  }
  rep(i, m) {
    int b;
    cin >> b;
    sb[i + 1] = sb[i] + b;
  }

  int idx = m, ans = 0;

  rep(i, n + 1) {
    if (sa[i] > k)
      break;
    while (idx > 0 && sa[i] + sb[idx] > k)
      idx--;
    cmax(ans, i + idx);
  }

  cout << ans << "\n";

  return 0;
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n, m, k;
  cin >> n >> m >> k;
  vl sa(n + 1), sb(m + 1);
  rep(i, n) {
    int a;
    cin >> a;
    sa[i + 1] = sa[i] + a;
  }
  rep(i, m) {
    int b;
    cin >> b;
    sb[i + 1] = sb[i] + b;
  }

  int idx = m, ans = 0;

  rep(i, n + 1) {
    if (sa[i] > k)
      break;
    while (idx > 0 && sa[i] + sb[idx] > k)
      idx--;
    cmax(ans, i + idx);
  }

  cout << ans << "\n";

  return 0;
}
Problem p02623 (C++)

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

using namespace std;

typedef long long int ll;

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

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1), prefa(m + 1), prefb(m + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    prefa[i] = prefa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    prefb[i] = prefb[i - 1] + b[i];
  }

  ll ans;
  if (k >= prefb[m])
    ans = m;
  else if (k < b[1])
    ans = 0;
  else
    ans = upper_bound(prefb.begin(), prefb.end(), k) - prefb.begin() - 1;
  for (int i = 1; i <= n; i++) {
    if (k - prefa[i] < 0)
      continue;
    if (k - prefa[i] >= prefb[m]) {
      ans = max(ans, i + m);
      continue;
    }
    if (k - prefa[i] < b[1]) {
      ans = max(ans, (ll)i);
      continue;
    }
    ans = max(ans, upper_bound(prefb.begin(), prefb.end(), k - prefa[i]) -
                       prefb.begin() - 1 + (ll)i);
  }

  cout << ans << '\n';
}


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

using namespace std;

typedef long long int ll;

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

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1), prefa(n + 1), prefb(m + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    prefa[i] = prefa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    prefb[i] = prefb[i - 1] + b[i];
  }

  ll ans;
  if (k >= prefb[m])
    ans = m;
  else if (k < b[1])
    ans = 0;
  else
    ans = upper_bound(prefb.begin(), prefb.end(), k) - prefb.begin() - 1;
  for (int i = 1; i <= n; i++) {
    if (k - prefa[i] < 0)
      continue;
    if (k - prefa[i] >= prefb[m]) {
      ans = max(ans, i + m);
      continue;
    }
    if (k - prefa[i] < b[1]) {
      ans = max(ans, (ll)i);
      continue;
    }
    ans = max(ans, upper_bound(prefb.begin(), prefb.end(), k - prefa[i]) -
                       prefb.begin() - 1 + (ll)i);
  }

  cout << ans << '\n';
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;

int n, m, k;
vi a, b;

bool Read() {
  cin >> n;
  if (cin.eof())
    return 0;
  cin >> m >> k;
  a.resize(n);
  b.resize(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  return 1;
}

void Process() {
  int ans = 0, x = 0;
  ll suma = 0;
  for (int i = 0; i <= n && suma <= k; i++) {
    int y = 0;
    ll sumb = 0;
    for (int j = 0; j <= m && suma + sumb <= k; j++) {
      ans = max(ans, x + y);
      ++y;
      sumb += j < m ? b[j] : 0;
    }
    ++x;
    suma += i < n ? a[i] : 0;
  }
  cout << ans << "\n";
}

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

  while (Read())
    Process();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;

int n, m, k;
vi a, b;

bool Read() {
  cin >> n;
  if (cin.eof())
    return 0;
  cin >> m >> k;
  a.resize(n);
  b.resize(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  return 1;
}

void Process() {
  vector<ll> psaa(n), psab(m);
  psaa[0] = a[0];
  psab[0] = b[0];
  for (int i = 1; i < n; i++)
    psaa[i] = psaa[i - 1] + a[i];
  for (int i = 1; i < m; i++)
    psab[i] = psab[i - 1] + b[i];
  int i = 0, j = 0;
  while (i < n && psaa[i] <= k)
    i++;
  int ans = i;
  while (i >= 0) {
    const ll t = i ? psaa[i - 1] : 0;
    while (j < m && t + psab[j] <= k)
      j++;
    ans = max(ans, i + j);
    i--;
  }
  cout << ans << "\n";
}

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

  while (Read())
    Process();

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <locale.h>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int n, m, k;
  long long int a[200002], b[200002]; //
  int possa = 0;
  long long int ssx = 0, ax; //
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    ssx += a[i];
    if (ssx <= k) {
      possa = i;
      ax = ssx;
    } // possaはAだけ読んだ時に読める本の数 その時にかかる時間がax
  }
  for (int i = 1; i <= m; i++) {
    scanf("%lld", &b[i]);
  }
  int nj = 1;
  long long int bx = 0; //
  int ans = possa;
  for (int i = possa; i >= 0; i--) {
    for (int j = nj; j <= m; j++) {
      if (ax + bx + b[nj] <= k) {
        bx += b[nj];
        nj++;
        ans = max(ans, (i + j));
      }
    }
    ax -= a[i]; // axはiを読むまでにかかる時間を表す
  }
  printf("%d", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <locale.h>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int n, m, k;
  long long int a[200002], b[200002]; //
  int possa = 0;
  long long int ssx = 0, ax; //
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    ssx += a[i];
    if (ssx <= k) {
      possa = i;
      ax = ssx;
    } // possaはAだけ読んだ時に読める本の数 その時にかかる時間がax
  }
  for (int i = 1; i <= m; i++) {
    scanf("%lld", &b[i]);
  }
  int nj = 1;
  long long int bx = 0; //
  int ans = possa;
  for (int i = possa; i >= 0; i--) {
    for (int j = nj; j <= m; j++) {
      if (ax + bx + b[nj] <= k) {
        bx += b[nj];
        nj++;
        ans = max(ans, (i + j));
      } else {
        break;
      }
    }
    ax -= a[i]; // axはiを読むまでにかかる時間を表す
  }
  printf("%d", ans);
}
Problem p02623 (C++)

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

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

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

  int b_cnt = m;
  int res = 0;
  rep(a_cnt, n + 1) {
    while (a[a_cnt] + b[b_cnt] > k)
      --b_cnt;
    res = max(res, a_cnt + b_cnt);
  }

  cout << res << endl;

  return 0;
}

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

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

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

  int b_cnt = m;
  int res = 0;
  rep(a_cnt, n + 1) {
    if (a[a_cnt] > k)
      break;
    while (a[a_cnt] + b[b_cnt] > k)
      --b_cnt;
    res = max(res, a_cnt + b_cnt);
  }

  cout << res << endl;

  return 0;
}
Problem p02623 (C++)

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

using namespace std;

long long int psumA[100001];
long long int psumB[100001];
int A[100001], B[100001];

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

  int n, m, k;

  cin >> n >> m >> k;

  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++) {
    psumA[i] = psumA[i - 1] + A[i];
  }
  for (int i = 1; i <= m; i++) {
    psumB[i] = psumB[i - 1] + B[i];
  }

  int res = 0;

  for (int i = 0; i <= n; i++) {
    if (psumA[i] > k) {
      break;
    }
    int lo = 0;
    int hi = m;
    int Max = 0;
    long long int val = k - psumA[i];
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      if (psumB[mid] <= val) {
        Max = max(Max, mid);
        lo = mid + 1;
      } else {
        hi = mid - 1;
      }
    }
    res = max(res, i + Max);
  }

  cout << res << '\n';

  return 0;
}

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

using namespace std;

long long int psumA[200001];
long long int psumB[200001];
int A[200001], B[200001];

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

  int n, m, k;

  cin >> n >> m >> k;

  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++) {
    psumA[i] = psumA[i - 1] + A[i];
  }
  for (int i = 1; i <= m; i++) {
    psumB[i] = psumB[i - 1] + B[i];
  }

  int res = 0;

  for (int i = 0; i <= n; i++) {
    if (psumA[i] > k) {
      break;
    }
    int lo = 0;
    int hi = m;
    int Max = 0;
    long long int val = k - psumA[i];
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      if (psumB[mid] <= val) {
        Max = max(Max, mid);
        lo = mid + 1;
      } else {
        hi = mid - 1;
      }
    }
    res = max(res, i + Max);
  }

  cout << res << '\n';

  return 0;
}
Problem p02623 (C++)

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

using namespace std;
using ll = long long;

int main() {
  ll n, m, k = 0;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<ll> aSum(n + 1);
  vector<ll> bSum(m + 1);
  aSum[0] = 0;
  bSum[0] = 0;
  for (ll i = 1; i < n + 1; i++) {
    aSum[i] = a[i - 1] + aSum[i - 1];
  }
  for (ll i = 1; i < m + 1; i++) {
    bSum[i] = b[i - 1] + bSum[i - 1];
  }
  /*
      for(ll i = 0;i <= n;i++){
          cout << aSum[i] << endl;
      }
      for(ll i = 0;i <= m;i++){
          cout << bSum[i] << endl;
      }
  */
  ll bCP = m;
  ll sum = 0;
  for (ll i = 0; i <= n; i++) {
    if (aSum[i] > k) {
      continue;
    }
    for (ll j = bCP; j >= 0; j--) {
      if (aSum[i] + bSum[j] <= k) {
        bCP = j + 1;
        break;
      }
    }
    // cout << aSum[i] << " " << bSum[bCP] << endl;
    sum = max(sum, i + bCP);
  }

  cout << sum << endl;

  return 0;
}

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

using namespace std;
using ll = long long;

int main() {
  ll n, m, k = 0;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<ll> aSum(n + 1);
  vector<ll> bSum(m + 1);
  aSum[0] = 0;
  bSum[0] = 0;
  for (ll i = 1; i < n + 1; i++) {
    aSum[i] = a[i - 1] + aSum[i - 1];
  }
  for (ll i = 1; i < m + 1; i++) {
    bSum[i] = b[i - 1] + bSum[i - 1];
  }
  /*
      for(ll i = 0;i <= n;i++){
          cout << aSum[i] << endl;
      }
      for(ll i = 0;i <= m;i++){
          cout << bSum[i] << endl;
      }
  */
  ll bCP = m;
  ll sum = 0;
  for (ll i = 0; i <= n; i++) {
    if (aSum[i] > k) {
      continue;
    }
    for (ll j = bCP; j >= 0; j--) {
      if (aSum[i] + bSum[j] <= k) {
        bCP = j;
        break;
      }
    }
    // cout << aSum[i] << " " << bSum[bCP] << endl;
    sum = max(sum, i + bCP);
  }

  cout << sum << endl;

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define md 1000000007
double PI = 3.1415926535;
const int N = (int)(1 * 1e6 + 10);
using namespace std;

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

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

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define md 1000000007
double PI = 3.1415926535;
const int N = (int)(1 * 1e6 + 10);
using namespace std;

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

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

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

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

using namespace std;

#define pb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define pii pair<int, int>

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


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

using namespace std;

#define pb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define pii pair<int, int>

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (auto &c : a)
    cin >> c;
  for (auto &c : b)
    cin >> c;
  ll now = 0;
  for (auto &c : a)
    now += c;
  int j = 0, ans = 0;
  for (int i = n; i >= 0; i--) {
    if (i < n)
      now -= a[i];
    while (j < m && now + b[j] <= k) {
      now += b[j];
      j++;
    }
    if (now <= k)
      ans = max(ans, i + j);
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++)

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

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

  ll N;
  cin >> N;
  ll M;
  cin >> M;
  ll K;
  cin >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  vector<ll> B(M);
  for (ll i = 0; i < M; i++)
    cin >> B[i];

  vector<ll> sumA(N + 1, 0);
  vector<ll> sumB(M + 1, 0);
  for (ll i = 0; i < N; i++)
    sumA[i + 1] = sumA[i] + A[i];
  for (ll i = 0; i < M; i++)
    sumB[i + 1] = sumB[i] + B[i];

  ll ans = 0;
  ll b = 0;
  for (ll i = N; i >= 0; i--) {
    while (sumA[i] + sumB[b] <= K) {
      ans = max(ans, i + b);
      b++;
      b = min(b, M);
    }
  }

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

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

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

  ll N;
  cin >> N;
  ll M;
  cin >> M;
  ll K;
  cin >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  vector<ll> B(M);
  for (ll i = 0; i < M; i++)
    cin >> B[i];

  vector<ll> sumA(N + 1, 0);
  vector<ll> sumB(M + 1, 0);
  for (ll i = 0; i < N; i++)
    sumA[i + 1] = sumA[i] + A[i];
  for (ll i = 0; i < M; i++)
    sumB[i + 1] = sumB[i] + B[i];

  ll ans = 0;
  ll b = 0;
  for (ll i = N; i >= 0; i--) {
    while (sumA[i] + sumB[b] <= K) {
      ans = max(ans, i + b);
      b++;
      if (b > M) {
        b = M;
        break;
      }
    }
  }

  cout << ans << '\n';
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10, mod = 1e9 + 7;

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

  ll a[n + 1], b[m + 1];
  a[0] = 0, b[0] = 0;

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

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

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

  cout << ans << endl;
}

int main(void) {
  int t = 1;
  while (t--)
    solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10, mod = 1e9 + 7;

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

  ll a[n + 1], b[m + 1];
  a[0] = 0, b[0] = 0;

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

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

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

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

  cout << ans << endl;
}

int main(void) {
  int t = 1;
  while (t--)
    solve();
  return 0;
}