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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long int ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < signed(n); i++)
#define EREP(i, n) for (int i = 1; i <= signed(n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
// #define EVEL 1
#ifdef EVEL
#define DEB(X) cout << #X << ":" << X << " ";
#define TF(f) f ? cout << "true  " : cout << "false ";
#define END cout << "\\n";
#else
#define DEB(X)                                                                 \\
  {}
#define TF(f)                                                                  \\
  {}
#define END                                                                    \\
  {}
#endif
const int MOD = 1000000007;
const ll INF = 5000000000000;
// typedef std::pair<int, int> P;
struct edge {
  int to;
  double cost;
};
#define VMAX 1010

ll N, M, K;
ll A[300000], B[300000], C;
std::vector<ll> G;
bool D[1000];
ll P[11];
ll ans;

int main() {
  cin >> N >> M >> K;
  REP(i, N) { cin >> A[i]; }
  REP(i, M) { cin >> B[i]; }
  REP(i, N) { A[i + 1] += A[i]; }
  REP(i, M) { B[i + 1] += B[i]; }
  REP(i, M) { G.push_back(B[i]); }
  auto itr = std::upper_bound(ALL(G), K);
  ans = max(0LL, ll(itr - G.begin()));
  REP(i, N) {
    DEB(i) DEB(A[i]) DEB(K - A[i]) if (A[i] > K) break;
    auto it = std::upper_bound(ALL(G), K - A[i]);
    DEB(it - G.begin()) END ans = max(ans, ll(it - G.begin()) + i + 1);
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long int ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < signed(n); i++)
#define EREP(i, n) for (int i = 1; i <= signed(n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
// #define EVEL 1
#ifdef EVEL
#define DEB(X) cout << #X << ":" << X << " ";
#define TF(f) f ? cout << "true  " : cout << "false ";
#define END cout << "\\n";
#else
#define DEB(X)                                                                 \\
  {}
#define TF(f)                                                                  \\
  {}
#define END                                                                    \\
  {}
#endif
const int MOD = 1000000007;
const ll INF = 5000000000000;
// typedef std::pair<int, int> P;
struct edge {
  int to;
  double cost;
};
#define VMAX 1010

ll N, M, K;
ll A[300000], B[300000], C;
bool D[1000];
ll P[11];
ll ans;

void DFS(ll x, ll y, ll z, ll d) {
  DEB(x)
  DEB(y) DEB(d) END

      if (z > K) {
    ans = max(ans, d - 1);
    return;
  }

  if (x + 1 <= N)
    DFS(x + 1, y, z + A[x], d + 1);
  else {
    ans = max(ans, d);
    return;
  }
  if (y + 1 <= M)
    DFS(x, y + 1, z + B[y], d + 1);
  else {
    ans = max(ans, d);
    return;
  }
  return;
  /*if(A[x]==B[y]){
      if(z+A[x]<=K&&x+1<N){
          DFS(x+1,y,z+A[x],d+1);
      }else {ans=max(d,ans);return;}
      if(z+B[y]<=K&&y+1<M){
          DFS(x,y+1,z+B[y],d+1);
      }else {ans=max(d,ans);return;}
  }else if(A[x]<B[y]){
      if(z+A[x]<=K&&x+1<N){
          DFS(x+1,y,z+A[x],d+1);
      }else {ans=max(d,ans);return;}
  }else{
      if(z+B[y]<=K&&y+1<M){
          DFS(x,y+1,z+B[y],d+1);
      }else {ans=max(d,ans);return;}
  }*/
}

int main() {
  cin >> N >> M >> K;
  REP(i, N) { cin >> A[i]; }
  REP(i, M) { cin >> B[i]; }
  DFS(0, 0, 0, 0);
  cout << ans << endl;
  return 0;
}