alexjercan/bugnet huggingface dataset:
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> a(n), b(m), aa(n + 1, 0), bb(m + 1, 0);

  long long suma = 0, sumb = 0;

  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    aa[i + 1] = aa[i] + a[i];
    suma += a[i];
  }
  for (long long i = 0; i < m; i++) {
    cin >> b[i];
    bb[i + 1] = bb[i] + b[i];
    sumb += b[i];
  }

  long long ans = 0, ia = 0, ib = 0;
  if (sumb + suma <= k) {
    ans = n + m;
  } else {

    for (ia = 0; ia <= n; ia++) {
      if (aa[ia] > k)
        break;
      ib = upper_bound(bb.begin(), bb.end(), k - aa[ia]) - bb.begin();
      if (ib > 0)
        ib--;
      if (ans < ia + ib)
        ans = ia + ib;
    }
  }

  cout << ans << endl;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

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

  long long ans = 0;
  long long itera = 0, iterb = 0;
  while (1) {
    if (itera < n && iterb < m) {
      if (a[itera] < b[iterb]) {
        k -= a[itera];
        if (k < 0)
          break;
        itera++;
        ans++;
      } else if (a[itera] > b[iterb]) {
        k -= b[iterb];
        if (k < 0)
          break;
        iterb++;
        ans++;
      } else {
        long long ia = itera, ib = iterb;
        while (1) {
          if (a[ia] != b[ib] || ia >= n || ib >= m)
            break;
          ia++;
          ib++;
        }
        if (ia >= m || ib >= n || a[ia] < b[ib]) {
          k -= a[itera];
          if (k < 0)
            break;
          itera++;
          ans++;
        } else {
          k -= b[iterb];
          if (k < 0)
            break;
          iterb++;
          ans++;
        }
      }
    } else if (itera >= n && iterb < m) {
      k -= b[iterb];
      if (k < 0)
        break;
      iterb++;
      ans++;
    } else if (iterb >= m && itera < n) {
      k -= a[itera];
      if (k < 0)
        break;
      itera++;
      ans++;
    } else {
      break;
    }
  }

  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <numeric>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, n) for (int i = (a); i < (n); ++i)
using namespace std;
using ll = long long;
using uint = unsigned int;
const ll mod = (ll)1e9 + 7;

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

  int ai = n;
  ll t = asum;
  while (ai >= 1 && t > k)
    t -= a[--ai];
  int ans = ai;

  ai = 0;
  int bi = m;
  t = bsum;
  while (true) {
    while (bi >= 1 && t > k)
      t -= b[--bi];
    if (bi == 0)
      break;
    ans = max(ans, ai + bi);
    if (ai == n)
      break;
    t += a[ai++];
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <numeric>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, n) for (int i = (a); i < (n); ++i)
using namespace std;
using ll = long long;
using uint = unsigned int;
const ll mod = (ll)1e9 + 7;

int ans = 0;
int n, m, k;
vector<int> a, b;
void dfs(int ai, int bi) {
  if (k <= 0 || (ai == n && bi == m)) {
    int bnum = ai + bi;
    if (k < 0)
      bnum--;
    if (bnum > ans)
      ans = bnum;
    return;
  }
  if (ai < n) {
    k -= a[ai];
    dfs(ai + 1, bi);
    k += a[ai];
  }
  if (bi < m) {
    k -= b[bi];
    dfs(ai, bi + 1);
    k += b[bi];
  }
}

int main() {
  cin >> n >> m >> k;
  a.resize(n), b.resize(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  dfs(0, 0);
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  long int N, M, K, count = 0, t = 0, A, B, temp = 0;
  vector<long int> a = {0}, b = {0};
  cin >> N >> M >> K;

  for (int i = 0; i < N; i++) {
    cin >> A;
    a.push_back(a[i] + A);
  }
  for (int j = 0; j < M; j++) {
    cin >> B;
    b.push_back(b[j] + B);
  }
  vector<long int>::iterator it1 = upper_bound(a.begin(), a.end(), K);
  A = it1 - a.begin() - 1;
  count = A;

  for (int k = 0; k < A + 1; k++) {
    vector<long int>::iterator it2 = upper_bound(b.begin(), b.end(), K - a[k]);
    B = it2 - b.begin() - 1;
    if (count < k + B)
      count = k + B;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  long int N, M, K, a[200001], b[200001], count = 0, t = 0, A, B, temp;
  cin >> N >> M >> K;
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < N; i++) {
    cin >> A;
    a[i + 1] = a[i] + A;
  }
  for (int j = 0; j < M; j++) {
    cin >> B;
    b[j + 1] = b[j] + B;
  }
  for (int k = 0; k < N + 1; k++) {
    if (a[k] > K) {
      count = k - 1;
      temp = k - 1;
      break;
    } else if (k == N) {
      count = N;
      temp = N;
    }
  }
  for (int l = 0; l <= temp; l++) {
    int c = l;
    for (int m = 0; m < M + 1; m++) {
      if (b[m] > K - a[l]) {
        c += m - 1;
        break;
      } else if (m == M) {
        c += m;
      }
    }
    if (count < c)
      count = c;
  }

  cout << count << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: 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] + 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;
}


Error: 0

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] + 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;
}
Problem p02623 (C++) — buggy submission, verdict: 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[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;
}


Error: WA

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[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;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

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

int main() {
  int t, n, m;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cin >> n >> m;
    long long a[n + 1], v = 0;
    long long b[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      a[i] %= m;
    }
    a[0] = -1;
    sort(a + 1, a + n + 1);

    memset(b, 0LL, sizeof(b));
    for (int i = 1; i <= n; i++) {
      if (a[i]) {
        if (a[i] == a[i - 1])
          b[i] = b[i - 1] + m;
        else
          b[i] = m - a[i];
      }
      v = max(b[i], v);
    }
    if (v == 0)
      cout << 0 << endl;
    else
      cout << v + 1 << endl;
  }
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
using namespace std;
using ll = long long;
using d = double;

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

  ll T = 0;
  rep(i, M) T += B[i];
  int C = M;
  int ans = 0;
  rep(i, N + 1) {
    while (T > K && !C == 0) {
      C--;
      T -= B[C];
    }
    if (T > K)
      break;
    ans = max(ans, i + C);
    if (i == N)
      break;
    T += A[i];
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
using namespace std;
using ll = long long;
using d = double;

int main() {
  int N, M;
  ll K;
  cin >> N >> M >> K;
  deque<ll> A;
  deque<ll> B;
  rep(i, N) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  rep(i, M) {
    ll b;
    cin >> b;
    B.push_back(b);
  }
  ll C = 0;
  rep(i, N + M) {
    if (A.empty()) {
      if (B.empty() || *B.begin() > K)
        break;
    }
    if (B.empty()) {
      if (A.empty() || *A.begin() > K)
        break;
    }
    if (*A.begin() > K && *B.begin() > K)
      break;

    if (*A.begin() < *B.begin() && *A.begin() <= K) {
      K -= *A.begin();
      A.erase(A.begin());
      C++;
    } else if (*A.begin() > *B.begin() && *B.begin() <= K) {
      K -= *B.begin();
      B.erase(B.begin());
      C++;
    } else {
      if (A.size() > 1 && B.size() > 1) {
        if ((*A.begin() + 1) < *(B.begin() + 1)) {
          K -= *A.begin();
          A.erase(A.begin());
          C++;
        } else {
          K -= *B.begin();
          B.erase(B.begin());
          C++;
        }
      } else if (i == N) {
        K -= *B.begin();
        B.erase(B.begin());
        C++;
      } else if (i == M) {
        K -= *A.begin();
        A.erase(A.begin());
        C++;
      }
    }
  }
  cout << C << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

//                _
//  ___ _ __ ___ (_) __ ___      ___   _ _ __ ___  _ __
// / __| '_ ` _ \\| |/ _` \\ \\ /\\ / / | | | '_ ` _ \\| '_ \\ 
// \\__ \\ | | | | | | (_| |\\ V  V /| |_| | | | | | | |_) |
// |___/_| |_| |_|_|\\__, | \\_/\\_/  \\__,_|_| |_| |_| .__/
//                  |___/                         |_|
// code by smigwump
// https://codeforces.com/blog/entry/62393
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long ll;
#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define PER(i, a, b) for (ll i = a; i >= b; i--)

using namespace std;

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0), b(m, 0);
  ll asum = 0, bsum = 0;
  REP(i, 0, n - 1) {
    cin >> a[i];
    asum += a[i];
  }
  REP(i, 0, m - 1) { cin >> b[i]; }
  ll ans = 0;
  int j = 0;
  PER(i, n, 0) {
    asum -= a[i];
    if (asum <= k) {
      while (j < m && asum + bsum + b[j] <= k) {
        bsum += b[j];
        j++;
      }
      ans = max(ans, i + j);
    }
  }
  cout << ans << "\\n";

  return 0;
}

Error: 0

Fixed submission:

//                _
//  ___ _ __ ___ (_) __ ___      ___   _ _ __ ___  _ __
// / __| '_ ` _ \\| |/ _` \\ \\ /\\ / / | | | '_ ` _ \\| '_ \\ 
// \\__ \\ | | | | | | (_| |\\ V  V /| |_| | | | | | | |_) |
// |___/_| |_| |_|_|\\__, | \\_/\\_/  \\__,_|_| |_| |_| .__/
//                  |___/                         |_|
// code by smigwump
// https://codeforces.com/blog/entry/62393
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define PER(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;

using namespace std;

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  deque<ll> a(n, 0), b(m, 0);
  REP(i, 0, n - 1) { cin >> a[i]; }
  REP(i, 0, m - 1) { cin >> b[i]; }

  ll ans = 0;
  ll time = 0;
  while (time < k) {
    if (a.front() < b.front()) {
      time += a.front();
      if (time > k)
        break;
      // cout<<a.front()<<"\\n";
      a.pop_front();
      ans++;
    } else {
      time += b.front();
      if (time > k)
        break;
      // cout<<b.front()<<"\\n";
      b.pop_front();
      ans++;
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  int M;
  int K;
  int k_max;
  int p_max;
  int ai;
  int bi;

  cin >> N >> M >> K;

  vector<int> A(N); // N個のデータ
  vector<int> B(M); // M個のデータ

  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }
  k_max = 0;
  for (int i = 0; i < N; i++) {
    k_max += A.at(i);
    ai = i + 1;
    if (k_max > K) {
      ai--;
      k_max -= A.at(i);
      break;
    }
  }
  p_max = ai;
  for (int i = 0; i < M; i++) {
    k_max += B.at(i);
    bi = i + 1;
    if (k_max > K) {
      while (k_max > K) {
        if (ai == 0) {
          bi--;
          if (bi > p_max)
            p_max = bi;
          break;
        }
        ai--;
        k_max -= A.at(ai);
      }
      if (k_max > K)
        break;
    }
    if (k_max <= K) {
      if (ai + bi > p_max) {
        p_max = ai + bi;
      }
      continue;
    }
  }
  if ((k_max <= K) && (ai == N)) {
    cout << N + M << endl;
    return 0;
  }
  cout << p_max << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

  cin >> N >> M >> K;

  vector<long long> A(N + 1); // N+1個のデータ
  vector<long long> B(M + 1); // M+1個のデータ

  for (int i = 1; i < N + 1; i++)
    cin >> A.at(i);
  for (int i = 1; i < M + 1; i++)
    cin >> B.at(i);
  for (int i = 1; i < N + 1; i++)
    A.at(i) += A.at(i - 1);
  for (int i = 1; i < M + 1; i++)
    B.at(i) += B.at(i - 1);
  p_max = 0;
  if (A.at(N) <= K)
    p_max = max(N, p_max);
  if (B.at(M) <= K)
    p_max = max(M, p_max);
  for (int i = 0; i < N + 1; i++) {
    if (A.at(i) > K)
      break;
    for (int j = 0; j < M + 1; j++) {
      if (A.at(i) + B.at(j) > K)
        break;
      p_max = max(p_max, i + j);
    }
  }
  cout << p_max << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<int> A(n + 1), B(m + 1);
  vector<ll> CA(n + 1), CB(m + 1);
  rep(i, n + 1) {
    if (i == 0)
      continue;
    cin >> A[i];
    CA[i] = A[i] + CA[i - 1];
  }
  rep(i, m + 1) {
    if (i == 0)
      continue;
    cin >> B[i];
    CB[i] = B[i] + CB[i - 1];
  }
  int ans = 0, ai = 0;
  while (ai <= n && CA[ai] <= k) {
    int l = -1, r = m + 1;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (CA[ai] + CB[mid] <= k) {
        l = mid;
      } else {
        r = mid;
      }
    }
    ans = max(ai + l, ans);
    ai++;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<int> A(n + 1), B(m + 1);
  vector<ll> CA(n + 1), CB(m + 1);
  rep(i, n + 1) {
    if (i == 0)
      continue;
    cin >> A[i];
    CA[i] = A[i] + CA[i - 1];
  }
  rep(i, m + 1) {
    if (i == 0)
      continue;
    cin >> B[i];
    CB[i] = B[i] + CB[i - 1];
  }
  int ans = 0, ai = 0;
  while (ai < m && k >= CA[ai]) {
    int l = -1, r = m + 1;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (CA[ai] + CB[mid] > k) {
        r = mid;
      } else {
        l = mid;
      }
    }
    ans = max(ai + l, ans);
    ai++;
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  long long int a, b;
  long long int A[n];
  long long int B[m];
  for (int i = 0; i < n; i++) {
    cin >> a;
    A[i] = a;
  }
  long long int Sa[n + 1];
  Sa[0] = 0;
  for (int i = 0; i < n; i++) {
    Sa[i + 1] = Sa[i] + A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b;
    B[i] = b;
  }
  long long int Sb[m + 1];
  Sb[0] = 0;
  for (int i = 0; i < m; i++) {
    Sb[i + 1] = Sb[i] + B[i];
  }
  int ans = 0, j = m;
  for (int i = 0; i <= n; i++) {
    if (Sa[i] > k) {
      break;
    }
    while (Sb[j] > k - Sa[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  long long int a, b;
  long long int A[n];
  long long int B[m];
  for (int i = 0; i < n; i++) {
    cin >> a;
    A[i] = a;
  }
  long long int Sa[n + 1];
  Sa[0] = 0;
  for (int i = 0; i < n; i++) {
    Sa[i + 1] = Sa[i] + A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b;
    B[i] = b;
  }
  long long int Sb[m + 1];
  Sb[0] = 0;
  for (int i = 0; i < m; i++) {
    Sb[i + 1] = Sb[i] + B[i];
  }
  int ans = 0, j = m + 1;
  for (int i = 0; i <= n; i++) {
    if (Sb[j] > k - Sa[i]) {
      while (Sb[j] > k - Sa[i]) {
        j--;
      }
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: 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 = 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;
}


Error: TLE

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 = 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;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> V;
typedef pair<int, int> P;
typedef vector<vector<int>> Graph;

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

    if (t > k)
      break;

    ans = max(ans, i + j);
    if (i == n)
      break;
    t += a[i];
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> V;
typedef pair<int, int> P;
typedef vector<vector<int>> Graph;

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