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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\\n";

using namespace std;

template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
  for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
    out << v[i] << " ";
  out << "\\n";
  return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << "(" << v[i].first << ", " << v[i].second << ") ";
  out << "\\n";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    for (size_t j = 0; j < v[i].size(); ++j) {
      out << v[i][j] << " ";
    }
    out << "\\n";
  }
  return out;
}

int n, m, k;
vector<int> a, b;

int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> k;

  a = vector<int>(n);
  b = vector<int>(m);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  for (int i = 0; i < m; ++i) {
    cin >> b[i];
  }

  int i = 0;
  long long curr_time = 0;
  int books = 0;
  while (i < n) {
    curr_time += a[i];

    if (curr_time > k) {
      curr_time -= a[i];
      break;
    }

    ++i;
    ++books;
  }

  int ans = books;
  int j = 0;
  --i;
  while (j < m) {
    if (curr_time + b[j] <= k) {
      curr_time += b[j];
      ++j;
      ++books;
    } else if (i >= 0) {
      curr_time -= a[i];
      --i;
      --books;
    } else {
      break;
    }

    ans = max(ans, books);
  }

  cout << ans << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\\n";

using namespace std;

template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
  for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
    out << v[i] << " ";
  out << "\\n";
  return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << "(" << v[i].first << ", " << v[i].second << ") ";
  out << "\\n";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    for (size_t j = 0; j < v[i].size(); ++j) {
      out << v[i][j] << " ";
    }
    out << "\\n";
  }
  return out;
}

int n, m, k;
vector<int> a, b;

int max_books(int i, int j, int rem_k) {
  int ans = 0;
  if (i < n && j < m && rem_k >= min(a[i], b[j])) {
    ans += max(max_books(i + 1, j, rem_k - a[i]),
               max_books(i, j + 1, rem_k - b[j])) +
           1;
  } else if (i < n && rem_k >= a[i]) {
    ans += max_books(i + 1, j, rem_k - a[i]) + 1;
  } else if (j < m && rem_k >= b[j]) {
    ans += max_books(i, j + 1, rem_k - b[j]) + 1;
  }

  return ans;
}

int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> k;

  a = vector<int>(n);
  b = vector<int>(m);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  for (int i = 0; i < m; ++i) {
    cin >> b[i];
  }

  cout << max_books(0, 0, k) << "\\n";

  return 0;
}