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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 999999999
#define rep(i, n) for (int i = 0; i < n; i++)
const int MOD = 1000000007;
ll cnt = 0, ans = 0;
bool graph[8][8];

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

  ll a[n + 1], b[m + 1], A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;
  int l = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    A[i] = A[i - 1] + a[i];
  }

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

  ll j = m;
  for (int i = 0; i <= n; i++) {
    if (A[i] > k)
      break;

    while (B[j] > k - A[i]) {
      j--;
    }

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

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 999999999
#define rep(i, n) for (int i = 0; i < n; i++)
const int MOD = 1000000007;
ll cnt = 0, ans = 0;
bool graph[8][8];

const int NUMBER_OF_SAMPLES = 100000000;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  queue<ll> aall, ball;
  int a, b;
  for (int i = 0; i < n; i++) {
    cin >> a;
    ans += a;
    aall.push(a);
  }
  for (int i = 0; i < m; i++) {
    cin >> b;
    ans += b;
    ball.push(b);
  }

  if (ans <= k) {
    cout << n + m << endl;
    return 0;
  }

  while (1) {
    a = aall.front();
    b = ball.front();

    if (a <= b) {
      cnt++;
      aall.pop();
      k -= a;
      if (k < 0) {
        cnt--;
        cout << cnt << endl;
        return 0;
      }
    } else {
      cnt++;
      ball.pop();
      k -= b;
      if (k < 0) {
        cnt--;
        cout << cnt << endl;
        return 0;
      }
    }
  }
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
using namespace std;
#define ll long long
const ll N = 2e5 + 107;
ll n, m, k, i, j, sum, rasp = INT_MIN, a[N], b[N];
int main() {
  cin >> n >> m >> k;
  for (i = 1; i <= n; ++i)
    cin >> a[i];
  for (i = 1; i <= m; ++i)
    cin >> b[i], sum += b[i];
  j = m;
  for (i = 0; i <= n; ++i) {
    sum += a[i];
    while (sum > k && j) {
      sum -= b[j];
      j--;
    }
    if (sum > k)
      break; /// importanta pentru cand sunt eliminate toate din b[], dar suma
             /// celor din a[] (pana la i) ramane mai mare decat k (prin
             /// adaugarea ultimului element)
    rasp = max(rasp, i + j);
  }
  cout << rasp << "\\n";
}


Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
using namespace std;
#define ll long long
const ll N = 2e5 + 107;
ll n, m, k, i, j, best, x, rasp = INT_MIN, a[N], b[N];
int main() {
  cin >> n >> m >> k;
  for (i = 1; i <= n; ++i) {
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  for (i = 1; i <= m; ++i) {
    cin >> x;
    b[i] = b[i - 1] + x;
  }
  while (b[j] <= k && j < m)
    j++;
  best = j;
  for (i = 0; i <= n; ++i) {
    if (a[i] > k)
      break;
    j = best;
    while (b[j] > k - a[i] && j >= 0)
      j--;
    rasp = max(rasp, i + j);
  }
  cout << rasp << "\\n";
}
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;
using ll = long long;
using P = pair<int, int>;
using V = vector<int>;

int main(int argc, char const *argv[]) {
  int n, m, k;
  cin >> n >> m >> k;
  long long int a, b, c[200010], d[200010];
  c[0] = 0;
  rep(i, n) {
    cin >> a;
    c[i + 1] = c[i] + a;
  }
  d[0] = 0;
  rep(i, m) {
    cin >> b;
    d[i + 1] = d[i] + b;
  }
  int ans = 0;
  int j = m;
  rep(i, n + 1) {
    if (k < c[i])
      break;
    while (k < c[i] + d[j]) {
      j--;
    }
    ans = max(ans, i + j);
    // cout << ans << " " << i << " " << j << endl;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

int main(int argc, char const *argv[]) {
  int n, m, k;
  cin >> n >> m >> k;
  long long int a, b, c[20010], d[20010];
  c[0] = 0;
  rep(i, n) {
    cin >> a;
    c[i + 1] = c[i] + a;
  }
  d[0] = 0;
  rep(i, m) {
    cin >> b;
    d[i + 1] = d[i] + b;
  }
  int ans = 0;
  int j = m;
  rep(i, n + 1) {
    if (k < c[i])
      break;
    while (k < c[i] + d[j]) {
      j--;
    }
    ans = max(ans, i + j);
    // cout << ans << " " << i << " " << j << endl;
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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;
  rep(i, 1, n + 1) {
    ll a;
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  rep(i, 1, m + 1) {
    ll b;
    cin >> b;
    B[i] = B[i - 1] + b;
  }
  ll ans = 0;
  ll check = m;
  rep(i, 0, n + 1) {
    if (A[i] > k) {
      break;
    } else {
      ll max_i;

      while (A[i] + B[check] > k && check >= 0) {

        check--;
      }
      // assert(check>=0);
      max_i = i + check;

      ans = max(ans, max_i);
    }
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> A(n);
  vector<ll> B(m);
  rep(i, 0, n) {
    ll a;
    cin >> a;
    if (i == 0) {
      A[i] = a;
    } else {
      A[i] = A[i - 1] + a;
    }
  }
  rep(i, 0, m) {
    ll b;
    cin >> b;
    if (i == 0) {
      B[i] = b;
    } else {
      B[i] = B[i - 1] + b;
    }
  }
  ll ans = 0;
  ll check = m - 1;
  rep(i, 0, n) {
    if (A[i] > k) {
      break;
    } else {
      ll max_i;
      if (A[i] + B[m - 1] <= k) {
        max_i = i + m - 1 + 2;
      } else {
        while (A[i] + B[check] > k && check >= 0) {

          check--;
        }
        assert(check >= 0);
        max_i = i + check + 2;
      }
      ans = max(ans, max_i);
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[214514], b[214514];
int main() {
  ll n, m, k;
  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];
  }
  ll now = m, ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = now; j >= 0; j--) {
      if (a[i] + b[j] <= k) {
        now = j;
        ans = max(ans, i + now);
        break;
      }
    }
  }
  cout << ans << endl;
  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 sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using ll = long long;
int main() {
  freopen("g:/coding/cpp/input.txt", "r", stdin);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), prefa(n), prefb(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (!i)
      prefa[i] = a[i];
    else
      prefa[i] = prefa[i - 1] + a[i];
  }
  for (int j = 0; j < m; j++) {
    cin >> b[j];
    if (!j)
      prefb[j] = b[j];
    else
      prefb[j] = prefb[j - 1] + b[j];
  }
  // x of a and y of b such that x+y is max
  ll best = 0;
  for (int i = 0; i < n; i++) {
    if (prefa[i] > k)
      break;
    for (int j = 0; j < m; j++) {
      if (prefa[i] + prefb[j] > k)
        break;
      best = max(best, (ll)i + j + 2);
    }
  }
  cout << best;
}
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;
using ll = long long;
using P = pair<int, int>;

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

  int ai = 0;
  rep(i, n) {
    if (a.at(i) <= k) {
      ai++;
      k -= a.at(i);
    } else
      break;
  }

  int bi = 0;
  rep(i, m) {
    if (b.at(i) <= k) {
      bi++;
      k -= b.at(i);
    } else
      break;
  }

  int ans = ai + bi;
  int ansi = ans;
  for (int i = ai - 1; 0 <= i; i--) {
    k += a.at(i);
    ansi--;
    while (bi < m) {
      if (b.at(bi) <= k) {
        ansi++;
        k -= b.at(bi);
        bi++;
      } else
        break;
    }
    ans = max(ansi, ans);
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

  int ai = 0;
  rep(i, n) {
    if (a.at(i) <= k) {
      ai++;
      k -= a.at(i);
    } else
      break;
  }

  int bi = 0;
  rep(i, m) {
    if (b.at(i) <= k) {
      bi++;
      k -= b.at(i);
    } else
      break;
  }

  int ans = ai + bi;
  int ansi = ans;
  for (int i = ai - 1; 0 <= i; i--) {
    k += a.at(i);
    ansi--;
    while (bi - 1 < m) {
      if (b.at(bi - 1) <= k) {
        ansi++;
        k -= b.at(bi - 1);
        bi++;
      } else
        break;
    }
    ans = max(ansi, ans);
  }
  cout << ans << endl;

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

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

#define ll long long int
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define all(A) A.begin(), A.end()
#define sz(A) (int)A.size()
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tii;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const int inf = 1e9;
// const ll inf = 1e18;
const int N = 2e5;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  a[0] = b[0] = 0;
  ll dp[m + 1];
  dp[0] = 0;
  ll dp2[n + 1];
  dp2[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp2[i] = dp2[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    dp[i] = dp[i - 1] + b[i];
  }
  ll sum = 0;
  ll ans = 0, temp;
  for (int i = 1; i <= n; i++) {
    sum += a[i];
    if (sum > k)
      break;
    temp = i;
    ll lo = 0, hi = m;
    while (hi - lo > 1) {
      ll md = (hi + lo) / 2;
      if (dp[md] + sum <= k)
        lo = md;
      else
        hi = md;
    }
    if (dp[hi] + sum <= k)
      temp += hi;
    else
      temp += lo;
    ans = max(ans, temp);
  }
  sum = 0;
  for (int i = 1; i <= m; i++) {
    sum += b[i];
    if (sum > k)
      break;
    ll lo = 0, hi = n;
    temp = i;
    while (hi - lo > 1) {
      ll md = (hi + lo) / 2;
      if (dp2[md] + sum < k)
        lo = md;
      else
        hi = md;
    }
    if (dp2[hi] + sum <= k)
      temp += hi;
    else
      temp += lo;
    ans = max(ans, temp);
  }
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long int
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define all(A) A.begin(), A.end()
#define sz(A) (int)A.size()
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tii;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const int inf = 1e9;
// const ll inf = 1e18;
const int N = 2e5;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  int i = 0, j = 0;
  ll cnt = 0;
  ll ans = 0;
  while (cnt <= k) {
    if (a[i] + cnt > k && cnt + b[j] > k)
      break;
    if (a[i] < b[i])
      cnt += a[i], i++;
    else
      cnt += b[i], j++;
    ans++;
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  VL a(n), b(m);
  VL sa(n + 1, 0), sb(m + 1, 0);
  rep(i, n) {
    cin >> a[i];
    sa[i + 1] = sa[i] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    sb[i + 1] = sb[i] + b[i];
  }
  ll ans = 0;
  rep(i, n + 1) {
    if (sa[i] > k) {
      continue;
    }
    ll cur = i;
    cur += upper_bound(ALL(sb), k - sa[i]) - sb.begin() - 1;
    // cerr << i << " " << cur << endl;
    chmax(ans, cur);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  VL a(n), b(m);
  VL sa(n + 1, 0), sb(m + 1, 0);
  rep(i, n) {
    cin >> a[i];
    sa[i + 1] = sa[i] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    sb[i + 1] = sb[i] + b[i];
  }
  ll ans = 0;
  rep(i, k + 1) {
    ll cur = upper_bound(ALL(sa), i) - sa.begin() - 1;
    cur += upper_bound(ALL(sb), k - i) - sb.begin() - 1;
    // cerr << i << " " << cur << endl;
    chmax(ans, cur);
  }
  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;

  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a.at(i);
  }

  vector<int> b(m);
  for (int i = 0; i < m; ++i) {
    cin >> b.at(i);
  }

  int bestNum = 0;
  int numA = 0;
  int numATime = k;

  int numB = 0;
  int numBTime = 0;

  {
    int tempK = k;
    for (int i = 0; i < n; ++i) {
      tempK -= a[i];
      if (tempK < 0) {
        tempK += a[i];
        break;
      }
      ++bestNum;
    }
    numA = bestNum;
    numATime = tempK;
    numBTime = numATime;

    for (int i = 0; i < m; ++i) {
      tempK -= b[i];
      if (tempK < 0) {
        break;
      }
      ++bestNum;
    }
  }

  while (numA > 0) {
    --numA;
    int tempK = numBTime;
    int tempNum = 0;

    numATime += a[numA];
    tempK += a[numA];
    tempNum = numA;

    tempNum += numB;

    for (int mi = numB; mi < m; ++mi) {
      tempK -= b[mi];
      if (tempK < 0) {
        tempK += b[mi];
        break;
      }
      ++numB;
    }

    numBTime = tempK;
    tempNum = numA + numB;

    if (tempNum > bestNum) {
      bestNum = tempNum;
    }
  }

  cout << bestNum;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a.at(i);
  }

  vector<int> b(m);
  for (int i = 0; i < m; ++i) {
    cin >> b.at(i);
  }

  int bestNum = 0;
  int numA = 0;
  int numATime = k;

  {
    int tempK = k;
    for (int i = 0; i < n; ++i) {
      tempK -= a[i];
      if (tempK < 0) {
        tempK += a[i];
        break;
      }
      ++bestNum;
    }
    numA = bestNum;
    numATime = tempK;

    for (int i = 0; i < m; ++i) {
      tempK -= b[i];
      if (tempK < 0) {
        break;
      }
      ++bestNum;
    }
  }

  while (numA > 0) {
    --numA;
    int tempK = k;
    int tempNum = 0;

    if (numA > 0) {
      numATime += a[numA - 1];
      tempK = numATime;
      tempNum = numA;
    }

    for (int mi = 0; mi < m; ++mi) {
      tempK -= b[mi];
      if (tempK < 0) {
        break;
      }
      ++tempNum;
    }

    if (tempNum > bestNum) {
      bestNum = tempNum;
    }
  }

  cout << bestNum;

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

#include <bits/stdc++.h>

using namespace std;

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

  vector<long long> A(n + 1, 0);
  vector<long long> B(m + 1, 0);
  int count = 0;
  long long int sum_a, sum_b = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A[i + 1] = A[i] + a;
  }
  for (int j = 0; j < m; j++) {
    int b;
    cin >> b;
    B[j + 1] = B[j] + b;
  }
  int subM = m;
  for (int i = 0; i <= n; i++) {
    for (int j = subM; j >= 0; j--) {
      if (A[i] + B[j] <= k) {
        count = max(count, i + j);
        subM = j;
        break;
      }
    }
  }
  cout << count << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long int n, m, k;
vector<int> a(n);
vector<int> b(m);

void dfs(int i, int j, long long int time, int count) {

  if (time >= k) {
    cout << count << endl;
  } else {
    if (i <= n - 1 and j <= m - 1) {
      dfs(i + 1, j, time + a[i], count + 1);
      dfs(i, j + 1, time + b[j], count + 1);
    } else if (i > n - 1 and j <= m - 1) {
      dfs(i, j + 1, time + b[j], count + 1);
    } else if (i <= n - 1 and j > m - 1) {
      dfs(i + 1, j, time + a[j], count + 1);
    } else
      cout << count << endl;
  }
}

int main() {
  // int i, j =0;
  cin >> n >> m >> k;
  // long long int time=0;
  // int count=0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j < m; j++)
    cin >> b[j];

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

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

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;
}

#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define sqrep(i, cc, n) for (long long i = cc; i * i <= n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const ll inf = 1001001001;
const ll e9 = 1000000000;
const ll mod = 1000000007;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1), suma(n + 1), sumb(m + 1);
  rep(i, 0, n) cin >> a[i + 1];
  rep(i, 0, m) cin >> b[i + 1];

  rep(i, 0, n) suma[i + 1] = suma[i] + a[i + 1];
  rep(i, 0, m) sumb[i + 1] = sumb[i] + b[i + 1];

  int ans = 0;
  bool flag = false;
  rep(i, 0, n + 1) {
    ll temp = k - suma[i];
    if (temp < 0)
      break;
    int l = -1;
    int r = m + 1;
    while (r - l > 1) {
      int s = (l + r) / 2;
      if (sumb[s] > temp)
        r = s;
      else
        l = s;
    }
    ans = max(ans, i + l);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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;
}

#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define sqrep(i, cc, n) for (long long i = cc; i * i <= n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const ll inf = 1001001001;
const ll e9 = 1000000000;
const ll mod = 1000000007;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1), suma(n + 1), sumb(m + 1);
  rep(i, 0, n) cin >> a[i + 1];
  rep(i, 0, m) cin >> b[i + 1];
  rep(i, 0, n) suma[i + 1] = suma[i] + a[i + 1];
  int p = 0;
  rep(i, 0, m) {
    sumb[i + 1] = sumb[i] + b[i + 1];
    if (sumb[i + 1] >= k)
      p = i + 1;
  }
  if (p == 0)
    p = m;
  int ans = 0;
  bool flag = false;
  rep(i, 0, n + 1) {
    ll temp = k - suma[i];
    if (temp < 0)
      continue;
    rep(j, 0, p + 1) {
      if (i == 0 && j == 0)
        continue;
      if (temp < sumb[j]) {
        ans = max(i + j - 1, ans);
        p = j;
        continue;
      }
      flag = true;
    }
  }
  if (ans == 0 && flag) {
    cout << n + m << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}