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

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

ll mod = 1000000007;

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

ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

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

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

  int ac = 0, bc = 0;
  while (ac < n) {
    if (a[ac] > k)
      break;
    k -= a[ac];
    ac++;
  }
  while (bc < m) {
    if (b[bc] > k)
      break;
    k -= b[bc];
    bc++;
  }

  int ret = ac + bc;
  while (ac > 0 && bc < m - 1) {
    k += a[ac - 1];
    ac--;
    while (bc < m) {
      if (b[bc] > k)
        break;
      k -= b[bc];
      bc++;
    }
    ret = max(ret, ac + bc);
  }

  cout << ret << endl;
}

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

ll mod = 1000000007;

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

ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

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

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

  int ac = 0, bc = 0;
  while (ac < n) {
    if (a[ac] > k)
      break;
    k -= a[ac];
    ac++;
  }
  while (bc < m) {
    if (b[bc] > k)
      break;
    k -= b[bc];
    bc++;
  }

  int ret = ac + bc;
  while (ac > 0 && bc < m - 1) {
    k += a[ac - 1];
    ac--;
    while (bc < m) {
      if (b[bc] > k)
        break;
      k -= b[bc];
      bc++;
    }
    ret = max(ret, ac + bc);
  }

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

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

using namespace std;
using ull = unsigned long long;

#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i, s, n) for (int i = s; i < (int)(n); ++i)

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

  vector<ull> As(N + 1);
  vector<ull> Bs(M + 1);

  {
    ull sum = 0;
    Rep(i, N) {
      ull j;
      cin >> j;
      sum += j;
      As[i + 1] = sum;
    }
  }
  {
    ull sum = 0;
    Rep(i, M) {
      ull j;
      cin >> j;
      sum += j;
      Bs[i + 1] = sum;
    }
  }

  int answer = 0;
  Rep(n, N + 1) {
    Rep(m, M + 1) {
      if (As[n] + Bs[m] <= K) {
        answer = max(answer, n + m);
      }
    }
  }

  cout << answer << endl;
  return 0;
}


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

using namespace std;
using ull = unsigned long long;

#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i, s, n) for (int i = s; i < (int)(n); ++i)

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

  vector<ull> As(N + 1);
  vector<ull> Bs(M + 1);

  {
    ull sum = 0;
    Rep(i, N) {
      ull j;
      cin >> j;
      sum += j;
      As[i + 1] = sum;
    }
  }
  {
    ull sum = 0;
    Rep(i, M) {
      ull j;
      cin >> j;
      sum += j;
      Bs[i + 1] = sum;
    }
  }

  int answer = 0;
  Rep(n, N + 1) {
    ull remain = K - As[n];
    auto bound = lower_bound(Bs.begin(), Bs.end(), remain);
    if (*bound > remain)
      bound--;
    if (bound != Bs.end()) {
      int pos = (int)(bound - Bs.begin());
      answer = max(answer, n + pos);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define ubo upper_bound
#define lbo lower_bound
#define VI vector<int>
#define VL vector<ll>
#define VP vector<pii>
#define all(x) x.begin(), x.end()
#define M vector<VI>

const int N = 202020;

using namespace std;

int n, m;
ll k, a[N], b[N];

int main() {

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

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

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

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define ubo upper_bound
#define lbo lower_bound
#define VI vector<int>
#define VL vector<ll>
#define VP vector<pii>
#define all(x) x.begin(), x.end()
#define M vector<VI>

const int N = 202020;

using namespace std;

int n, m;
ll k, a[N], b[N];

int main() {

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

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

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

  cout << ans << '\n';

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100123
#define ll long long
#define endl '\n'

using namespace std;

int n, m, ans, curr;
ll k, a[N], b[N];

void print_vetor(ll v[], int size) {
  for (int i = 0; i < size; ++i) {
    cout << v[i] << ' ';
  }
  cout << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> k;

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

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

  partial_sum(a, a + n, a);
  partial_sum(b, b + m, b);

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

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

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 200123
#define ll long long
#define endl '\n'

using namespace std;

int n, m, ans, curr;
ll k, a[N], b[N];

void print_vetor(ll v[], int size) {
  for (int i = 0; i < size; ++i) {
    cout << v[i] << ' ';
  }
  cout << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> k;

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

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

  partial_sum(a, a + n, a);
  partial_sum(b, b + m, b);

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

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

  cout << ans << endl;

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 400005;
const ll INF = 1LL << 60;

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  ll K;
  cin >> N >> M >> K;
  vector<ll> 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];
  }
  A.push_back(INF);
  B.push_back(INF);

  int ans = 0;
  int j = N;
  for (int i = 0; i <= N; i++) {
    if (A[i] > K)
      break;
    while (j >= 0 && A[i] + B[j] > K)
      j--;
    chmax(ans, i + j);
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 400005;
const ll INF = 1LL << 60;

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  ll K;
  cin >> N >> M >> K;
  vector<ll> 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];
  }
  A.push_back(INF);
  B.push_back(INF);

  int ans = 0;
  int j = M;
  for (int i = 0; i <= N; i++) {
    if (A[i] > K)
      break;
    while (j >= 0 && A[i] + B[j] > K)
      j--;
    chmax(ans, i + j);
  }

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

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

inline int read() {
  re int ans = 0;
  re bool f = 1;
  re char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ans = (ans << 3) + (ans << 1) + (ch ^ 48);
    ch = getchar();
  }
  return f ? ans : ~(ans - 1);
}
int a[204800], b[204800];

signed main(void) {
  int n, m, k, ans = 0;
  n = read(), m = read(), k = read();
  for (re int i = 1; i <= n; ++i)
    a[i] = a[i - 1] + read();
  for (re int i = 1; i <= m; ++i)
    b[i] = b[i - 1] + read();
  for (re int i = 1; i <= n; ++i) {
    if (a[i] > k)
      n = i - 1;
  }
  for (re int i = 1; i <= m; ++i) {
    if (b[i] > k)
      m = i - 1;
  }
  for (re int i = 0; i <= n; ++i) {
    if (a[i] > k)
      break;
    for (re int j = 0; j <= m; ++j) {
      if (b[j] > k)
        break;
      if (i + j <= ans)
        continue;
      if (a[i] + b[j] <= k)
        ans = max(ans, i + j);
      else
        break;
    }
  }
  printf("%lld\n", ans);
  return 0;
}

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

inline int read() {
  re int ans = 0;
  re bool f = 1;
  re char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ans = (ans << 3) + (ans << 1) + (ch ^ 48);
    ch = getchar();
  }
  return f ? ans : ~(ans - 1);
}
int a[204800], b[204800];

signed main(void) {
  int n, m, k, ans = 0;
  n = read(), m = read(), k = read();
  for (re int i = 1; i <= n; ++i)
    a[i] = a[i - 1] + read();
  for (re int i = 1; i <= m; ++i)
    b[i] = b[i - 1] + read();
  int j = m;
  for (re int i = 0; i <= n && a[i] <= k; ++i) {
    while (j >= 0 && a[i] + b[j] > k)
      j--;
    ans = max(ans, i + j);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  for (int i = 1; i < n; i++)
    a[i] = a[i] + a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] = b[i] + b[i - 1];
  ll ans = 0;
  rep(i, n) {
    ll cnt = 0;
    if (k - a[i] >= 0)
      cnt = i + 1;
    cnt += upper_bound(all(b), k - a[i]) - b.begin();
    chmax(ans, cnt);
  }
  chmax(ans, ll(upper_bound(all(b), k) - b.begin()));
  cout << ans << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  for (int i = 1; i < n; i++)
    a[i] = a[i] + a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] = b[i] + b[i - 1];
  ll ans = 0;
  rep(i, n) {
    ll cnt = 0;
    if (k - a[i] >= 0)
      cnt = i + 1;
    cnt += upper_bound(all(b), k - a[i]) - b.begin();
    chmax(ans, cnt);
  }
  chmax(ans, ll(upper_bound(all(b), k) - b.begin()));
  cout << ans << endl;
}
Problem p02623 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    n, m, k = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    sum_A = [A[0]]
    sum_B = [B[0]]

    for i in range(n - 1):
        sum_A.append(sum_A[i] + A[i + 1])

    for i in range(m - 1):
        sum_B.append(sum_B[i] + B[i + 1])

    R = []

    b = m - 1
    flag = False
    while b > -1:
        if sum_B[b] <= k:
            R.append(b + 1)
            flag = True
            break
        else:
            b -= 1

    if not flag:
        R.append(0)

    for i in range(n):
        a = sum_A[i]

        b = m - 1
        flag = False
        while b > -1:
            if sum_B[b] <= k - a:
                R.append(i + 1 + b + 1)
                flag = True
                break
            else:
                b -= 1

        if not flag:
            if 0 <= k - a:
                R.append(i + 1)
            else:
                R.append(0)

    print(max(R))
    # while k > 0 and (a < n or b < m):
    #     if a == n:
    #         time = B[b]
    #     elif b == m:
    #         time = A[a]
    #     else:
    #         time = min(A[a], B[b])

    #     if k - time < 0:
    #         break

    #     r += 1
    #     k -= time
    #     print(k)

    #     if a == n:
    #         b += 1
    #     elif b == m:
    #         a += 1
    #     else:
    #         if A[a] >= B[b]:
    #             b += 1
    #         else:
    #             a += 1

    # print(r)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, m, k = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    sum_A = [A[0]]
    sum_B = [B[0]]

    for i in range(n - 1):
        sum_A.append(sum_A[i] + A[i + 1])

    for i in range(m - 1):
        sum_B.append(sum_B[i] + B[i + 1])

    R = []

    b = m - 1
    flag = False
    while b > -1:
        if sum_B[b] <= k:
            R.append(b + 1)
            flag = True
            break
        else:
            b -= 1

    if not flag:
        R.append(0)

    for i in range(n):
        a = sum_A[i]

        flag = False
        while b > -1:
            if sum_B[b] <= k - a:
                R.append(i + 1 + b + 1)
                flag = True
                break
            else:
                b -= 1

        if not flag:
            if 0 <= k - a:
                R.append(i + 1)
            else:
                R.append(0)

    print(max(R))
    # while k > 0 and (a < n or b < m):
    #     if a == n:
    #         time = B[b]
    #     elif b == m:
    #         time = A[a]
    #     else:
    #         time = min(A[a], B[b])

    #     if k - time < 0:
    #         break

    #     r += 1
    #     k -= time
    #     print(k)

    #     if a == n:
    #         b += 1
    #     elif b == m:
    #         a += 1
    #     else:
    #         if A[a] >= B[b]:
    #             b += 1
    #         else:
    #             a += 1

    # print(r)


if __name__ == "__main__":
    main()
Problem p02623 (Python)

Buggy submission (status: Runtime Error):
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    a, b = [0], [0]
    for i in range(N):
        a.append(a[i] + A[i])
    for i in range(M):
        a.append(b[i] + B[i])

    ans, j = 0, M
    for i in range(N + 1):
        if a[i] > K:
            break

        while a[i] + b[j] > K:
            j -= 1

        ans = max(ans, i + j)

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    a, b = [0], [0]
    for i in range(N):
        a.append(a[i] + A[i])
    for i in range(M):
        b.append(b[i] + B[i])

    ans, j = 0, M
    for i in range(N + 1):
        if a[i] > K:
            break

        while a[i] + b[j] > K:
            j -= 1

        ans = max(ans, i + j)

    print(ans)


if __name__ == "__main__":
    main()
Problem p02623 (C++)

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

inline int D() {
  int t;
  scanf("%d", &t);
  return t;
}
inline ll LLD() {
  ll t;
  scanf("%lld", &t);
  return t;
}
// const int INF = 0x3f3f3f3f;
// const int mod=1e9+7;
// const double pi=3.14159265359;
// ###################################################################

int main() {
  int n = D(), m = D(), k = D();
  int a[n], b[m];
  ll cm1[n + 1], cm2[m + 1];
  cm1[0] = 0, cm2[0] = 0;
  for (int i = 0; i <= n; i++) {
    if (i < n)
      a[i] = D();
    if (i)
      cm1[i] = cm1[i - 1] + a[i - 1];
  }

  for (int i = 0; i <= m; i++) {
    if (i < m)
      b[i] = D();
    if (i)
      cm2[i] = cm2[i - 1] + b[i - 1];
  }
  int ans = 0;
  for (int i = 0; i <= n && cm1[i] <= k; i++) {
    if (cm1[i] > k)
      break;
    for (int j = 0; j <= m && (cm2[j] + cm1[i]) <= k; j++) {
      if (cm2[j] + cm1[i] > k)
        break;
      ans = max(ans, i + j);
    }
  }
  cout << ans;
}

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

inline int D() {
  int t;
  scanf("%d", &t);
  return t;
}
inline ll LLD() {
  ll t;
  scanf("%lld", &t);
  return t;
}
// const int INF = 0x3f3f3f3f;
// const int mod=1e9+7;
// const double pi=3.14159265359;
// ###################################################################

int main() {
  int n = D(), m = D(), k = D();
  int a[n], b[m];
  ll cm1[n + 1], cm2[m + 1];
  cm1[0] = 0, cm2[0] = 0;
  for (int i = 0; i <= n; i++) {
    if (i < n)
      a[i] = D();
    if (i)
      cm1[i] = cm1[i - 1] + a[i - 1];
  }

  for (int i = 0; i <= m; i++) {
    if (i < m)
      b[i] = D();
    if (i)
      cm2[i] = cm2[i - 1] + b[i - 1];
  }
  int ans = 0;
  for (int i = 0; i <= n && cm1[i] <= k; i++) {
    if (cm1[i] > k)
      break;
    int it = upper_bound(cm2, cm2 + m + 1, (k - cm1[i])) - cm2;
    ans = max(ans, i + it - 1);
  }
  cout << ans;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

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

signed main() {
  cin >> N >> M >> K;
  vector<ll> a(N), b(M);
  cin >> a;
  cin >> b;
  vector<ll> sb(M);
  sb[0] = b[0];
  REPS(i, N - 1) { sb[i] = sb[i - 1] + b[i]; }
  auto itr = upper_bound(ALL(sb), K);
  ans = itr - sb.begin();
  dump(sb);
  dump(ans);
  REP(i, N) {
    K -= a[i];
    if (K < 0) {
      break;
    }
    auto itr = upper_bound(ALL(sb), K);
    ans = max(ans, (ll)(itr - sb.begin() + i + 1));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

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

signed main() {
  cin >> N >> M >> K;
  vector<ll> a(N), b(M);
  cin >> a;
  cin >> b;
  vector<ll> sb(M);
  sb[0] = b[0];
  REPS(i, M - 1) { sb[i] = sb[i - 1] + b[i]; }
  auto itr = upper_bound(ALL(sb), K);
  ans = itr - sb.begin();
  dump(sb);
  dump(ans);
  REP(i, N) {
    K -= a[i];
    if (K < 0) {
      break;
    }
    auto itr = upper_bound(ALL(sb), K);
    ans = max(ans, (ll)(itr - sb.begin() + i + 1));
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define fl(i, n) for (i = 0; i < n; i++)
#define fld(i, a, b) for (i = a; i > b; i--)
#define fli(i, a, n) for (i = a; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define mod 998244353
#define all(x) x.begin(), x.end()
#define speedup ios_base::sync_with_stdio(false), cin.tie(0)
using namespace std;
ll power(ll x, ll y) {
  ll out = 1;
  while (y > 0) {
    if (y & 1)
      out = (out * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return out % mod;
}
ll inverse(ll a) { return power(a, mod - 2) % mod; }
ll pref_v1[2001];
ll pref_v2[2001];
bool check(ll mid, ll k, ll m, ll n) {
  ll i = 0, j;
  while (i <= mid && i <= n) {
    if (mid - i <= m) {
      j = mid - i;
      if (pref_v1[i] + pref_v2[j] <= k)
        return true;
    }
    i++;
  }
  return false;
}
void comp() {
  ll i, j, k, n, m;
  cin >> n >> m >> k;
  vector<ll> v1(n + 1), v2(m + 1);
  fl(i, n) { cin >> v1[i + 1]; }
  fl(i, m) { cin >> v2[i + 1]; }
  pref_v2[1] = v2[1];
  pref_v1[1] = v1[1];
  fli(i, 2, n + 1) pref_v1[i] = pref_v1[i - 1] + v1[i];
  fli(i, 2, m + 1) pref_v2[i] = pref_v2[i - 1] + v2[i];
  ll low = 0, high = (n + m), mid;
  ll ans = 0;
  while (low <= high) {
    mid = (low + high) / 2;
    if (check(mid, k, m, n)) {
      low = mid + 1;
      ans = mid;
    } else {
      high = mid - 1;
    }
  }
  cout << ans << "\n";
}
int main() {
  speedup;
  ll i = 0, t;
  t = 1;
  // cin>>t;
  while (t--) {
    comp();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define fl(i, n) for (i = 0; i < n; i++)
#define fld(i, a, b) for (i = a; i > b; i--)
#define fli(i, a, n) for (i = a; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define mod 998244353
#define all(x) x.begin(), x.end()
#define speedup ios_base::sync_with_stdio(false), cin.tie(0)
using namespace std;
ll power(ll x, ll y) {
  ll out = 1;
  while (y > 0) {
    if (y & 1)
      out = (out * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return out % mod;
}
ll inverse(ll a) { return power(a, mod - 2) % mod; }
ll pref_v1[200001];
ll pref_v2[200001];
bool check(ll mid, ll k, ll m, ll n) {
  ll i = 0, j;
  while (i <= mid && i <= n) {
    if (mid - i <= m) {
      j = mid - i;
      if (pref_v1[i] + pref_v2[j] <= k)
        return true;
    }
    i++;
  }
  return false;
}
void comp() {
  ll i, j, k, n, m;
  cin >> n >> m >> k;
  vector<ll> v1(n + 1), v2(m + 1);
  fl(i, n) { cin >> v1[i + 1]; }
  fl(i, m) { cin >> v2[i + 1]; }
  pref_v2[1] = v2[1];
  pref_v1[1] = v1[1];
  fli(i, 2, n + 1) pref_v1[i] = pref_v1[i - 1] + v1[i];
  fli(i, 2, m + 1) pref_v2[i] = pref_v2[i - 1] + v2[i];
  ll low = 0, high = (n + m), mid;
  ll ans = 0;
  while (low <= high) {
    mid = (low + high) / 2;
    if (check(mid, k, m, n)) {
      low = mid + 1;
      ans = mid;
    } else {
      high = mid - 1;
    }
  }
  cout << ans << "\n";
}
int main() {
  speedup;
  ll i = 0, t;
  t = 1;
  // cin>>t;
  while (t--) {
    comp();
  }
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define all(x) x.begin(), x.end()
ll power(ll a, ll b) {
  ll res = 1;
  a = a % mod;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
ll invmod(ll a) { return power(a, mod - 2); }
int main(void) {
  FIO int t = 1;
  // cin >> t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll a[n + 1], b[n + 1];
    a[0] = b[0] = 0;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    for (int i = 1; i <= m; i++)
      cin >> b[i];
    for (int i = 0; i < n; i++)
      a[i + 1] += a[i];
    for (int i = 0; i < m; i++)
      b[i + 1] += b[i];
    int ans = 0, j = m;
    for (int i = 0; i <= n; i++) {
      if (a[i] > k)
        break;
      while (j >= 0 && b[j] > k - a[i])
        j--;
      ans = max(ans, i + j);
    }
    cout << ans << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define all(x) x.begin(), x.end()
ll power(ll a, ll b) {
  ll res = 1;
  a = a % mod;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
ll invmod(ll a) { return power(a, mod - 2); }
int main(void) {
  FIO int t = 1;
  // cin >> t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll a[n + 1], b[m + 1];
    a[0] = b[0] = 0;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    for (int i = 1; i <= m; i++)
      cin >> b[i];
    for (int i = 0; i < n; i++)
      a[i + 1] += a[i];
    for (int i = 0; i < m; i++)
      b[i + 1] += b[i];
    int ans = 0, j = m;
    for (int i = 0; i <= n; i++) {
      if (a[i] > k)
        break;
      while (j >= 0 && b[j] > k - a[i])
        j--;
      ans = max(ans, i + j);
    }
    cout << ans << "\n";
  }
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int m, n;
ll k;

vector<ll> a, b;

int main() {
  int bkmax = 0;
  int amax, bmax;
  int i, j;

  cin >> n >> m >> k;
  a.resize(n + 1);
  b.resize(m + 1);

  a[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    // printf("a[%d] = %lld\n", i, a[i]);
    if (a[i - 1] <= k && a[i] > k)
      amax = i - 1;
  }
  if (a[n] <= k)
    amax = n;
  // printf("amax = %d\n", amax);
  b[0] = 0;
  for (i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
    // printf("b[%d] = %lld\n", i, b[i]);
    if (b[i - 1] <= k && b[i] > k)
      bmax = i - 1;
  }
  if (b[m] <= k)
    bmax = m;
  // printf("bmax = %d\n", bmax);

  i = amax;
  for (j = 0; j <= bmax; j++) {
    if (a[i] + b[j] > k) {
      break;
    }
  }
  j--;
  bkmax = max(bkmax, i + j);
  // printf("bkmax = %d, a[%d]+b[%d] = %lld\n", bkmax, i, j, a[i]+b[j]);
  for (i--; i >= 0; i--) {
    while (a[i] + b[j] <= k) {
      bkmax = max(bkmax, i + j);
      // printf("bkmax = %d, a[%d]+b[%d] = %lld\n", bkmax, i, j, a[i]+b[j]);
      j++;
    }
  }

  cout << bkmax << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int m, n;
ll k;

vector<ll> a, b;

int main() {
  int bkmax = 0;
  int amax, bmax;
  int i, j;

  cin >> n >> m >> k;
  a.resize(n + 1);
  b.resize(m + 1);

  a[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    // printf("a[%d] = %lld\n", i, a[i]);
    if (a[i - 1] <= k && a[i] > k)
      amax = i - 1;
  }
  if (a[n] <= k)
    amax = n;
  // printf("amax = %d\n", amax);
  b[0] = 0;
  for (i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
    // printf("b[%d] = %lld\n", i, b[i]);
    if (b[i - 1] <= k && b[i] > k)
      bmax = i - 1;
  }
  if (b[m] <= k)
    bmax = m;
  // printf("bmax = %d\n", bmax);

  i = amax;
  for (j = 0; j <= bmax; j++) {
    if (a[i] + b[j] > k) {
      break;
    }
  }
  j--;
  bkmax = max(bkmax, i + j);
  // printf("bkmax = %d, a[%d]+b[%d] = %lld\n", bkmax, i, j, a[i]+b[j]);
  for (i--; i >= 0 && j <= m; i--) {
    while (a[i] + b[j] <= k && j <= m) {
      bkmax = max(bkmax, i + j);
      // printf("bkmax = %d, a[%d]+b[%d] = %lld\n", bkmax, i, j, a[i]+b[j]);
      j++;
    }
  }

  cout << bkmax << endl;

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, m, k, a[200001], b[200001], A, ans, L;
vector<ll> v, u;
int main(void) {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  v.push_back(0);
  u.push_back(0);
  for (int i = 0; i < n; i++) {
    v.push_back(v[i] + a[i]);
  }
  for (int i = 0; i < m; i++) {
    u.push_back(v[i] + b[i]);
  }
  for (ll i = 0LL; i <= n; i++) {
    A = k - v[i];
    // cout<<A<<endl;
    if (A < 0)
      continue;
    L = upper_bound(u.begin(), u.end(), A) - u.begin();
    // cout<<i<<" "<<L<<endl;
    ans = max(ans, i + L - 1);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, m, k, a[200001], b[200001], A, ans, L;
vector<ll> v, u;
int main(void) {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  v.push_back(0);
  u.push_back(0);
  for (int i = 0; i < n; i++) {
    v.push_back(v[i] + a[i]);
  }
  for (int i = 0; i < m; i++) {
    u.push_back(u[i] + b[i]);
  }
  for (ll i = 0LL; i <= n; i++) {
    A = k - v[i];
    // cout<<A<<endl;
    if (A < 0)
      continue;
    L = upper_bound(u.begin(), u.end(), A) - u.begin();
    // cout<<i<<" "<<L<<endl;
    ans = max(ans, i + L - 1);
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int read() {
  int x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-')
      f = -1;
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
ll a[100005], b[100005], n, m, k, ans;
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    a[i] = a[i - 1] + read();
  for (int i = 1; i <= m; i++)
    b[i] = b[i - 1] + read();
  ll now = 0;
  while (b[now] <= k && now <= m)
    now++;
  now--;
  ans = now;
  for (int i = 1; i <= n; i++) {
    if (a[i] > k)
      break;
    while (a[i] + b[now] > k)
      now--;
    ans = max(ans, i + now);
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int read() {
  int x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-')
      f = -1;
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
ll a[200005], b[200005], n, m, k, ans;
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    a[i] = a[i - 1] + read();
  for (int i = 1; i <= m; i++)
    b[i] = b[i - 1] + read();
  ll now = 0;
  while (b[now] <= k && now <= m)
    now++;
  now--;
  ans = now;
  for (int i = 1; i <= n; i++) {
    if (a[i] > k)
      break;
    while (a[i] + b[now] > k)
      now--;
    ans = max(ans, i + now);
  }
  cout << ans;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

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

  vector<long long> A(N), AS(N + 1, 0), B(M), BS(M + 1, 0);

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    AS[i + 1] = AS[i] + A[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> B[i];
    BS[i + 1] = BS[i] + B[i];
  }

  int an = 0, bn = 0, ans = 0;
  for (an = N; an >= 0; --an) {
    while (BS[bn + 1] <= K - AS[an])
      ++bn;
    if (K >= AS[an] + BS[bn])
      ans = max(an + bn, ans);
  }

  cout << ans << endl;
}

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

using namespace std;

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

  vector<long long> A(N), AS(N + 1, 0), B(M), BS(M + 1, 0);

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    AS[i + 1] = AS[i] + A[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> B[i];
    BS[i + 1] = BS[i] + B[i];
  }

  int an = 0, bn = 0, ans = 0;
  for (an = N; an >= 0; --an) {
    while (bn + 1 <= M && BS[bn + 1] <= K - AS[an])
      ++bn;
    if (K >= AS[an] + BS[bn])
      ans = max(an + bn, ans);
  }

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

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

long long A[100002], B[100002];

int main() {

  long long n, m, ms;
  cin >> n >> m >> ms;

  for (int A_i = 0; A_i < n; A_i++) {
    cin >> A[A_i];
  }
  for (int B_i = 0; B_i < m; B_i++) {
    cin >> B[B_i];
  }

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

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

  long long ans = x;

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

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

  cout << ans << endl;

  return 0;
}

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

long long A[200000], B[200000];

int main() {

  long long n, m, ms;
  cin >> n >> m >> ms;

  for (int A_i = 0; A_i < n; A_i++) {
    cin >> A[A_i];
  }
  for (int B_i = 0; B_i < m; B_i++) {
    cin >> B[B_i];
  }

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

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

  long long ans = x;

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

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

  cout << ans << endl;

  return 0;
}
Problem p02623 (Python)

Buggy submission (status: Runtime Error):
# f = open('test.txt')
f = open("b11.txt")
n, m, k = map(int, f.readline().split())
a = list(map(int, f.readline().split()))
b = list(map(int, f.readline().split()))

a_sum = [0]
b_sum = [0]
cnt = 0
best_b = m
confirm = ()

for i in a:
    a_sum.append(i + a_sum[-1])

for i in b:
    b_sum.append(i + b_sum[-1])

for a_idx in range(n + 1):
    if a_sum[a_idx] > k:
        break
    for b_idx in reversed(range(best_b + 1)):
        if b_sum[b_idx] + a_sum[a_idx] <= k:
            # if cnt < a_idx + b_idx:
            cnt = max(cnt, a_idx + b_idx)
            best_b = b_idx
            break

print(cnt)


Fixed submission:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a_sum = [0]
b_sum = [0]
cnt = 0
best_b = m
confirm = ()

for i in a:
    a_sum.append(i + a_sum[-1])

for i in b:
    b_sum.append(i + b_sum[-1])

for a_idx in range(n + 1):
    if a_sum[a_idx] > k:
        break
    for b_idx in reversed(range(best_b + 1)):
        if b_sum[b_idx] + a_sum[a_idx] <= k:
            # if cnt < a_idx + b_idx:
            cnt = max(cnt, a_idx + b_idx)
            best_b = b_idx
            break

print(cnt)
Problem p02623 (Python)

Buggy submission (status: Runtime Error):
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]
for i in range(n):
    a.append(a[i] + A[i])
for i in range(m):
    b.append(b[i] + B[i])
ans = 0
j = m
for i in range(n + 1):
    if a[0] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)


Fixed submission:
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]
for i in range(n):
    a.append(a[i] + A[i])
for i in range(m):
    b.append(b[i] + B[i])
ans = 0
j = m
for i in range(n + 1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)
Problem p02623 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-
# 標準入力を取得
N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

# 求解処理
a, b = [0], [0]
for i in range(N):
    a.append(a[i] + A[i])
for j in range(M):
    b.append(b[i] + B[i])

ans, j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)

# 結果出力
print(ans)


Fixed submission:
# -*- coding: utf-8 -*-
# 標準入力を取得
N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

# 求解処理
a, b = [0], [0]
for i in range(N):
    a.append(a[i] + A[i])
for j in range(M):
    b.append(b[j] + B[j])

ans, j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)

# 結果出力
print(ans)
Problem p02623 (C++)

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

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  for (ll i = 0; i < N; i++)
    cin >> A.at(i);
  for (ll i = 0; i < M; i++)
    cin >> B.at(i);

  ll ans = 0, i = 0, j = 0, T = K;
  while (i < M) {
    if (T - B.at(i) >= 0) {
      T -= B.at(i);
      i++;
      ans++;
    } else {
      i--;
      break;
    }
  }
  ll tmp = ans;
  while (j < N) {
    // cout << tmp << ": i = " << i << ", j = " << j << ", T = " << T << endl;
    if (T - A.at(j) >= 0) {
      T -= A.at(j);
      j++;
      tmp++;
    } else {
      if (i >= 0) {
        T += B.at(i);
        i--;
        tmp--;
      } else
        break;
    }
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  for (ll i = 0; i < N; i++)
    cin >> A.at(i);
  for (ll i = 0; i < M; i++)
    cin >> B.at(i);

  ll ans = 0, i = 0, j = 0, T = K;
  while (true) {
    if (i >= M) {
      i--;
      break;
    }
    if (T - B.at(i) >= 0) {
      T -= B.at(i);
      i++;
      ans++;
    } else {
      i--;
      break;
    }
  }
  ll tmp = ans;
  while (j < N) {
    // cout << tmp << ": i = " << i << ", j = " << j << ", T = " << T << endl;
    if (T - A.at(j) >= 0) {
      T -= A.at(j);
      j++;
      tmp++;
    } else {
      if (i >= 0) {
        T += B.at(i);
        i--;
        tmp--;
      } else
        break;
    }
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long int ll;
typedef unsigned long long ull;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef pair<ll, ll> P;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long int ll;
typedef unsigned long long ull;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef pair<ll, ll> P;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1);
  int ans = 0, N = 0, M = 0;
  a[0] = 0;
  b[0] = 0;
  rep(i, n) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  rep(i, m) {
    cin >> b[i + 1];
    b[i + 1] += b[i];
  }
  int o = m;
  for (int i = 0; i <= n; i++) {
    while (a[i] + b[o] > k && o >= 0) {
      o--;
    }
    // cout<<ans<<endl;
    if (o >= 0)
      ans = max(ans, i + o);
    else
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (Python)

Buggy submission (status: Runtime Error):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_sum = [0]
for i in range(N):
    A_sum.append(A_sum[i] + A[i])
B_sum = [0]
for i in range(N):
    B_sum.append(B_sum[i] + B[i])
B_sum.append(10**10)

res = 0
for i in range(len(A_sum)):
    low = 0
    high = len(B_sum)
    while high - low > 1:
        mid = (low + high) // 2
        if A_sum[i] + B_sum[mid] <= K:
            low = mid
        else:
            high = mid
    if A_sum[i] + B_sum[low] <= K:
        res = max(res, i + low)

print(res)


Fixed submission:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_sum = [0]
for i in range(N):
    A_sum.append(A_sum[i] + A[i])
B_sum = [0]
for i in range(M):
    B_sum.append(B_sum[i] + B[i])
B_sum.append(10**10)

res = 0
for i in range(len(A_sum)):
    low = 0
    high = len(B_sum)
    while high - low > 1:
        mid = (low + high) // 2
        if A_sum[i] + B_sum[mid] <= K:
            low = mid
        else:
            high = mid
    if A_sum[i] + B_sum[low] <= K:
        res = max(res, i + low)

print(res)
Problem p02623 (Python)

Buggy submission (status: Runtime Error):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

b_sum = 0
for i in range(M):
    b_sum += B[i]
    if b_sum > K:
        b_sum -= B[i]
        j = i - 1
        break
result = j + 1

a_sum = 0
for i in range(N):
    a_sum += A[i]
    if a_sum > K:
        break
    while a_sum + b_sum > K:
        b_sum -= B[j]
        j -= 1
    result = max(result, (i + 1) + (j + 1))
print(result)


Fixed submission:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

b_sum = 0
for i in range(M):
    b_sum += B[i]
    if b_sum > K:
        b_sum -= B[i]
        j = i - 1
        break
else:
    j = M - 1
result = j + 1

a_sum = 0
for i in range(N):
    a_sum += A[i]
    if a_sum > K:
        break
    while a_sum + b_sum > K:
        b_sum -= B[j]
        j -= 1
    result = max(result, (i + 1) + (j + 1))
print(result)