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

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

#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
#define ldb long double
#define db double
#define int long long

#define fr first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mpr make_pair

#define pi pair<int, int>
#define pdb pair<db, db>
#define pldb pair<ldb, ldb>

#define vi vector<int>
#define vdb vector<db>
#define vldb vector<ldb>
#define vpi vector<pi>

#define mi map<int, int>
#define mdb map<db, db>
#define mldb map<ldb, ldb>

#define forr(a, b) for (int i = (a); i < (b); ++i)
#define rrof(a, b) for (int i = (b)-1; i >= (a); --i)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

const int md = 1e9 + 7;
const int MX = 2e5 + 5;
const int INF = 1e18;
const ldb PI = 4 * atan((ldb)1);

int power_md(int a, int n) {
  int res = 1;
  while (n) {
    if (n % 2)
      res = (res % md * a % md) % md;
    a = (a % md * a % md) % md;
    n /= 2;
  }
  res %= md;
  return res;
}

int power(int a, int n) {
  int res = 1;
  while (n) {
    if (n % 2)
      res *= a;
    a = a * a;
    n /= 2;
  }
  return res;
}

int abst(int a) { return ((a < 0) ? (-1 * a) : (a)); }

class cmp_set {
public:
  bool operator()(int a, int b) { return a > b; }
};

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

/*int ter_search(int l,int r)
{
    while((r-l)>0)
    {
        int m1=l+(r-l)/3;
        int m2=r-(r-l)/3;
        int f1=f(m1);
        int f2=f(m2);
        if(f1>f2)
         l=m1+1;
         else
          r=m2-1;
    }
    return f(l);
}*/

int32_t main() {
  fast;
  int n, m, k;
  cin >> n >> m >> k;
  int a[n + 1], b[m + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
  }
  int prefa[n + 1], prefb[m + 1];
  prefa[0] = prefb[0] = 0;
  for (int i = 1; i <= n; i++) {
    prefa[i] = prefa[i - 1] + a[i];
  }
  for (int i = 1; i <= m; i++) {
    prefb[i] = prefb[i - 1] + b[i];
  }
  int r = m;
  int res = 0;
  for (int l = 0; l <= n; l++) {
    if (prefa[l] > k)
      break;
    while ((k - prefa[l]) < prefb[r])
      r--;
    res = max(res, l + r);
  }
  cout << res << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

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

#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
#define ldb long double
#define db double
#define int long long

#define fr first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mpr make_pair

#define pi pair<int, int>
#define pdb pair<db, db>
#define pldb pair<ldb, ldb>

#define vi vector<int>
#define vdb vector<db>
#define vldb vector<ldb>
#define vpi vector<pi>

#define mi map<int, int>
#define mdb map<db, db>
#define mldb map<ldb, ldb>

#define forr(a, b) for (int i = (a); i < (b); ++i)
#define rrof(a, b) for (int i = (b)-1; i >= (a); --i)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

const int md = 1e9 + 7;
const int MX = 2e5 + 5;
const int INF = 1e18;
const ldb PI = 4 * atan((ldb)1);

int power_md(int a, int n) {
  int res = 1;
  while (n) {
    if (n % 2)
      res = (res % md * a % md) % md;
    a = (a % md * a % md) % md;
    n /= 2;
  }
  res %= md;
  return res;
}

int power(int a, int n) {
  int res = 1;
  while (n) {
    if (n % 2)
      res *= a;
    a = a * a;
    n /= 2;
  }
  return res;
}

int abst(int a) { return ((a < 0) ? (-1 * a) : (a)); }

class cmp_set {
public:
  bool operator()(int a, int b) { return a > b; }
};

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

/*int ter_search(int l,int r)
{
    while((r-l)>0)
    {
        int m1=l+(r-l)/3;
        int m2=r-(r-l)/3;
        int f1=f(m1);
        int f2=f(m2);
        if(f1>f2)
         l=m1+1;
         else
          r=m2-1;
    }
    return f(l);
}*/

int n, m, k;
vi a, b;

int fun(int i, int j, int rem) {
  if ((i == n) && (j == m))
    return 0;
  if (k <= 0)
    return 0;
  int p1 = 0, p2 = 0;
  if (i < n && a[i] <= rem)
    p1 = fun(i + 1, j, rem - a[i]) + 1;
  if (j < m && b[j] <= rem)
    p2 = fun(i, j + 1, rem - b[j]) + 1;
  return max(p1, p2);
}

int32_t main() {
  fast;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a.pb(x);
  }
  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    b.pb(x);
  }
  int res = fun(0, 0, k);
  cout << res << "\\n";
  return 0;
}