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

#include <bits/stdc++.h>
using namespace std;
/*
 *  JUST KEEP GOING
 */
#define endl '\\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define For(i, s) for (auto i : s)
#define Forr(i, s) for (auto &i : s)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<ll, ll>
#define vii vector<ii>
#define vll vector<pair<ll, ll>>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)
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...);
}
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
void run() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 5;
int n, m;
ll A[N], B[N], k;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  run();
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
    A[i] += A[i - 1];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> B[i];
    B[i] += B[i - 1];
  }
  int ans = 0;
  for (int i = 0; i <= n && A[i] <= k; ++i) {
    ll rem = k - A[i];
    int curAns = i;
    int l = 0, r = m, mid, idx;
    while (l <= r) {
      mid = (l + r) / 2;
      if (B[mid] <= rem) {
        l = mid + 1;
        idx = mid;
      } else
        r = mid - 1;
    }
    curAns += idx;
    ans = max(curAns, ans);
  }

  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int book[3][200010];
int max = 0;

void read(int aIndex, int aMax, int bIndex, int bMax, int remains,
          int counter) {
  bool bisa = 0;
  if (remains >= book[1][aIndex] && aIndex <= aMax) {
    read(aIndex + 1, aMax, bIndex, bMax, remains - book[1][aIndex],
         counter + 1);
    bisa = 1;
  }
  if (remains >= book[2][bIndex] && bIndex <= bMax) {
    read(aIndex, aMax, bIndex + 1, bMax, remains - book[2][aIndex],
         counter + 1);
    bisa = 1;
  }
  if (!bisa) {
    max = std::max(max, counter);
  }
}

int main() {
  int aMax, bMax, remains;
  scanf("%d %d %d", &aMax, &bMax, &remains);
  for (int i = 1; i <= aMax; i++) {
    scanf("%d", &book[1][i]);
  }
  for (int i = 1; i <= bMax; i++) {
    scanf("%d", &book[2][i]);
  }
  read(1, aMax, 1, bMax, remains, 0);
  printf("%d\\n", max);
}