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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n, m) for (int i = (n); i >= (m); --i)
using namespace std;
using LL = long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLD = vector<PLD>;

const int IINF = numeric_limits<int>::max();

int main() {

  LL n, m, k;
  cin >> n >> m >> k;
  VLL a(n + 1, 0), b(m + 1, 0);
  REP(i, n) {
    LL tmp;
    cin >> tmp;
    a[i + 1] = a[i] + tmp;
  }
  REP(i, m) {
    LL tmp;
    cin >> tmp;
    b[i + 1] = b[i] + tmp;
  }

  LL ans = 0;
  LL tmp_j = m;
  REP(i, n + 1) {
    if (a[i] > k)
      break;
    LL j = tmp_j;
    while (b[j] > k - a[i]) {
      j--;
    }
    ans = max(ans, i + j);
    j = tmp_j;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n, m) for (int i = (n); i >= (m); --i)
using namespace std;
using LL = long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLD = vector<PLD>;

const int IINF = numeric_limits<int>::max();

int main() {

  LL n, m, k;
  cin >> n >> m >> k;
  VLL a(n + 1, 0), b(m + 1, 0);
  REP(i, n) {
    LL tmp;
    cin >> tmp;
    a[i + 1] = a[i] + tmp;
  }
  REP(i, m) {
    LL tmp;
    cin >> tmp;
    b[i + 1] = b[i] + tmp;
  }

  LL ans = 0;
  LL tmp_j = m;
  REP(i, n + 1) {
    if (a[i] > k)
      break;
    LL j = tmp_j;
    while (b[j] > k - a[i]) {
      j--;
    }
    ans = max(ans, i + j);
    tmp_j = j;
  }

  cout << ans << endl;

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int N, M, K;
vector<int> A, B;

int main() {
  cin >> N >> M >> K;
  A.resize(N);
  B.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++)
    cin >> B[i];

  vector<long long int> Asum(N + 1);
  vector<long long int> Bsum(M + 1);
  Asum[0] = 0;
  for (int i = 1; i <= N; i++)
    Asum[i] = A[i - 1] + Asum[i - 1];
  Bsum[0] = 0;
  for (int i = 1; i <= M; i++)
    Bsum[i] = B[i - 1] + Bsum[i - 1];

  int res = 0;
  for (int i = 0; i <= N; i++) {
    if (Asum[i] > K) {
      break;
    }

    long long int rest = K - Asum[i];
    auto it = upper_bound(Bsum.cbegin(), Bsum.cend(), rest);
    int tmp = i + distance(Bsum.cbegin(), it) - 1;
    if (tmp > res)
      res = tmp;
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int N, M, K;
vector<int> A, B;

int main() {
  cin >> N >> 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];

  vector<long long int> Asum(N + 1);
  vector<long long int> Bsum(M + 1);
  Asum[0] = 0;
  for (int i = 1; i <= N; i++)
    Asum[i] = A[i - 1] + Asum[i - 1];
  Bsum[0] = 0;
  for (int i = 1; i <= M; i++)
    Bsum[i] = B[i - 1] + Bsum[i - 1];

  int res = 0;
  for (int i = 0; i <= N; i++) {
    if (Asum[i] > K) {
      break;
    }

    long long int rest = K - Asum[i];
    auto it = upper_bound(Bsum.cbegin(), Bsum.cend(), rest);
    int tmp = i + distance(Bsum.cbegin(), it) - 1;
    if (tmp > res)
      res = tmp;
  }
  cout << res << endl;

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

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

using namespace std;
typedef long long ll;

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

  int N, M, K;
  cin >> N >> M >> K;

  vector<int> A(N);
  vector<int> B(M);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  int AS = A.size();
  int BS = B.size();
  int ai = 0;
  int bi = 0;

  int result = 0;

  while (true) {
    if (ai >= AS)
      break;
    int a = A[ai];
    if (a > K)
      break;

    K -= a;
    result++;
    ai++;
  }

  while (true) {
    if (bi >= BS)
      break;
    int b = B[bi];
    if (b > K)
      break;

    K -= b;
    result++;
    bi++;
  }

  cerr << result << endl;

  int t = result;
  int prev = result;
  rep(i, t) {
    int tmp = prev;

    ai--;
    tmp--;
    K += A[ai];

    while (true) {
      if (bi >= BS)
        break;
      int b = B[bi];
      if (b > K)
        break;

      K -= b;
      tmp++;
      bi++;
    }

    cerr << tmp << endl;
    result = max(result, tmp);
    prev = tmp;
  }

  cout << result << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

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

using namespace std;
typedef long long ll;

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

  int N, M, K;
  cin >> N >> M >> K;

  vector<int> A(N);
  vector<int> B(M);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  int AS = A.size();
  int BS = B.size();
  int ai = 0;
  int bi = 0;

  int result = 0;

  while (true) {
    if (ai >= AS)
      break;
    int a = A[ai];
    if (a > K)
      break;

    K -= a;
    result++;
    ai++;
  }

  while (true) {
    if (bi >= BS)
      break;
    int b = B[bi];
    if (b > K)
      break;

    K -= b;
    result++;
    bi++;
  }

  cerr << result << endl;

  int t = result;
  int prev = result;
  rep(i, t) {
    int tmp = prev;

    if (ai > 0) {
      ai--;
      tmp--;
      K += A[ai];
    }

    while (true) {
      if (bi >= BS)
        break;
      int b = B[bi];
      if (b > K)
        break;

      K -= b;
      tmp++;
      bi++;
    }

    cerr << tmp << endl;
    result = max(result, tmp);
    prev = tmp;
  }

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

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  vector<ll> s(m + 1);
  rep(i, m) s[i + 1] = s[i] + a[i];
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if ((i - 1) >= 0)
      k -= a[i - 1];
    ll left = -1;
    ll right = s.size();
    while (abs(right - left) > 1) {
      ll mid = (right + left) / 2;
      if (s[mid] <= k)
        left = mid;
      else
        right = mid;
    }

    if (k >= 0)
      ans = max(ans, left + i);
  }
  cout << ans << endl;
}


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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  vector<ll> s(m + 1);
  rep(i, m) s[i + 1] = s[i] + b[i];
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if ((i - 1) >= 0)
      k -= a[i - 1];
    ll left = -1;
    ll right = s.size();
    while (abs(right - left) > 1) {
      ll mid = (right + left) / 2;
      if (s[mid] <= k)
        left = mid;
      else
        right = mid;
    }

    if (k >= 0)
      ans = max(ans, left + i);
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
// よるかつ
// 前に解説ACしたから今度は自力で
// 尺取り解法

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using ll = long long;
const int INF = 2147483647;
const ll MOD = 1000000007;
// const ll INF = 1000000000000000000LL;

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];
  // 全てaを読む
  ll t = 0;
  rep(i, n) t += a[i];
  int j = n;
  int ans = 0;
  rep(i, m + 1) {
    // 読めるところまで減らす
    while (j > 0 && t > k) {
      --j;
      t -= a[i];
    }
    // 0まで減らしても無理だったらあきらめる
    if (t > k)
      break;
    // 最大値を更新
    ans = max(ans, i + j);
    if (i == m)
      break;
    // b[i]を足す
    t += b[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// よるかつ
// 前に解説ACしたから今度は自力で
// 尺取り解法

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using ll = long long;
const int INF = 2147483647;
const ll MOD = 1000000007;
// const ll INF = 1000000000000000000LL;

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];
  // 全てaを読む
  ll t = 0;
  rep(i, n) t += a[i];
  int j = n;
  int ans = 0;
  rep(i, m + 1) {
    // 読めるところまで減らす
    while (j > 0 && t > k) {
      --j;
      t -= a[j];
    }
    // 0まで減らしても無理だったらあきらめる
    if (t > k)
      break;
    // 最大値を更新
    ans = max(ans, i + j);
    if (i == m)
      break;
    // b[i]を足す
    t += b[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // sort
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>

#define DEBUG 0

#define REP(i, n) for (long long i = 0; i < (n); i++)
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
// 999999997000000003
// 1000000000000000000

using namespace std;

int solve() {
  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];

  vector<ll> a_l_Sum(N + 1, 0);

  for (int i = 0; i < N; ++i) {
    a_l_Sum[i + 1] = a[i] + a_l_Sum[i];
  }

  vector<ll> b_l_Sum(M + 1, 0);

  for (int i = 0; i < M; ++i) {
    b_l_Sum[i + 1] = b[i] + b_l_Sum[i];
  }

  // なぜかdequeでつなげてたがそんなはずはなかった

  // a[0]～a[n-1] +
  // b[0]～b[m-1]の最善をみつける

  // a[0]とb[0]は固定
  int res = 0;
  // Aの机の本を何冊読むか
  for (int i = N; i >= 0; --i) {
    for (int j = 0; j <= M; ++j) {
      if (a_l_Sum[i] + b_l_Sum[j] <= K) {
        res = max(res, i + j);
      } else {
        break;
      }
    }
  }

  cout << res << endl;

  return 0;
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#include <algorithm> // sort
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>

#define DEBUG 0

#define REP(i, n) for (long long i = 0; i < (n); i++)
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
// 999999997000000003
// 1000000000000000000

using namespace std;

int solve() {
  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];

  vector<ll> a_l_Sum(N + 1, 0);

  for (int i = 0; i < N; ++i) {
    a_l_Sum[i + 1] = a[i] + a_l_Sum[i];
  }

  vector<ll> b_l_Sum(M + 1, 0);

  for (int i = 0; i < M; ++i) {
    b_l_Sum[i + 1] = b[i] + b_l_Sum[i];
  }

  // なぜかdequeでつなげてたがそんなはずはなかった

  // a[0]～a[n-1] +
  // b[0]～b[m-1]の最善をみつける

  // a[0]とb[0]は固定
  int res = 0;
  // Aの机の本を何冊読むか
  for (int i = 0; i <= N; ++i) {
    // 0冊のとき
    if (a_l_Sum[i] + b_l_Sum[0] <= K) {
      res = max(res, i);
    }
    int l = 0;
    int r = M + 1;
    while (l + 1 < r) {
      int mid = (l + r) / 2;
      if (a_l_Sum[i] + b_l_Sum[mid] <= K) {
        l = mid;
        res = max(res, i + mid);
      } else
        r = mid;
    }
  }

  cout << res << endl;

  return 0;
}

int main() {
  solve();
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
typedef long long int ll;
typedef long double ld;

#define MAX 1000005
#define mod 1000000007

ll add(ll a, ll b) { return (mod + (a + b) % mod) % mod; }
ll mul(ll a, ll b) { return (mod + (a * b) % mod) % mod; }

ll n, m;

unordered_map<ll, vector<ll>> mp;

int main() {

  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, m, k;
  cin >> n >> m >> k;

  vector<ll> A(n + 1), B(n + 1);

  ll sum0 = 0, sum1 = 0;

  for (ll i = 0; i < n; i++)
    cin >> A[i], sum0 += A[i];
  for (ll i = 0; i < m; i++)
    cin >> B[i];
  ll last = n - 1;
  ll ans = 0;
  for (ll i = 0; i <= m; i++) {
    while (sum0 + sum1 > k && last >= 0) {
      sum0 -= A[last];
      last--;
    }
    if (sum0 + sum1 <= k) {
      ans = max(ans, i + last + 1);
    }

    if (i != m)
      sum1 += B[i];
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
typedef long long int ll;
typedef long double ld;

#define MAX 1000005
#define mod 1000000007

ll add(ll a, ll b) { return (mod + (a + b) % mod) % mod; }
ll mul(ll a, ll b) { return (mod + (a * b) % mod) % mod; }

ll n, m;

unordered_map<ll, vector<ll>> mp;

int main() {

  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, m, k;
  cin >> n >> m >> k;

  vector<ll> A(n + 1), B(m + 1);

  ll sum0 = 0, sum1 = 0;

  for (ll i = 0; i < n; i++)
    cin >> A[i], sum0 += A[i];
  for (ll i = 0; i < m; i++)
    cin >> B[i];
  ll last = n - 1;
  ll ans = 0;
  for (ll i = 0; i <= m; i++) {
    while (sum0 + sum1 > k && last >= 0) {
      sum0 -= A[last];
      last--;
    }
    if (sum0 + sum1 <= k) {
      ans = max(ans, i + last + 1);
    }

    if (i != m)
      sum1 += B[i];
  }

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

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;

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

  int ans = 0;
  rep(i, n + 1) {
    if (a[i] > k)
      break;
    int b_r = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin();
    ans = max(ans, i + b_r);
  }
  cout << ans << endl;
  return 0;
}

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

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

  int ans = 0;
  rep(i, n + 1) {
    if (a[i] > k)
      break;
    int b_r = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin();
    ans = max(ans, i + b_r);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9
#define PI 3.14159265359
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
typedef long long ll;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// isPrime
// modpow modinv
// getDigit
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;
}
int main() {
  cout << fixed << setprecision(10);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, n) { cin >> a[i]; }
  rep(i, m) { cin >> b[i]; }
  vector<ll> c(n + 1, 0);
  vector<ll> d(m + 1, 0);
  rep(i, n) { c[i + 1] = c[i] + a[i]; }
  rep(i, m) { d[i + 1] = d[i] + a[i]; }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (c[i] > k) {
      continue;
    } else {
      int p = k - c[i];
      // cout << p << endl;
      int ok, ng;
      ok = 0;
      ng = m + 1;
      while (ng - ok > 1) {
        int mid = (ng + ok) / 2;
        if (d[mid] <= p) {
          ok = mid;
        } else {
          ng = mid;
        }
      }
      // cout << ok << endl;
      chmax(ans, i + ok);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9
#define PI 3.14159265359
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
typedef long long ll;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// isPrime
// modpow modinv
// getDigit
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;
}
int main() {
  cout << fixed << setprecision(10);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, n) { cin >> a[i]; }
  rep(i, m) { cin >> b[i]; }
  vector<ll> c(n + 1, 0);
  vector<ll> d(m + 1, 0);
  rep(i, n) { c[i + 1] = c[i] + a[i]; }
  rep(i, m) { d[i + 1] = d[i] + b[i]; }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (c[i] > k) {
      continue;
    } else {
      int p = k - c[i];
      // cout << p << endl;
      int ok, ng;
      ok = 0;
      ng = m + 1;
      while (ng - ok > 1) {
        int mid = (ng + ok) / 2;
        if (d[mid] <= p) {
          ok = mid;
        } else {
          ng = mid;
        }
      }
      // cout << ok << endl;
      chmax(ans, i + ok);
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60; // intじゃ扱えないことに注意！
using P = pair<int, int>;
#define rep(i, n)                                                              \
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  ll t = 0;
  rep(i, M) t += B[i];

  int j = M;
  int ans = 0;
  rep(i, N + 1) { // 尺取り法
    while (j > 0 && t > K) {
      j--;
      t -= B[j]; // tがKを超えないようにBを減らす
    }
    if (t > K)
      break;
    ans = max(ans, i + j);
    if (i == N)
      break;
    t += A[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60; // intじゃ扱えないことに注意！
using P = pair<int, int>;
#define rep(i, n)                                                              \
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  ll t = 0;
  rep(i, M) t += B[i];

  int j = M;
  int ans = 0;
  rep(i, N + 1) { // 尺取り法
    while (j > 0 && t > K) {
      j--;
      t -= B[j]; // tがKを超えないようにBを減らす
    }
    if (t > K)
      break;
    ans = max(ans, i + j);
    if (i == N)
      break;
    t += A[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

typedef long long int ll;

using namespace std;

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

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

  vector<ll> as(n + 1), bs(m + 1);
  for (int i = 1; i < n + 1; i++)
    as[i] = as[i - 1] + a[i - 1];
  for (int i = 1; i < m + 1; i++)
    bs[i] = bs[i - 1] + b[i - 1];

  int val = 0;
  for (int i = 0; i < n + 1; i++) {
    if (k < as[i])
      break;
    int j = 0;
    for (j = 0; j < m; j++) {
      if (bs[j + 1] > k - as[i])
        break;
    }
    val = max(val, i + j);
  }

  cout << val << endl;

  return 0;
}

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

typedef long long int ll;

using namespace std;

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

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

  vector<ll> as(n + 1), bs(m + 1);
  for (int i = 1; i < n + 1; i++)
    as[i] = as[i - 1] + a[i - 1];
  for (int i = 1; i < m + 1; i++)
    bs[i] = bs[i - 1] + b[i - 1];

  int val = 0;
  for (int i = 0; i < n + 1; i++) {
    if (k < as[i])
      break;
    int j = lower_bound(bs.begin(), bs.end(), k - as[i] + 1) - bs.begin() - 1;
    //      int j = 0;
    //      for (j = 0; j < m; j++) {
    //          if (bs[j + 1] > k - as[i]) break;
    //      }

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

  cout << val << endl;

  return 0;
}
Problem p02623 (C++)

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0), b(m + 1, 0);
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    a.at(i) = a.at(i - 1) + c;
  }
  for (int i = 1; i <= m; i++) {
    int d;
    cin >> d;
    b.at(i) = b.at(i - 1) + d;
  }

  int jbest = 0;
  bool judge = true;
  while (judge) {
    if (k >= b.at(jbest + 1)) {
      jbest++;
    } else {
      judge = false;
    }
    if (jbest == m) {
      judge = false;
    }
  }
  // cout << jbest << endl;

  int ans = jbest;
  for (int i = 1; i <= n; i++) {
    if (a.at(i) > k) {
      break;
    }
    int j = jbest;
    while (b.at(j) > k - a.at(i)) {
      j--;
    }
    ans = max(ans, i + j);
  }

  /*for(int j = jbest; j >= 0; j--) {
    for(int i = 1; i <= n; i++) {
      if(a.at(i) <= k - b.at(j) && ans < i + j) {
        ans = i + j;
      }
      if(a.at(i) > k - b.at(j)) {
        break;
      }
    }
  }*/

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

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0), b(m + 1, 0);
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    a.at(i) = a.at(i - 1) + c;
  }
  for (int i = 1; i <= m; i++) {
    int d;
    cin >> d;
    b.at(i) = b.at(i - 1) + d;
  }

  int jbest = 0;
  bool judge = true;
  while (judge) {
    if (k >= b.at(jbest + 1)) {
      jbest++;
    } else {
      judge = false;
    }
    if (jbest == m) {
      judge = false;
    }
  }
  // cout << jbest << endl;

  int ans = jbest;
  for (int i = 1; i <= n; i++) {
    if (a.at(i) > k) {
      break;
    }
    int j = jbest;
    while (b.at(j) > k - a.at(i)) {
      j--;
    }
    ans = max(ans, i + j);
    jbest = j;
  }

  /*for(int j = jbest; j >= 0; j--) {
    for(int i = 1; i <= n; i++) {
      if(a.at(i) <= k - b.at(j) && ans < i + j) {
        ans = i + j;
      }
      if(a.at(i) > k - b.at(j)) {
        break;
      }
    }
  }*/

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

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

using namespace std;

int main() {
  int n, m;
  unsigned long long k;
  unsigned long long a[200001], b[200001];

  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  int mm = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = mm - i; j <= m; j++) {
      if (a[i] + b[j] <= k) {
        mm = max(mm, i + j);
      } else {
        continue;
      }
    }
  }
  cout << mm << endl;
  return 0;
}

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

using namespace std;

int main() {
  int n, m;
  unsigned long long k;
  unsigned long long a[200001], b[200001];

  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  int mm = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = mm - i; j <= m; j++) {
      if (a[i] + b[j] <= k) {
        mm = max(mm, i + j);
      } else {
        break;
      }
    }
  }
  cout << mm << endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  long long int x;
  long long int a[n + 1], b[n + 1];
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  for (int i = 1; i <= m; i++) {
    cin >> x;
    b[i] = b[i - 1] + x;
  }
  long long int ans = 0;
  int j = m;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k) {
      break;
    }
    while (b[j] > k - a[i]) {
      j--;
    }
    ans = max(ans, (long long int)i + j);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  long long int x;
  long long int a[n + 1], b[m + 1];
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  for (int i = 1; i <= m; i++) {
    cin >> x;
    b[i] = b[i - 1] + x;
  }
  long long int ans = 0;
  int j = m;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k) {
      break;
    }
    while (b[j] > k - a[i]) {
      j--;
    }
    ans = max(ans, (long long int)i + j);
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++)

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

using namespace std;

using vi = vector<int>;

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

  int n, m, k;
  cin >> n >> m >> k;
  vi psa(n + 1), psb(m + 1);
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    psa.at(i) = min(k + 1, psa.at(i - 1) + a);
  }
  for (int i = 1; i <= m; i++) {
    int b;
    cin >> b;
    psb.at(i) = min(k + 1, psb.at(i - 1) + b);
  }

  int ans = 0;
  for (int i = upper_bound(psa.begin(), psa.end(), k) - psa.begin() - 1, j = 0;
       i >= 0; i--) {
    while (psa.at(i) + psb.at(j) <= k) {
      j++;
    }
    ans = max(ans, i + j - 1);
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;

using vi = vector<int>;

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

  int n, m, k;
  cin >> n >> m >> k;
  vi psa(n + 1), psb(m + 1);
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    psa.at(i) = min(k + 1, psa.at(i - 1) + a);
  }
  for (int i = 1; i <= m; i++) {
    int b;
    cin >> b;
    psb.at(i) = min(k + 1, psb.at(i - 1) + b);
  }

  int ans = 0;
  for (int i = upper_bound(psa.begin(), psa.end(), k) - psa.begin() - 1, j = 0;
       i >= 0; i--) {
    while (j <= m && psa.at(i) + psb.at(j) <= k) {
      j++;
    }
    ans = max(ans, i + j - 1);
  }

  cout << ans << endl;

  return 0;
}
Problem p02623 (C++)

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

using namespace std;

bool is_digit(char c) { return c >= '0' && c <= '9'; }

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<long long> sa(n + 1), sb(m + 1);
  sa[0] = 0;
  for (int i = 0; i < n; i++) {
    sa[i + 1] = a[i] + sa[i];
  }
  sb[0] = 0;
  for (int i = 0; i < m; i++) {
    sb[i + 1] = a[i] + sb[i];
  }
  int res = 0;
  for (int i = 0; i <= n; i++) {
    if (sa[i] > k)
      break;
    int l = 0, r = m + 1;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (sa[i] + sb[mid] > k) {
        r = mid;
      } else {
        l = mid;
      }
    }
    res = max(res, i + l);
  }
  cout << res;
  return 0;
}

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

using namespace std;

bool is_digit(char c) { return c >= '0' && c <= '9'; }

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<long long> sa(n + 1), sb(m + 1);
  sa[0] = 0;
  for (int i = 0; i < n; i++) {
    sa[i + 1] = a[i] + sa[i];
  }
  sb[0] = 0;
  for (int i = 0; i < m; i++) {
    sb[i + 1] = b[i] + sb[i];
  }
  int res = 0;
  for (int i = 0; i <= n; i++) {
    if (sa[i] > k)
      break;
    int l = 0, r = m + 1;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (sa[i] + sb[mid] > k) {
        r = mid;
      } else {
        l = mid;
      }
    }
    res = max(res, i + l);
  }
  cout << res;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF 1234567890
#define pii pair<LL, LL>
#define LL long long
using namespace std;

int main() {
  // clock_t start = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  LL n, m, k;
  cin >> n >> m >> k;
  LL a[n + 5], b[n + 5];
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  for (LL i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (LL i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }

  LL ansreal = 0;
  for (LL i = 0; i <= n; i++) {
    LL x = i;
    LL rem = k - a[x];
    if (rem < 0)
      continue;
    LL l = 1, r = m, ans = 0;
    while (l <= r) {
      LL mid = (l + r) / 2;
      if (b[mid] <= rem) {
        ans = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    ansreal = max(ansreal, i + ans);
  }
  cout << ansreal << endl;
  // cerr << fixed << setprecision(3) << (clock()-start)*1./CLOCKS_PER_SEC <<
  // endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF 1234567890
#define pii pair<LL, LL>
#define LL long long
using namespace std;

int main() {
  // clock_t start = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  LL n, m, k;
  cin >> n >> m >> k;
  LL a[n + 5], b[m + 5];
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  for (LL i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (LL i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }

  LL ansreal = 0;
  for (LL i = 0; i <= n; i++) {
    LL x = i;
    LL rem = k - a[x];
    if (rem < 0)
      continue;
    LL l = 1, r = m, ans = 0;
    while (l <= r) {
      LL mid = (l + r) / 2;
      if (b[mid] <= rem) {
        ans = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    ansreal = max(ansreal, i + ans);
  }
  cout << ansreal << endl;
  // cerr << fixed << setprecision(3) << (clock()-start)*1./CLOCKS_PER_SEC <<
  // endl;
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define MP make_pair
#define EACH(i, c) for (auto i : c)
#define SORT(c) sort((c).begin(), (c).end())

#define ALL(a) (a).begin(), (a).end()

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

  LL N, M, K;
  cin >> N >> M >> K;

  vector<LL> A(N), B(N);
  REP(i, N) cin >> A[i];
  REP(i, M) cin >> B[i];

  LL a = 0, b = 0;
  REP(j, M) b += B[j];
  LL j = M;
  LL ret = 0;
  REP(i, N + 1) {
    if (0 < i)
      a += A[i - 1];
    while (0 < j && K < a + b) {
      b -= B[j - 1];
      j--;
    }
    if (a + b <= K)
      ret = max(i + j, ret);
  }

  cout << ret << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define MP make_pair
#define EACH(i, c) for (auto i : c)
#define SORT(c) sort((c).begin(), (c).end())

#define ALL(a) (a).begin(), (a).end()

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

  LL N, M, K;
  cin >> N >> M >> K;

  vector<LL> A(N), B(M);
  REP(i, N) cin >> A[i];
  REP(i, M) cin >> B[i];

  LL a = 0, b = 0;
  REP(j, M) b += B[j];
  LL j = M;
  LL ret = 0;
  REP(i, N + 1) {
    if (0 < i)
      a += A[i - 1];
    while (0 < j && K < a + b) {
      b -= B[j - 1];
      j--;
    }
    if (a + b <= K)
      ret = max(i + j, ret);
  }

  cout << ret << endl;

  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 1000000000
using namespace std;

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

  vector<long long> a(n), b(m);
  long long i, j;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < m; i++)
    cin >> b[i];

  vector<long long> aa(n + 1), bb(m + 1);

  aa[i] = 0;
  bb[i] = 0;
  for (i = 1; i <= n; i++) {
    aa[i] = aa[i - 1] + a[i - 1];
  }
  for (j = 1; j <= m; j++) {
    bb[j] = bb[j - 1] + b[j - 1];
  }

  long long cnt = 0;

  for (i = 0; i < n + 1; i++) {
    if (aa[i] > k)
      break;
    long long tmp = 0;
    tmp += i;
    j = lower_bound(bb.begin(), bb.end(), k - aa[i]) - bb.begin();
    if (bb[j] == k - aa[i])
      tmp += j;
    else
      tmp += (j - 1);
    cnt = max(cnt, tmp);
  }

  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 1000000000
using namespace std;

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

  vector<long long> a(n), b(m);
  long long i, j;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < m; i++)
    cin >> b[i];

  vector<long long> aa(n + 1), bb(m + 1);

  aa[0] = 0;
  bb[0] = 0;
  for (i = 1; i <= n; i++) {
    aa[i] = aa[i - 1] + a[i - 1];
  }
  for (j = 1; j <= m; j++) {
    bb[j] = bb[j - 1] + b[j - 1];
  }

  long long cnt = 0;

  for (i = 0; i < n + 1; i++) {
    if (aa[i] > k)
      break;
    long long tmp = 0;
    tmp += i;
    j = lower_bound(bb.begin(), bb.end(), k - aa[i]) - bb.begin();
    if (bb[j] == k - aa[i])
      tmp += j;
    else
      tmp += (j - 1);
    cnt = max(cnt, tmp);
  }

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

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

using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<int> a(n + 1, 0);
  vector<int> b(m + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i];
  vector<ll> sum(m + 1, 0);
  for (int i = 1; i <= m; i++)
    sum[i] = sum[i - 1] + a[i];
  int x = m;
  int res = 0;
  ll now = 0;
  for (int i = 0; i <= n; i++) {
    now += a[i];
    if (now > k)
      break;
    while (now + sum[x] > k)
      x--;
    res = max(res, i + x);
  }
  cout << res << "\n";
  return 0;
}


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

using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<int> a(n + 1, 0);
  vector<int> b(m + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i];
  vector<ll> sum(m + 1, 0);
  for (int i = 1; i <= m; i++)
    sum[i] = sum[i - 1] + b[i];
  int x = m;
  int res = 0;
  ll now = 0;
  for (int i = 0; i <= n; i++) {
    now += a[i];
    if (now > k)
      break;
    while (now + sum[x] > k)
      x--;
    res = max(res, i + x);
  }
  cout << res << "\n";
  return 0;
}
Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
// mt19937 mrand(random_device{}());
const ll mod = 1000000007;
// int rnd(int x) { return mrand() % x;}
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
const int maxx = 1e6 + 10;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
// mt19937 mrand(random_device{}());
const ll mod = 1000000007;
// int rnd(int x) { return mrand() % x;}
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
const int maxx = 1e6 + 10;

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;

int bs(vector<ll> b, ll tar) {
  int start = 0, end = b.size() - 1;
  int ans = -1;
  while (start <= end) {
    int mid = start + (end - start) / 2;
    if (b[mid] <= tar) {
      ans = mid;
      start = mid + 1;
    } else
      end = mid - 1;
  }
  return ans;
}

int solve(vector<ll> a, vector<ll> b, ll k) {
  int res = 0;
  ll left;
  for (int i = 0; i < a.size(); ++i) {
    left = k - a[i];
    if (left < 0)
      break;
    int index = bs(b, left);
    if (index == -1)
      break;
    res = max(res, i + index);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  ll k, now;
  cin >> n >> m;
  cin >> k;
  vector<ll> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  rep(i, n) {
    cin >> now;
    a[i + 1] = now + a[i];
  }
  rep(i, m) {
    cin >> now;
    b[i + 1] = now + b[i];
  }
  int res = max(solve(a, b, k), solve(b, a, k));
  cout << res << endl;
  return 0;
}

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

int bs(vector<ll> b, ll tar) {
  int start = 0, end = b.size() - 1;
  int ans = -1;
  while (start <= end) {
    int mid = start + (end - start) / 2;
    if (b[mid] <= tar) {
      ans = mid;
      start = mid + 1;
    } else
      end = mid - 1;
  }
  return ans;
}

int solve(vector<ll> a, vector<ll> b, ll k) {
  int res = 0;
  ll left;
  for (int i = 0; i < a.size(); ++i) {
    left = k - a[i];
    if (left < 0)
      break;
    int index = bs(b, left);
    if (index == -1)
      break;
    res = max(res, i + index);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  ll k, now;
  cin >> n >> m;
  cin >> k;
  vector<ll> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  rep(i, n) {
    cin >> now;
    a[i + 1] = now + a[i];
  }
  rep(i, m) {
    cin >> now;
    b[i + 1] = now + b[i];
  }
  ll ans = 0;
  rep(i, n + 1) {
    ll cur = k - a[i];
    if (cur >= 0) {
      ll cur2 = upper_bound(b.begin(), b.end(), cur) - b.begin() - 1;
      ans = max(ans, i + cur2);
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

typedef long long ll;

int main(int argc, char *argv[]) {
  ll n, m, k;
  std::vector<ll> a, b;

  // 入力
  std::cin >> n >> m >> k;

  a.resize(n);
  b.resize(m);

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

  // pre
  std::vector<ll> aSum(n + 1), bSum(m + 1);

  aSum[0] = 0;
  bSum[0] = 0;

  for (int i = 0; i < n; i++) {
    aSum[i + 1] = aSum[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    bSum[i + 1] = bSum[i] + a[i];
  }

  int aPtr, bPtr;
  for (aPtr = 0; aPtr < n; aPtr++) {
    if (aSum[aPtr + 1] > k) {
      break;
    }
  }
  for (bPtr = 0; bPtr < m; bPtr++) {
    if (aSum[aPtr] + bSum[bPtr + 1] > k) {
      break;
    }
  }

  // solve
  int max = aPtr + bPtr;
  for (; aPtr >= 0; aPtr--) {
    for (; bPtr < m; bPtr++) {
      if (aSum[aPtr] + bSum[bPtr + 1] > k) {
        break;
      }
    }
    max = std::max(max, aPtr + bPtr);
  }

  std::cout << max << std::endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

typedef long long ll;

int main(int argc, char *argv[]) {
  ll n, m, k;
  std::vector<ll> a, b;

  // 入力
  std::cin >> n >> m >> k;

  a.resize(n);
  b.resize(m);

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

  // pre
  std::vector<ll> aSum(n + 1), bSum(m + 1);

  aSum[0] = 0;
  bSum[0] = 0;

  for (int i = 0; i < n; i++) {
    aSum[i + 1] = aSum[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    bSum[i + 1] = bSum[i] + b[i];
  }

  int aPtr, bPtr;
  for (aPtr = 0; aPtr < n; aPtr++) {
    if (aSum[aPtr + 1] > k) {
      break;
    }
  }
  for (bPtr = 0; bPtr < m; bPtr++) {
    if (aSum[aPtr] + bSum[bPtr + 1] > k) {
      break;
    }
  }

  // solve
  int max = aPtr + bPtr;
  for (; aPtr >= 0; aPtr--) {
    for (; bPtr < m; bPtr++) {
      if (aSum[aPtr] + bSum[bPtr + 1] > k) {
        break;
      }
    }
    max = std::max(max, aPtr + bPtr);
  }

  std::cout << max << std::endl;

  return 0;
}
Problem p02623 (C++)

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

using namespace std;

#define debug(x)                                                               \
  cerr << endl << "DEBUG: (" << (#x) << " = " << x << ")" << endl;
#define debugArray(v)                                                          \
  for (auto i : v)                                                             \
    cerr << i << " ";                                                          \
  cerr << endl;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const long long INFLL = 0x3f3f3f3f3f3f3f3fLL;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);

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

  long long n, m, k;

  cin >> n >> m >> k;

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

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

  int ans = 0;

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

  cout << ans << endl;

  return 0;
}

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

using namespace std;

#define debug(x)                                                               \
  cerr << endl << "DEBUG: (" << (#x) << " = " << x << ")" << endl;
#define debugArray(v)                                                          \
  for (auto i : v)                                                             \
    cerr << i << " ";                                                          \
  cerr << endl;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const long long INFLL = 0x3f3f3f3f3f3f3f3fLL;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);

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

  long long n, m, k;

  cin >> n >> m >> k;

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

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

  int ans = 0;

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

  cout << ans << endl;

  return 0;
}