Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define sync                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ll long long int
#define mod 1000000007
using namespace std;
vector<ll> prime, sfac;
vector<ll> ps;
void sieve(ll n) {
  prime.assign(n + 1, 1);
  sfac.assign(n + 1, 1);
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == 1) {
      sfac[p] = p;
      for (int i = p * p; i <= n; i += p) {
        prime[i] = 0;
        sfac[i] = p;
      }
    }
  }
  for (ll i = 2; i <= n; i++) {
    if (prime[i])
      ps.push_back(i);
  }
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    ll k = y & 1LL;
    if (k)
      res = res * x;
    y = y >> 1LL;
    x = x * x;
  }
  return res;
}

int isPrime(ll n) {
  if (n <= 1)
    return 0;
  if (n <= 3)
    return 1;
  if (n % 2 == 0 || n % 3 == 0)
    return 0;
  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return 0;
  return 1;
}
ll count(ll a[], ll b[], ll t, ll k, ll c, ll ai, ll bi, ll m, ll n) {
  if (t > k)
    return c - 1;
  ll c1 = -1, c2 = -1;
  if (ai < n)
    c1 = count(a, b, t + a[ai], k, c + 1, ai + 1, bi, m, n);
  if (bi < m)
    c2 = count(a, b, t + b[bi], k, c + 1, ai, bi + 1, m, n);
  if (c1 == -1 && c2 == -1)
    return n + m;
  else {
    return max(c1, c2);
  }
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m], ac[n], bc[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ac[i] = a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    bc[i] = b[i];
  }
  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] += b[i - 1];
  ll c = 0, mc = 0, pos = n;
  for (int i = 0; i < n; i++) {
    if (a[i] > k) {
      pos = i;
      break;
    }
  }

  c = pos;
  mc = c;
  pos--;
  ll rem = k;
  if (pos > -1)
    rem -= a[pos];
  int p = lower_bound(b, b + m, rem) - b;
  ll cc = c;
  if (p == m) {
    c += m;
    mc = c;
  } else {
    if (b[p] > rem)
      c += p;
    else
      c += p + 1;
    mc = c;
  }
  c = cc;
  while (pos > -1) {
    rem += ac[pos];
    c--;
    cc--;
    p = lower_bound(b, b + m, rem) - b;
    if (p == m) {
      c += m;
    } else {
      if (b[p] > rem)
        c += p;
      else
        c += p + 1;
    }
    if (c > mc)
      mc = c;
    c = cc;
    pos--;
  }
  cout << mc << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define sync                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ll long long int
#define mod 1000000007
using namespace std;
vector<ll> prime, sfac;
vector<ll> ps;
void sieve(ll n) {
  prime.assign(n + 1, 1);
  sfac.assign(n + 1, 1);
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == 1) {
      sfac[p] = p;
      for (int i = p * p; i <= n; i += p) {
        prime[i] = 0;
        sfac[i] = p;
      }
    }
  }
  for (ll i = 2; i <= n; i++) {
    if (prime[i])
      ps.push_back(i);
  }
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    ll k = y & 1LL;
    if (k)
      res = res * x;
    y = y >> 1LL;
    x = x * x;
  }
  return res;
}

int isPrime(ll n) {
  if (n <= 1)
    return 0;
  if (n <= 3)
    return 1;
  if (n % 2 == 0 || n % 3 == 0)
    return 0;
  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return 0;
  return 1;
}
ll count(ll a[], ll b[], ll t, ll k, ll c, ll ai, ll bi, ll m, ll n) {
  if (t > k)
    return c - 1;
  ll c1 = -1, c2 = -1;
  if (ai < n)
    c1 = count(a, b, t + a[ai], k, c + 1, ai + 1, bi, m, n);
  if (bi < m)
    c2 = count(a, b, t + b[bi], k, c + 1, ai, bi + 1, m, n);
  if (c1 == -1 && c2 == -1)
    return n + m;
  else {
    return max(c1, c2);
  }
}
int main() {
  ll n, m, k;
  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];
  cout << count(a, b, 0, k, 0, 0, 0, m, n) << endl;
}