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

#include <bits/stdc++.h>
#include <string>
typedef long long ll;
const int mod = 1e9 + 7;
using namespace std;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0), b(m + 1, 0);
  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];
  }
  ll ans = -1e18, j = 0;
  for (int i = n; i >= 0; --i) {
    int t = k;
    if (a[i] > k)
      continue;
    k -= a[i];
    while (k - b[j] > 0 && j <= m) {
      if (k - b[++j] < 0 || j > m) {
        --j;
        break;
      }
    }
    ans = max(ans, i + j);
    k = t;
  }
  cout << ans << endl;
}

int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <string>
typedef long long ll;
const int mod = 1e9 + 7;
using namespace std;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n + 1, 0), b(m + 1, 0);
  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 = -1e9;
  int j = 1;
  for (int i = 1; i <= n; ++i) {
    int t = k;
    if (a[i] > k)
      break;
    k -= a[i];
    while (k - b[j] > 0) {
      j++;
      ans = max(i + j, ans);
    }
    k = t;
  }
  cout << ans << endl;
}

int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  solve();
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int N, M;
  ll 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]; }
  int ans = 0;

  ll time = 0;
  rep(i, M) { time += B[i]; }

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

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int N, M;
  ll 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(N + 1);
  a[0] = 0;
  for (int i = 1; i < a.size(); ++i) {
    a[i] = A[i - 1] + a[i - 1];
  }

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

  cout << ans << endl;

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

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

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

Error: TLE

Fixed submission:

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

int se(int N, int M, int K, int A[], int B[], int index_a, int index_b,
       int time, int count) {
  if (time > K) {
    count--;
    return count;
  }
  int count_list[2] = {-1, -1};
  if (index_a >= N && index_b >= M)
    return count;
  if (index_a < N) {
    count_list[0] =
        se(N, M, K, A, B, index_a + 1, index_b, time + A[index_a], count + 1);
  }
  if (index_b < M) {
    count_list[1] =
        se(N, M, K, A, B, index_a, index_b + 1, time + B[index_b], count + 1);
  }
  // return *max_element(count_list, count_list+2);
  return max(count_list[0], count_list[1]);
}

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

  int s;
  s = se(N, M, K, A, B, 0, 0, 0, 0);
  cout << s << endl;
}
Problem p02623 (C++) — buggy submission, verdict: 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[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;
}

Error: 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[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;
}
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 long long ll;

int main() {
  int N, M;
  ll K, A, B;
  cin >> N >> M >> K;
  vector<ll> AS(N + 1, 0), BS(M + 1, 0);
  rep(i, N) {
    cin >> A;
    AS[i + 1] = AS[i] + A;
  }
  rep(i, M) {
    cin >> B;
    BS[i + 1] = BS[i] + B;
  }
  int ans = 0;
  for (int i = 0; i <= N; i++) {
    if (AS[i] > K)
      break;
    int j = upper_bound(BS.begin(), BS.end(), K - AS[i]) - BS.begin();
    ans = max(ans, i + j - 1);
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

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

typedef long long ll;

int main() {
  int N, M;
  ll K, A, B;
  cin >> N >> M >> K;
  vector<ll> AS(N + 2, 0), BS(M + 2, 0);
  rep(i, N) {
    cin >> A;
    AS[i + 1] = AS[i] + A;
  }
  AS[N + 1] = 1e18;
  rep(i, M) {
    cin >> B;
    BS[i + 1] = BS[i] + B;
  }
  BS[N + 1] = 1e18;
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (AS[i] > K)
      break;
    int j = upper_bound(BS.begin(), BS.end(), K - AS[i]) - BS.begin();
    ans = max(ans, i + min(M, j - 1));
  }
  cout << ans << "\\n";
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

#define ipr pair<int, int>
#define pb push_back
#define ff first
#define ss second
#define fr(i, j, k) for (int i = j; i < k; i++)
#define rf(i, j, k) for (int i = j; i >= k; i--)
#define ll unsigned long long
#define int long long
#define ld long double
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
#define cnt_ones(x) __builtin_popcountll(x)

#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const int Maxn = (int)1e6;
const ld pi = acos(-1);
const int mod = (int)1e9 + 7;
const int inf = (int)1e18;

template <class T> ostream &operator<<(ostream &out, vector<T> &A) {
  fr(i, 0, (int)A.size()) out << A[i] << " ";
  return out;
}

int32_t main() {
  IOS int n, m, k, res = 0;
  cin >> n >> m >> k;
  vector<int> A(n + 1), B(m + 1);
  A[0] = B[0] = 0;
  fr(i, 1, n + 1) cin >> A[i];
  fr(i, 1, m + 1) cin >> B[i];
  fr(i, 1, n + 1) A[i] += A[i - 1];
  fr(j, 1, m + 1) B[j] += B[j - 1];
  int j = m;
  for (int i = 0; i <= n; i++) {
    if (A[i] > k)
      break;
    while (j > 0 and A[i] + B[j] > k) {
      j--;
    }
    res = max(res, i + j);
  }
  cout << res << endl;
  return 0;
}

Error: -11

Fixed submission:

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

#define ipr pair<int, int>
#define pb push_back
#define ff first
#define ss second
#define fr(i, j, k) for (int i = j; i < k; i++)
#define rf(i, j, k) for (int i = j; i >= k; i--)
#define ll unsigned long long
#define int long long
#define ld long double
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
#define cnt_ones(x) __builtin_popcountll(x)

#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const int Maxn = (int)1e6;
const ld pi = acos(-1);
const int mod = (int)1e9 + 7;
const int inf = (int)1e18;

template <class T> ostream &operator<<(ostream &out, vector<T> &A) {
  fr(i, 0, (int)A.size()) out << A[i] << " ";
  return out;
}

//}

int32_t main() {
  IOS int n, m, k, res = 0;
  cin >> n >> m >> k;
  vector<int> A(n + 1), B(m + 1);
  A[0] = B[0] = 0;
  fr(i, 1, n + 1) cin >> A[i];
  fr(i, 1, m + 1) cin >> B[i];
  fr(i, 1, n + 1) A[i] += A[i - 1];
  fr(j, 1, m + 1) B[j] += B[j - 1];
  int j = m;
  for (int i = 0; i <= n; i++) {
    while (A[i] + B[j] > k) {
      j--;
    }
    res = max(res, i + j);
  }
  cout << res << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
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;
}
const ll INF = 1LL << 60;

int main() {
  int 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> a_sum(n + 1);
  rep(i, n) a_sum[i + 1] = a_sum[i] + a[i];
  vector<ll> b_sum(m + 1);
  rep(i, m) b_sum[i + 1] = b_sum[i] + b[i];

  ll ans = 0;
  ll b_max = m;
  rep(i, n + 1) {
    if (a_sum[i] > k)
      break;
    while (b_max > 0 && a_sum[i] + b_sum[b_max] > k)
      b_max--;
    ans = max(ans, i + b_max);
    // cout << i << ' ' << b_max << endl;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
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;
}
const ll INF = 1LL << 60;

int main() {
  int 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> a_sum(n + 1);
  rep(i, n) a_sum[i + 1] = a_sum[i] + a[i];
  vector<ll> b_sum(m + 1);
  rep(i, m) b_sum[i + 1] = b_sum[i] + b[i];

  ll ans = 0;
  rep(i, n) {
    ll b_max = 0;
    if (a_sum[i] > k)
      break;
    while (b_max <= m && a_sum[i] + b_sum.at(b_max + 1) <= k)
      b_max++;
    ans = max(ans, i + b_max);
  }

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

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

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 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>
#include <string>
#include <vector>
using namespace std;

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

  int i = 0;
  long long total = 0;
  while (total < k && i < m) {
    total += b[i];
    i++;
  }
  int maxb = i - 1;
  while (total < k && i < n) {
    total += a[i];
    i++;
  }
  int maxa = i - 1;
  int ans = maxb;
  for (int i = 0; i < maxa; i++) {
    for (int l = 0; l <= i; l++) {
      total += a[l];
    }
    for (int j = 0; j < maxb; j++) {
      total += b[j];
      if (total > k) {
        ans = max(ans, i + j);
        cout << total << endl;
        cout << i << " " << j << endl;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
#define PI 3.14159265359

int main() {
  ll N, M, K, A, B;
  cin >> N >> M >> K;
  vector<ll> Avec, Bvec;
  Avec.reserve(N + 1);
  Bvec.reserve(M + 1);
  Avec.push_back(0);
  Bvec.push_back(0);
  ll A_count = 0;
  bool flag = true;
  for (auto i = 0; i < N; i++) {
    cin >> A;
    if (!flag) {
      continue;
    }
    A_count += A;
    if (A_count > K) {
      flag = false;
      continue;
    }
    Avec.push_back(A_count);
  }
  ll B_count = 0;
  flag = true;
  for (auto i = 0; i < M; i++) {
    cin >> B;
    if (!flag) {
      continue;
    }
    B_count += B;
    if (B_count > K) {
      flag = false;
      continue;
    }
    Bvec.push_back(B_count);
  }
  ll B_index = Bvec.size() - 1;
  ll max = B_index;
  for (auto i = 0; i < Avec.size(); i++) {
    for (auto k = B_index; k >= 0; k--) {
      if (Avec[i] + Bvec[k] <= K) {
        break;
      }
      B_index--;
    }
    max = std::max(max, i + B_index);
  }
  cout << max << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
#define PI 3.14159265359

int main() {
  ll N, M, K, A, B;
  cin >> N >> M >> K;
  vector<ll> Avec, Bvec;
  Avec.reserve(N + 1);
  Bvec.reserve(M + 1);
  ll A_count = 0;
  Avec.push_back(0);
  Bvec.push_back(0);
  for (auto i = 0; i < N; i++) {
    cin >> A;
    A_count += A;
    if (A_count > K) {
      A_count -= A;
      break;
    }
    Avec.push_back(A_count);
  }
  ll B_count = 0;
  for (auto i = 0; i < M; i++) {
    cin >> B;
    B_count += B;
    if (B_count > K) {
      B_count -= B;
      break;
    }
    Bvec.push_back(B_count);
  }
  if (A_count + B_count <= K) {
    cout << Avec.size() + Bvec.size() - 2 << endl;
    return 0;
  }
  ll B_index = Bvec.size() - 1;
  ll max = B_index;
  for (auto i = 0; i < Avec.size(); i++) {
    for (auto k = B_index; k >= 0; k++) {
      if (Avec[i] + Bvec[k] < K) {
        break;
      }
      B_index--;
    }
    max = std::max(max, i + B_index);
  }
  cout << max << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

typedef long long ll;

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define AA(i) cout << i << endl;
#define AS(i) cout << i << " ";

const int INF = 1001001001;
const double PI = 3.14159;

using namespace std;

int main() {

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

  ll a[n], b[m];
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  int ans = 0;

  ll bm = 0;
  int bn = 0;
  rep(i, m) { bm += b[i]; }

  bn = m;

  ll anow = 0;
  rep(i, n + 1) {

    if (i > 0)
      anow += a[i - 1];

    while (bm > k - anow && anow < k) {
      bn--;
      bm -= b[bn];
    }

    // AA("an:" << i << " bn:" << bn << " sum:" << anow + bm);
    if (anow + bm <= k)
      ans = max(i + bn, ans);
  }

  AA(ans);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define AA(i) cout << i << endl;
#define AS(i) cout << i << " ";

const int INF = 1001001001;
const double PI = 3.14159;

using namespace std;

int main() {

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

  ll a[n], b[m];
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  int ans = 0;

  int bm = 0;
  int bn = 0;
  rep(i, m) { bm += b[i]; }

  bn = m;

  int anow = 0;
  rep(i, n + 1) {

    if (i > 0)
      anow += a[i - 1];

    while (bm > k - anow && anow < k) {
      bn--;
      bm -= b[bn];
    }

    // AA("an:" << i << " bn:" << bn << " sum:" << anow + bm);
    if (anow + bm <= k)
      ans = max(i + bn, ans);
  }

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

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

int fun(vector<long long int> X, int N) {

  int size = X.size();
  int min = 0;
  int max = size - 1;
  int mid;

  if (X[min] > N)
    return 0;
  if (X[max] <= N)
    return (max + 1);

  while (max - min > 1) {
    mid = (max + min) / 2;

    if (X[mid] > N) {
      max = mid;
    } else {
      min = mid;
    }
  }

  if (X[max] > N) {
    return (min + 1);
  } else {
    return (max + 1);
  }

  return 0;
}

int main() {

  int i, j, N, M, K, x, ans;
  cin >> N >> M >> K;

  vector<long long int> A(N, 0);
  vector<long long int> B(M, 0);
  vector<long long int> A_sum(N, 0);
  vector<long long int> B_sum(M, 0);

  for (i = 0; i < N; i++) {
    cin >> A[i];
    if (i == 0) {
      A_sum[i] = A[i];
    } else {
      A_sum[i] = A_sum[i - 1] + A[i];
    }
  }

  for (i = 0; i < M; i++) {
    cin >> B[i];
    if (i == 0) {
      B_sum[i] = B[i];
    } else {
      B_sum[i] = B_sum[i - 1] + B[i];
    }
  }

  x = fun(B_sum, K);

  if (x == 0) {
    ans = fun(A_sum, K);
    cout << ans << "\\n";
    return 0;
  }

  ans = x;

  for (i = 0; i < N; i++) {

    int t = K - A_sum[i];
    if (t < 0)
      break;

    while (B_sum[x - 1] > t && x > 0)
      x--;

    if (i + 1 + x > ans)
      ans = i + 1 + x;
  }

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

  return 0;
}


Error: TLE

Fixed submission:

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

int fun(vector<long long int> X, int N) {

  int size = X.size();
  int min = 0;
  int max = size - 1;
  int mid;

  if (X[min] > N)
    return 0;
  if (X[max] <= N)
    return (max + 1);

  while (max - min > 1) {
    mid = (max + min) / 2;

    if (X[mid] > N) {
      max = mid;
    } else {
      min = mid;
    }
  }

  if (X[max] > N) {
    return (min + 1);
  } else {
    return (max + 1);
  }

  return 0;
}

int main() {

  int i, N, M, K, x, ans;
  cin >> N >> M >> K;

  vector<long long int> A(N, 0);
  vector<long long int> B(M, 0);
  vector<long long int> A_sum(N, 0);
  vector<long long int> B_sum(M, 0);

  for (i = 0; i < N; i++) {
    cin >> A[i];
    if (i == 0) {
      A_sum[i] = A[i];
    } else {
      A_sum[i] = A_sum[i - 1] + A[i];
    }
  }

  for (i = 0; i < M; i++) {
    cin >> B[i];
    if (i == 0) {
      B_sum[i] = B[i];
    } else {
      B_sum[i] = B_sum[i - 1] + B[i];
    }
  }

  ans = fun(B_sum, K);

  for (i = 0; i < N; i++) {

    int t = K - A_sum[i];
    if (t < 0)
      break;

    x = fun(B_sum, t) + i + 1;

    if (x > ans)
      ans = x;
  }

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

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

// be name khoda
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N, M, ans = 0;
  ll K, t = 0;
  cin >> N >> M >> K;
  ll A[N], B[M];
  for (int i = 0; i < N; i++)
    cin >> A[i];

  for (int i = 0; i < M; i++)
    cin >> B[i];

  ll c[M + 1]{};
  for (int i = 0; i < M; i++)
    c[i + 1] = c[i] + B[i];

  for (int i = 0; i <= N; i++) {
    if (i != 0)
      t += A[i - 1];
    if (t > K)
      break;
    int l = -1;
    int r = M + 1;
    while (r - l > 1) {
      int m = (l + r) / 2;
      if (t + c[m] <= K)
        l = m;
      else
        r = m;
    }
    ans = max(ans, i + l);
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

// be name khoda
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int n, m, k, x, sum = 0, ans = 0;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  int a[n + 1], b[m + 1], r = n + 1, y = m + 1;
  a[0] = 0;
  b[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    sum += x;
    a[i] = sum;
    if (sum > k) {
      r = i;
      break;
    }
  }
  for (int i = r; i < n; i++)
    cin >> x;
  sum = 0;
  for (int i = 1; i <= m; i++) {
    cin >> x;
    sum += x;
    b[i] = sum;
    if (sum > k) {
      y = i;
      break;
    }
  }
  bool d = false;
  for (int i = r - 1; i >= 0; i--) {
    if (a[i] > k)
      break;
    for (int j = 0; j < y; j++) {
      if (a[i] + b[j] > k)
        break;
      if (i + j > ans)
        ans = i + j;
      if (ans == k) {
        d = true;
        break;
      }
    }
    if (d)
      break;
  }
  cout << ans;
  return 0;
}