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

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < n; i++)
#define cint(a)                                                                \\
  int a;                                                                       \\
  cin >> a;
#define cstr(str)                                                              \\
  string str;                                                                  \\
  cin >> str;
#define vv(Type, n, m, d) vector<vector<Type>> vv(n, vector<Type>(m, d));

using namespace std;
using ll = long long;

const ll mod = 1e9 + 7;

int main(void) {

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

  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 as = 0, bs = 0;
  ll cnt = 0;
  ll ans = 0;
  rep(i, m) {
    bs += b[i];
    if (bs <= k)
      ans = i + 1;
  }

  rep(i, n) {
    as += a[i];
    if (as > k)
      break;
    cnt = i + 1;
    while (true) {
      if (m == 0)
        break;
      if (as + bs > k) {
        m--;
        bs -= b[m];
      } else
        break;
    }
    cnt += m;
    ans = max(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"

#define rep(i, n) for (ll i = 0; i < n; i++)
#define cint(a)                                                                \\
  int a;                                                                       \\
  cin >> a;
#define cstr(str)                                                              \\
  string str;                                                                  \\
  cin >> str;
#define vv(Type, n, m, d) vector<vector<Type>> vv(n, vector<Type>(m, d));

using namespace std;
using ll = long long;

const ll mod = 1e9 + 7;

int main(void) {

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

  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 as = 0, bs = 0;
  ll cnt = 0;
  ll ans = 0;
  rep(i, m) bs += b[i];
  ll c = bs;
  ans = m;
  while (c < k) {
    ans--;
    c -= b[ans];
  }

  rep(i, n) {
    as += a[i];
    if (as > k) {
      ans = max(ans, i);
      break;
    }
    while (m) {
      if (as + bs > k) {
        m--;
        bs -= b[m];
      } else
        break;
    }
    ans = max(ans, m + i + 1);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N + 1);
  vector<long long> B(M + 1);
  A[0] = 0;
  B[0] = 0;
  long long prev = 0;
  for (long long i = 1; i <= N; ++i) {
    long long t;
    cin >> t;
    A[i] = t + A[i - 1];
  }
  for (long long i = 1; i <= M; ++i) {
    long long t;
    cin >> t;
    B[i] = t + B[i - 1];
  }
  long long cnt = 0;
  long long j = M;
  for (long long i = 0; i <= N; ++i) {
    if (A[i] > K) {
      break;
    }
    while (j >= 0 && A[i] + B[j] > K) {
      j--;
    }
    cnt = max(cnt, i + j);
  }
  cout << cnt << endl;

  return 0;
}


Error: TLE

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 + 1);
  vector<long long> B(M + 1);
  A[0] = 0;
  B[0] = 0;
  long long prev = 0;
  for (long long i = 1; i <= N; ++i) {
    long long t;
    cin >> t;
    A[i] = t + A[i - 1];
  }
  for (long long i = 1; i <= M; ++i) {
    long long t;
    cin >> t;
    B[i] = t + B[i - 1];
  }
  long long cnt = 0;
  for (long long i = 0; i <= N; ++i) {
    if (A[i] > K) {
      break;
    }
    for (long long j = 0; j <= M; ++j) {
      if (A[i] + B[j] > K) {
        break;
      }
      cnt = max(i + j, cnt);
    }
  }
  cout << cnt << endl;

  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int ll
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\\n"
typedef long long ll;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int k;
  cin >> k;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int b[m];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    arr[i] += arr[i - 1];
  }
  for (int i = 1; i < m; i++) {
    b[i] += b[i - 1];
  }
  ans = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] <= k) {
      int cnt = i + 1;
      int val = k - arr[i];
      int pos = upper_bound(b, b + m, val) - b;
      cnt += pos;
      ans = max(ans, cnt);
    }
  }
  for (int i = 0; i < m; i++) {
    if (b[i] <= k) {
      int cnt = i + 1;
      int val = k - b[i];
      int pos = upper_bound(arr, arr + n, val) - arr;
      cnt += pos;
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int ll
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\\n"
typedef long long ll;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int k;
  cin >> k;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int b[n];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    arr[i] += arr[i - 1];
  }
  for (int i = 1; i < m; i++) {
    b[i] += b[i - 1];
  }
  ans = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] <= k) {
      int cnt = i + 1;
      int val = k - arr[i];
      int pos = upper_bound(b, b + m, val) - b;
      cnt += pos;
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  a[0] = 0;
  b[0] = 0;
  rep1(i, n) {
    ll p;
    cin >> p;
    a[i] = a[i - 1] + p;
  }
  rep1(i, m) {
    ll q;
    cin >> q;
    b[i] = b[i - 1] + q;
  }
  int ans = 0;
  int v = m + 1;
  rep(i, n + 1) {
    ll judge = k - a[i];
    if (judge < 0)
      break;
    bool decide = false;
    per(j, v) {
      if (judge >= b[j]) {
        v = j + 1;
        decide = true;
        break;
      }
    }
    if (decide)
      ans = max(ans, i + v - 1);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  a[0] = 0;
  b[0] = 0;
  rep1(i, n) {
    ll p;
    cin >> p;
    a[i] = a[i - 1] + p;
  }
  rep1(i, m) {
    ll q;
    cin >> q;
    b[i] = b[i - 1] + q;
  }
  int ans = 0;
  rep(i, n + 1) {
    ll judge = k - a[i];
    if (judge < 0)
      break;
    int v = -1;
    per(j, m + 1) {
      if (judge >= b[j]) {
        v = j;
        break;
      }
    }
    if (v != -1)
      ans = max(ans, i + v);
  }

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

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

int main() {

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

  vector<long long> ruiseki_A(N + 1);
  ruiseki_A[0] = 0;
  for (int i = 1; i <= N; i++)
    ruiseki_A[i] = ruiseki_A[i - 1] + A[i - 1];

  vector<long long> ruiseki_B(M + 1);
  ruiseki_B[0] = 0;
  for (int i = 1; i <= M; i++)
    ruiseki_B[i] = ruiseki_B[i - 1] + B[i - 1];

  int result = 0;
  for (int i = 0; i <= N; i++) {
    int t;
    long long K2 = K - ruiseki_A[i];
    if (K2 < 0)
      break;
    else {
      auto it = upper_bound(ruiseki_B.begin(), ruiseki_B.end(), K2);
      if (it != ruiseki_B.end())
        t = i + distance(ruiseki_B.begin(), it) - 1;
      else
        t = i + M;
      if (result < t)
        result = t;
    }
  }

  cout << result;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

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

  vector<long long> ruiseki_A(N + 1);
  ruiseki_A[0] = 0;
  for (int i = 1; i <= N; i++)
    ruiseki_A[i] = ruiseki_A[i - 1] + A[i - 1];

  vector<long long> ruiseki_B(M + 1);
  ruiseki_B[0] = 0;
  for (int i = 1; i <= M; i++)
    ruiseki_B[i] = ruiseki_B[i - 1] + B[i - 1];

  long long max = 0;
  int result = 0;

  for (int i = 0; i <= N; i++) {
    for (int j = 0; ruiseki_A[i] + ruiseki_B[j] <= K && j <= M; j++) {
      int t = i + j;
      if (result < t)
        result = t;
    }
  }

  cout << result;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 200010;
ll a[N], b[N];
int n, m;
ll k;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int res = 0;
  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];
  }
  for (int i = 0, j = m; i <= n; i++) {
    while (j && a[i] > k - b[j])
      j--;
    if (a[i] <= k - b[j])
      res = max(res, i + j);
  }
  cout << res << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 200010;
int a[N], b[N];
int n, m, k;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long res = 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];

  int i = 0, j = 0;
  while (i < n && j < m) {
    if (a[i] <= b[j]) {
      if (k >= a[i]) {
        k -= a[i++];
        res++;
      } else
        break;
    } else {
      if (k >= b[j]) {
        k -= b[j++];
        res++;
      } else
        break;
    }
  }
  while (i < n && k >= a[i]) {
    k -= a[i++];
    res++;
  }
  while (j < m && k >= b[k]) {
    k -= b[j++];
    res++;
  }
  cout << res << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N, M, K, ans = 0;
  int j;
  cin >> N >> M >> K;

  vector<long long> A(N + 5), B(M + 5);
  A[0] = 0;
  B[0] = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i + 1];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i + 1];
  }
  for (int i = 1; i < N + 1; i++) {
    A[i] += A[i - 1];
  }
  for (int i = 1; i < M + 1; i++) {

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

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


Error: -11

Fixed submission:

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

int main() {
  long long N, M, K, ans = -1, j;
  cin >> N >> M >> K;
  j = M;
  vector<int> A(N + 5), B(M + 5);
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
  }
  for (int i = 1; i < M + 1; 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];
  }
  for (int i = 0; i < N + 1; i++) {
    while (A[i] + B[j] > K) {
      j -= 1;
    }
    ans = max(ans, i + j);
  }

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

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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

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

  vector<ll> SA(N + 1, 0);
  for (int ii = 0; ii < N; ++ii) {
    ll tmp;
    cin >> tmp;
    SA[ii + 1] = SA[ii] + tmp;
  }

  vector<ll> SB(M + 1, 0);
  for (int ii = 0; ii < M; ++ii) {
    ll tmp;
    cin >> tmp;
    SB[ii + 1] = SB[ii] + tmp;
  }

  ll ans = 0;
  for (int ii = 0; ii <= N; ++ii) {
    if (SA[ii] <= K) {
      ll l = -1;
      ll r = M + 1;
      while (l + 1 < r) {
        ll mid = (l + r) / 2;
        ll time = SA[ii] + SB[mid];
        if (time > K) {
          r = mid;
        } else {
          l = mid;
        }
      }
      ans = max(ans, ii + l);
    }
  }

  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

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

  vector<ll> A(N);
  for (int ii = 0; ii < N; ++ii) {
    cin >> A[ii];
  }
  vector<ll> B(N);
  for (int ii = 0; ii < N; ++ii) {
    cin >> B[ii];
  }

  ll ans = 0;
  ll time = 0;
  int aidx = 0;
  int bidx = 0;
  while (true) {
    if ((aidx == N) && (bidx == M)) {
      break;
    } else if (aidx == N) {
      time += B[bidx];
      if (time > K) {
        break;
      }
      bidx++;
      ans++;
    } else if (bidx == M) {
      time += A[aidx];
      if (time > K) {
        break;
      }
      aidx++;
      ans++;
    } else {
      ll ta = A[aidx];
      ll tb = B[bidx];
      if (ta < tb) {
        time += ta;
        if (time > K) {
          break;
        }
        aidx++;
        ans++;
      } else {
        time += tb;
        if (time > K) {
          break;
        }
        bidx++;
        ans++;
      }
    }
  }

  cout << ans << "\\n";

  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>

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

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

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

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

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

ll ltmp;
string stmp;
double dtmp;

int main() {

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

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

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

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

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

  cout << cnt << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>

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

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

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

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

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

ll ltmp;
string stmp;
double dtmp;

int main() {

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

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

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

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

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

  cout << cnt << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

/* [Template].cpp
Example code to use when starting new code. This particular problem was problem
977A_Wrong_Subtraction from CodeForces

Compile: g++  -o C.exe C.cpp
Execute: ./C
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main() {
  int n, m, k;
  long a[200000], b[200000];
  vector<long long> a_cum(200001, 0), b_cum(200001, 0);
  int result = 0;

  // Input
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  // Set up cumulative arrays
  a_cum[0] = 0;
  for (int i = 1; i <= n; i++) {
    a_cum[i] = a_cum[i - 1] + a[i - 1];
  }
  b_cum[0] = 0;
  for (int i = 1; i <= m; i++) {
    b_cum[i] = b_cum[i - 1] + b[i - 1];
  }

  // Solve problem
  result = 0;
  int j_start;
  for (int i = 0; i <= n && a_cum[i] <= k; i++) {
    j_start = max(0, min(m, result - i));
    for (int j = j_start; j <= m && a_cum[i] + b_cum[j] <= k; j++) {
      if (a_cum[i] + b_cum[j] <= k) {
        if (i + j > result) {
          result = i + j;
        }
      }
    }
  }

  cout << result << endl;

  return 0;
}


Error: 0

Fixed submission:

/* [Template].cpp
Example code to use when starting new code. This particular problem was problem
977A_Wrong_Subtraction from CodeForces

Compile: g++  -o C.exe C.cpp
Execute: ./C
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main() {
  int n, m, k;
  long a[200000], b[200000];
  vector<long long> a_cum(200001, 0), b_cum(200001, 0);
  long result = 0;

  // Input
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  // Set up cumulative arrays
  a_cum[0] = 0;
  for (int i = 1; i <= n; i++) {
    a_cum[i] = a_cum[i - 1] + a[i - 1];
  }
  b_cum[0] = 0;
  for (int i = 1; i <= m; i++) {
    b_cum[i] = b_cum[i - 1] + b[i - 1];
  }

  // Solve problem
  result = 0;
  for (int i = 0; i <= n && a_cum[i] <= k; i++) {
    for (int j = result - i; j <= m && a_cum[i] + b_cum[j] <= k; j++) {
      if (a_cum[i] + b_cum[j] <= k) {
        if (i + j > result) {
          result = i + j;
        }
      }
    }
  }

  cout << result << endl;

  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define mx 200005
#define ll long long

int ar[mx], br[mx];
char ch[mx];
ll pre1[mx], pre[mx];

int n, m, k, ii;

bool func(int tar) {
  bool f = false;
  int i = min(n, tar);
  int j = tar - i;
  // cout<<i+j<<" "<<tar<<endl;
  while (i >= 0 && j <= m && i + j == tar) {
    if (pre1[j] + pre[i] <= 1LL * k) {
      // cout<<tar<<" "<<pre[i]<<" "<<pre1[j]<<endl;
      f = true;
      break;
    } else
      i--, j++;
  }
  return f;
}

void solve() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &ar[i]);
    pre[i + 1] = pre[i] + ar[i];
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &br[i]);
    pre1[i + 1] = pre1[i] + br[i];
  }
  int lo = 1, hi = n + m;
  int re = 0;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (func(mid)) {
      re = mid;
      lo = mid + 1;
    } else
      hi = mid - 1;
  }
  printf("%d\\n", re);
}

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int t = 1;
  // scanf("%d",&t);
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

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

#define mx 200005
#define ll long long

int ar[mx], br[mx];
char ch[mx];

int n, m, k, ii;

int func(int i, int j, int val) {
  int re = 0, re1 = 0;
  if (i < n && ar[i] <= val)
    re = 1 + func(i + 1, j, val - ar[i]);
  if (j < m && br[j] <= val)
    re1 = 1 + func(i, 1 + j, val - br[j]);
  return max(re, re1);
}

void solve() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &ar[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &br[i]);
  printf("%d\\n", func(0, 0, k));
}

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int t = 1;
  // scanf("%d",&t);
  while (t--)
    solve();
  return 0;
}