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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mx *max_element
#define mn *min_element
#define fo(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define rfo(i, n) for (ll i = n - 1; i >= 0; i--)
#define rloop(i, a, b) for (ll i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define precision cout << fixed << setprecision(10)
#define mem(x, p) memset(x, p, sizeof(x))
#define w(t)                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define kk "\\n"
#define setbits(x) __builtin_popcountll(x)
const double pi = 3.1415926535897932384626;
// const ll mod = 998244353;
const ll mod = 1e9 + 7;
int main() {
  IOS ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  fo(i, n) cin >> a[i];
  fo(i, m) cin >> b[i];
  ll x[n], y[m];
  x[0] = a[0];
  y[0] = b[0];
  for (ll i = 1; i < n; i++)
    x[i] = x[i - 1] + a[i];
  for (ll i = 1; i < m; i++)
    y[i] = y[i - 1] + b[i];
  ll ans = upper_bound(y, y + m, k) - y;
  for (ll i = 0; i < n; i++) {
    if (x[i] <= k) {
      ll p = upper_bound(y, y + m, k - x[i]) - y;
      ans = max(ans, i + 1 + p);
    }
  }
  ll q = upper_bound(x, x + n, k) - x;
  ans = max(ans, q);
  for (ll i = 0; i < m; i++) {
    if (y[i] <= k) {
      ll p = upper_bound(x, x + n, k - y[i]) - x;
      ans = max(ans, i + 1 + p);
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mx *max_element
#define mn *min_element
#define fo(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define rfo(i, n) for (ll i = n - 1; i >= 0; i--)
#define rloop(i, a, b) for (ll i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define precision cout << fixed << setprecision(10)
#define mem(x, p) memset(x, p, sizeof(x))
#define w(t)                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define kk "\\n"
#define setbits(x) __builtin_popcountll(x)
const double pi = 3.1415926535897932384626;
// const ll mod = 998244353;
const ll mod = 1e9 + 7;
ll solve(ll *a, ll *b, ll i, ll j, ll n, ll m, ll k) {
  if (i == n && j == m)
    return 0;
  if (i == n) {
    if (b[j] <= k)
      return 1 + solve(a, b, i, j + 1, n, m, k - b[j]);
    else
      return 0;
  }
  if (j == m) {
    if (a[i] <= k)
      return 1 + solve(a, b, i + 1, j, n, m, k - a[i]);
    else
      return 0;
  }
  if (a[i] > k && b[j] > k)
    return 0;
  if (a[i] <= k && b[j] <= k)
    return 1 + max(solve(a, b, i + 1, j, n, m, k - a[i]),
                   solve(a, b, i, j + 1, n, m, k - b[j]));
  else {
    if (a[i] <= k)
      return 1 + solve(a, b, i + 1, j, n, m, k - a[i]);
    if (b[j] <= k)
      return 1 + solve(a, b, i, j + 1, n, m, k - b[j]);
  }
}
int main() {
  IOS ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  fo(i, n) cin >> a[i];
  fo(i, m) cin >> b[i];
  cout << solve(a, b, 0ll, 0ll, n, m, k);
  return 0;
}