alexjercan/bugnet huggingface dataset:
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  long long N, M, K;

  cin >> N >> M >> K;

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

  long books = 0;
  long j = M;

  for (long i = 0; i <= N; i++) {
    if (A[i] > K) {
      break;
    }
    while (B[j] > K - A[i]) {
      j--;
    }
    books = max(books, (i + j));
  }
  cout << books << "\\n";
}


Error: TLE

Fixed submission:

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

int main(void) {
  long long N, M, K;

  cin >> N >> M >> K;

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

  long long books = 0;

  for (long i = 0; i <= N; i++) {
    if (A[i] > K) {
      break;
    }
    for (long j = 0; j <= M; j++) {
      if (B[j] > K - A[i]) {
        break;
      }
      if ((i + j) > books) {
        books = i + j;
      }
    }
  }
  cout << books << "\\n";
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
#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 eb emplace_back
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define INF 1000000000
#define MOD 1000000007

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vll s1(n + 1), s2(m + 1);
  s1[0] = s2[0] = 0;
  rep(i, 0, n) {
    int tmp;
    cin >> tmp;
    s1[i + 1] = s1[i] + tmp;
  }
  rep(i, 0, m) {
    int tmp;
    cin >> tmp;
    s2[i + 1] = s2[i] + tmp;
  }
  int ans = 0;
  int j = m;
  for (int i = 0; i <= n && s1[i] <= k; ++i) {
    while (s2[j] > k - s1[i])
      --j;
    ans = max(ans, i + j);
  }
  cout << ans << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
#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 eb emplace_back
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define INF 1000000000
#define MOD 1000000007

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vll s1(n + 1), s2(m + 1);
  vi a(n), b(m);
  s1[0] = s2[0] = 0;
  rep(i, 0, n) {
    cin >> a[i];
    s1[i + 1] = s1[i] + a[i];
  }
  rep(i, 0, m) {
    cin >> b[i];
    s2[i + 1] = s2[i] + b[i];
  }
  int ans = 0;
  int j = m;
  for (int i = 0; i <= n && s1[i] <= k; ++i) {
    while (b[j] > k - a[i])
      --j;
    ans = max(ans, i + j);
  }
  cout << ans << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;

int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> aSum(n + 1, 0), bSum(m + 1, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    aSum[i + 1] = aSum[i] + a;
  }
  rep(i, m) {
    ll b;
    cin >> b;
    bSum[i + 1] = bSum[i] + b;
  }
  int ans = 0;
  int bPos = m;
  for (int i = 0; i <= n; i++) {
    if (aSum[i] > k)
      break;
    for (; bPos > 0; bPos--) {
      if (bSum[bPos] + aSum[i] <= k) {
        break;
      }
    }
    ans = max(ans, i + bPos);
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MOD = (int)1e9 + 7;
const double PI = 3.14159265358979323846;

int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  //   vector<ll> a(n), b(m);
  vector<ll> aSum(n + 1, 0), bSum(m + 1, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    aSum[i + 1] = aSum[i] + a;
  }
  rep(i, m) {
    ll b;
    cin >> b;
    bSum[i + 1] = bSum[i] + b;
  }
  int ans = 0;
  int bPos = m;
  for (int i = 0; i <= n; i++) {
    if (aSum[i] > k)
      break;
    for (bPos = m; bPos > 0; bPos--) {
      if (bSum[bPos] + aSum[i] <= k) {
        break;
      }
    }
    ans = max(ans, i + bPos);
  }

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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF 1000000007
#define mod 1005
#define ll long long int
const double mx = 2e6 + 5;

void solve() {
  ll n, m, k;
  ll maxPos = 0;
  cin >> n >> m >> k;
  //    cout<<"nmk\\n";
  vector<ll> a(n + 1), b(m + 1);
  a[0] = b[0] = 0;
  //    cout<<"workng\\n";
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    //		cout<<a[i]<<" ";
  }
  //    cout<<"working\\n";
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
    //		cout<<b[i]<<" ";
  }
  //	cout<<"\\n";
  for (int i = 0; i <= n; i++) {
    ll diff = k - a[i];
    ll ans = 0;
    if (diff < 0)
      break;
    ll low = 1, high = m;
    while (low <= high) {
      ll mid = low + (high - low) / 2;
      if (b[mid] <= diff) {
        ans = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    maxPos = max(maxPos, ans + i);
  }
  cout << maxPos << "\\n";
}
int main() {
  //    ios_base::sync_with_stdio(false);
  //    cin.tie(NULL);
  //    freopen("abc172dInput.txt", "r", stdin);
  int t = 1;
  //	cin>>t;
  while (t--) {
    //    	cout<<"ENTER\\n";
    solve();
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF 1000000007
#define mod 1005
#define ll long long int
const double mx = 2e6 + 5;

void solve() {
  ll n, m, k;
  ll maxPos = 0;
  cin >> n >> m >> k;
  //    cout<<"nmk\\n";
  vector<ll> a(n + 1), b(m + 1);
  a[0] = b[0] = 0;
  //    cout<<"workng\\n";
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    //		cout<<a[i]<<" ";
  }
  //    cout<<"working\\n";
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
    //		cout<<b[i]<<" ";
  }
  //	cout<<"\\n";
  for (int i = 0; i <= n; i++) {
    ll diff = k - a[i];
    ll ans = 0;
    if (diff < 0)
      break;
    ll low = 1, high = m;
    while (low <= high) {
      ll mid = low + (high - low) / 2;
      if (b[mid] <= diff) {
        ans = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    maxPos = max(maxPos, ans + i);
  }
  cout << maxPos << "\\n";
}
int main() {
  //    ios_base::sync_with_stdio(false);
  //    cin.tie(NULL);
  freopen("abc172dInput.txt", "r", stdin);
  int t = 1;
  //	cin>>t;
  while (t--) {
    //    	cout<<"ENTER\\n";
    solve();
  }
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using ll = long long;
using P = pair<ll, ll>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0), b(m + 1, 0);
  rep(i, n) {
    ll A;
    cin >> A;
    a[i + 1] = a[i] + A;
  }
  rep(i, m) {
    ll B;
    cin >> B;
    b[i + 1] = b[i] + B;
  }
  ll ans = 0;
  rep(i, n + 1) {
    if (a[i] <= k) {
      auto Iter = upper_bound(b.begin(), b.end(), k - a[i]);
      ans = max(i - 1 + (Iter - b.begin()), ans);
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using ll = long long;
using P = pair<ll, ll>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll cnt = 0;
  ll n, m, k;
  cin >> n >> m >> k;
  queue<ll> a, b;
  rep(i, n) {
    ll A;
    cin >> A;
    a.push(A);
  }
  rep(i, m) {
    ll B;
    cin >> B;
    a.push(B);
  }
  ll ans = 0;
  for (;;) {
    bool can_a = true;
    bool can_b = true;
    if (a.front() + cnt > k || a.empty())
      can_a = false;
    if (b.front() + cnt > k || b.empty())
      can_b = false;
    if (!can_a && !can_b)
      break;
    if (a.front() <= b.front() && can_a) {
      cnt += a.front();
      a.pop();
      ans++;
    } else if (b.front() < a.front() || can_b) {
      cnt += b.front();
      b.pop();
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

signed main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N), B(M);
  cin >> A >> B;
  int La = A.size(), Lb = B.size();

  int b = 0;
  int ta = 0, tb = 0;
  for (b = 0; b < Lb; ++b) {
    if (tb + B[b] > K)
      break;
    tb += B[b];
  }

  int cnt_max = b;
  for (int a = 1; a <= La; ++a) {
    ta += A[a - 1];
    while (ta + tb > K && b > 0) {
      tb -= B[b - 1];
      --b;
    }
    if (ta + tb > K)
      break;
    cnt_max = max(cnt_max, a + b);
  }
  cout << cnt_max << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

signed main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N), B(M);
  cin >> A >> B;
  int La = A.size(), Lb = B.size();

  int t = 0;
  int cnt_max = 0;
  for (int a = 0; a <= La; ++a) {
    if (a != 0) {
      t += A[a - 1];
    }
    int time = t, cnt = a;
    if (time > K)
      continue;
    REP(b, Lb) {
      if (time + B[b] > K)
        break;
      time += B[b];
      ++cnt;
    }
    cnt_max = max(cnt_max, cnt);
  }
  cout << cnt_max << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n], b[m];
  rep(i, n) { cin >> a[i]; }
  rep(i, m) { cin >> b[i]; }

  // 累積和
  ll A[n + 1], B[n + 1];
  A[0] = 0;
  B[0] = 0;
  rep(i, n) { A[i + 1] = A[i] + a[i]; }
  rep(i, m) { B[i + 1] = B[i] + b[i]; }

  int ans = 0;
  int j = m;
  rep(i, n + 1) {
    if (A[i] > k) {
      break;
    }
    while (B[j] > k - A[i]) {
      --j;
    }
    ans = max(ans, i + j);
  }
  cout << ans << 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))
#define all(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;

int main() {
  int n, m;
  ll 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> aa(n + 1, 0ll), bb(m + 1, 0ll);

  rep(i, n) aa[i + 1] = aa[i] + a[i];
  rep(i, m) bb[i + 1] = bb[i] + b[i];

  int ans = 0;

  for (int i = 0; i <= n; ++i) {
    int res = 0;
    ll remain = k - aa[i];
    if (remain < 0)
      break;
    res = i;

    int j = upper_bound(all(bb), remain) - bb.begin();
    res += j - 1;
    ans = max(ans, res);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);

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

  reverse(all(a));
  reverse(all(b));

  int ans = 0;
  while (k > 0ll) {
    int a_size = a.size();
    int b_size = b.size();

    if (a_size == 0 && b_size == 0)
      break;

    if (a_size == 0) {
      k -= b[b_size - 1];
      if (k < 0)
        break;
      b.pop_back();
      ans++;
    } else if (b_size == 0) {
      k -= b[a_size - 1];
      if (k < 0)
        break;
      a.pop_back();
      ans++;
    } else {
      if (a[a_size - 1] < b[b_size - 1]) {
        k -= a[a_size - 1];
        if (k < 0)
          break;
        a.pop_back();
        ans++;
      } else {
        k -= b[b_size - 1];
        if (k < 0)
          break;
        b.pop_back();
        ans++;
      }
    }
  }

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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <stdio.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

int main() {

  int N, M;
  cin >> N >> M;
  ll K;
  cin >> K;
  ll A[N + 1], B[M + 1];
  ll a, b;

  int ans = 0;

  A[0] = 0;
  B[0] = 0;

  for (int i = 1; i != N + 1; ++i) {
    cin >> a;
    A[i] = a + A[i - 1];
  };
  for (int i = 1; i != M + 1; ++i) {
    cin >> b;
    B[i] = b + B[i - 1];
  }

  ll *x;
  ll total;

  for (int i = 0; i <= N; ++i) {
    x = upper_bound(B, B + M + 1, K - A[i]);

    // cout<<A[i]<<":"<<K-A[i]<<":"<<(*x)<<":"<<x-B-1<<":"<<i<<endl;
    ans = ans >= (i + x - B - 1) ? ans : (i + x - B - 1);
    if (A[i] > K) {
      break;
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <stdio.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

int binary(ll A[], int N, ll k) {
  ll left = -1;
  ll right = N;
  while (left + 1 < right) {
    ll num = (left + right) / 2;
    if (k < A[num]) {
      right = num;
    } else {
      left = num;
    }
  }
  return left;
}

int main() {

  int N, M;
  cin >> N >> M;
  ll K;
  cin >> K;
  ll A[N + 1], B[M + 1];
  ll a, b;
  A[0] = 0;
  B[0] = 0;
  int ans = 0;

  for (int i = 1; i != N + 1; ++i) {
    cin >> A[i];
  };
  int b_max = 0;
  for (int i = 1; i != M + 1; ++i) {
    cin >> b;
    B[i] = b + B[i - 1];
  }

  ll R = 0;
  ll P = 0;
  int c = 0;

  for (int i = 0; i <= M; ++i) {
    R += A[i];
    P = K - R >= 0 ? K - R : 0;
    c = binary(B, M, P) == -1 ? 0 : binary(B, M, P);
    //  cout<<"c:"<<c<<endl;
    if (R + B[c] <= K) {
      ans = ans >= i + c ? ans : i + c;
      // cout<<"ans:"<<ans<<endl;
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define DATASIZE 200000

int main(void) {
  // Your code here!
  int A[DATASIZE];
  long Acum[DATASIZE];
  int B[DATASIZE];
  long Bcum[DATASIZE];
  int N, M, K, Nlim, Mlim;

  scanf("%d %d %d", &N, &M, &K);
  Nlim = N - 1;
  Mlim = M - 1;
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    if (i == 0)
      Acum[i] = A[i];
    else
      Acum[i] = Acum[i - 1] + A[i];

    if (Acum[i] > K && Nlim == N - 1)
      Nlim = i - 1;
  }

  for (int i = 0; i < M; i++) {
    scanf("%d", &B[i]);
    if (i == 0)
      Bcum[i] = B[i];
    else
      Bcum[i] = Bcum[i - 1] + B[i];

    if (Bcum[i] > K && Mlim == M - 1)
      Mlim = i - 1;
  }

  int mx = 0;
  int i = Nlim;
  int j = 0;
  if (Nlim < 0) {
    if (Mlim < 0) {
      mx = 0;
    } else {
      mx = Mlim + 1;
    }
  } else {
    if (Mlim < 0) {
      mx = Nlim + 1;
    } else {
      mx = Nlim + 1;
      while (i >= 0 && j <= Mlim) {
        if (Acum[i] + Bcum[j] <= K) {
          if (i + 1 + j + 1 > mx) {
            mx = i + 1 + j + 1;
            // printf("i:%d, j:%d, %ld + %ld < %d, mx = %d\\n",
            // i,j,Acum[i],Bcum[j],K,mx);
          }
          j++;
        } else {
          i--;
        }
      }
      if (mx < Mlim + 1)
        mx = Mlim + 1;
    }
  }

  printf("%d\\n", mx);
}

Error: TLE

Fixed submission:

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

#define DATASIZE 200000

int main(void) {
  // Your code here!
  int A[DATASIZE];
  long Acum[DATASIZE];
  int B[DATASIZE];
  int N, M, K;

  scanf("%d %d %d", &N, &M, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    if (i == 0)
      Acum[i] = A[i];
    else
      Acum[i] = Acum[i - 1] + A[i];
  }
  for (int j = 0; j < M; j++)
    scanf("%d", &B[j]);

  int mx = 0;
  long tempK;
  for (int i = 0; i <= N; i++) {
    if (i == 0)
      tempK = 0;
    else
      tempK = Acum[i - 1];

    for (int j = 0; j <= M; j++) {
      if (j != 0)
        tempK += B[j - 1];

      if (tempK > K)
        j = M;
      else if (tempK <= K && mx < i + j) {
        mx = i + j;
        // printf("i:%d j:%d, mx = %d\\n",i,j,mx);
      }
    }
  }

  printf("%d\\n", mx);
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define pai 3.1415926535897932384

using namespace std;
using ll = long long;
using P = pair<int, int>;

#define MAX_N 200001
#define MAX_M 200001

int N, M;
ll K = 0;
int res = 0;

ll A[MAX_N];
ll B[MAX_M];

int main(int argc, const char *argv[]) {

  cin >> N >> M >> K;

  rep(i, N) {
    int a;
    cin >> a;
    A[i + 1] = A[i] + a;
  }

  rep(i, M) {
    int b;
    cin >> b;
    B[i + 1] = B[i] + b;
  }

  for (int i = 0; i < N + 1; i++) {
    int l = 0;
    int r = M + 1;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (B[mid] <= K - A[i])
        l = mid;
      else
        r = mid;
    }

    if (A[i] + B[l] <= K)
      res = max(res, i + l);
  }

  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define pai 3.1415926535897932384

using namespace std;
using ll = long long;
using P = pair<int, int>;

#define MAX_N 200001
#define MAX_M 200001

int N, M;
int K = 0;
int res = 0;

int A[MAX_N];
int B[MAX_M];

void dfs(int n, int m, int time) {

  if (n < N && K >= time + A[n])
    dfs(n + 1, m, time + A[n]);
  if (m < M && K >= time + B[m])
    dfs(n, m + 1, time + B[m]);

  res = max(res, n + m);
}

int main(int argc, const char *argv[]) {

  cin >> N >> M >> K;

  rep(i, N) { cin >> A[i]; }

  rep(i, M) { cin >> B[i]; }

  dfs(0, 0, 0);

  cout << res << endl;

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

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

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

  vector<uint64_t> a(n + 1), b(m + 1);
  for (auto it = a.begin() + 1; it != a.end(); ++it) {
    uint64_t in;
    cin >> in;
    *it = *(it - 1) + in;
  }
  for (auto it = b.begin() + 1; it != b.end(); ++it) {
    uint64_t in;
    cin >> in;
    *it = *(it - 1) + in;
  }

  uint32_t ans = 0;
  uint32_t best = m;
  for (uint32_t i = 0; i <= n; ++i) {
    if (k < a[i])
      break;

    uint32_t j = best;
    do {
      if (b[j] <= k - a[i])
        break;
    } while (j-- != 0);
    best = j;
    ans = max(ans, i + j);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  vector<uint64_t> a(n + 1), b(m + 1);
  for (auto it = a.begin() + 1; it != a.end(); ++it) {
    uint64_t in;
    cin >> in;
    *it = *(it - 1) + in;
  }
  for (auto it = b.begin() + 1; it != b.end(); ++it) {
    uint64_t in;
    cin >> in;
    *it = *(it - 1) + in;
  }

  uint32_t ans = 0;
  for (uint32_t i = 0; i <= n; ++i) {
    if (k < a[i])
      break;

    uint32_t j = 0;
    while (j <= m && b[j] <= k - a[i]) {
      ++j;
    }
    --j;
    ans = max(ans, i + j);
  }

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

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

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

  vector<long> A(N + 1), B(M + 1);
  A.at(0) = 0;
  B.at(0) = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> conv;
    A.at(i) = conv + A.at(i - 1);
  }
  for (int i = 1; i < M + 1; i++) {
    cin >> conv;
    B.at(i) = conv + B.at(i - 1);
  }

  int ans = 0, m = M;
  for (int i = 0; i < N + 1; i++) {
    if (A.at(i) > K) {
      break;
    }
    while (K < A.at(i) + B.at(m)) {
      m--;
    }

    ans = max(ans, i + m);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  vector<long> A(N + 1), B(M + 1);
  A.at(0) = 0;
  B.at(0) = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> conv;
    A.at(i) = conv + A.at(i - 1);
  }
  for (int i = 1; i < M + 1; i++) {
    cin >> conv;
    B.at(i) = conv + B.at(i - 1);
  }

  int ans = 0, m;
  for (int i = 0; i < N + 1; i++) {
    if (A.at(i) > K) {
      break;
    }
    m = M;
    while (K < A.at(i) + B.at(m)) {
      m--;
    }

    ans = max(ans, i + m);
  }

  cout << ans << endl;
}