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

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

const ll INF = 1LL << 60;
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;
}
using Graph = vector<vector<int>>;

typedef pair<int, int> P;
typedef priority_queue<int, vector<int>, greater<int>> PQ;
const int MOD = 1000000007;

ll N, M, K;
vector<ll> A, B;
ll ans = 0;
/*
void dfs(ll count, ll index, ll time){
    if (time > K) return;
    if (index >= N && index >= M){
        ans = max(ans, count);
        return;
    }
    if (index < N) dfs(count+1, index+1, time+A[index]);
    if (index < M) dfs(count+1, index+1, time+B[index]);
    if (index < N && index < M) dfs(count+2, index+1, time+A[index]+ B[index]);
    dfs(count, index+1, time);

    ans = max(ans, count);

}
*/

int main() {
  cin >> N >> M >> K;
  A.assign(N, 0);
  B.assign(M, 0);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];
  vector<ll> A_sum(N + 1, 0), B_sum(M + 1, 0);

  ll a_max_index = -1;
  ll b_max_index = -1;

  for (ll i = 0; i < N; i++) {
    A_sum[i + 1] = A_sum[i] + A[i];
  }

  // cout << ans << endl;
  for (ll i = 0; i < M; i++) {
    B_sum[i + 1] = B_sum[i] + B[i];
  }

  // cout << ans << endl;
  ll time = A_sum[a_max_index];
  ll B_index = 0;
  rep(i, N + 1) {
    time = A_sum[i];
    if (time > K)
      continue;
    ll index =
        upper_bound(B_sum.begin(), B_sum.end(), K - time) - B_sum.begin() - 1;
    ans = max(ans, i + index);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

const ll INF = 1LL << 60;
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;
}
using Graph = vector<vector<int>>;

typedef pair<int, int> P;
typedef priority_queue<int, vector<int>, greater<int>> PQ;
const int MOD = 1000000007;

ll N, M, K;
// vector<ll> A, B;
queue<ll> A, B;
ll ans = 0;

void dfs(ll count, queue<ll> a, queue<ll> b, ll time) {
  if (time > K)
    return;
  if (a.empty() && b.empty()) {
    ans = max(ans, count);
    return;
  }
  queue<ll> at = a;
  ll time_a = at.front();
  at.pop();
  if (!a.empty()) {
    dfs(count + 1, at, b, time + time_a);
  }
  queue<ll> bt = b;
  ll time_b = bt.front();
  bt.pop();
  if (!b.empty()) {
    dfs(count + 1, a, bt, time + time_b);
  }
  if (!a.empty() && !b.empty()) {
    dfs(count + 2, at, bt, time + time_a + time_b);
  }

  ans = max(ans, count);
}

int main() {
  cin >> N >> M >> K;
  // A.assign(N,0);
  // B.assign(M,0);
  // rep(i,N) cin >> A[i];
  // rep(i,M) cin >> B[i];
  rep(i, N) {
    ll a;
    cin >> a;
    A.push(a);
  }
  rep(i, M) {
    ll b;
    cin >> b;
    B.push(b);
  }

  dfs(0, A, B, 0);

  cout << ans << endl;
}