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

#include <bits/stdc++.h>
#define IOS                                                                    \\
  {                                                                            \\
    ios::sync_with_stdio(0);                                                   \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define endl "\\n"
#define int long long int
#define f(i, a, b) for (int i = a; i < b; i++)
#define fb(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define S(x) (int)x.size()
#define LB lower_bound
#define UB upper_bound
#define fp(x, y) fixed << setprecision(y) << x
#define PI 3.141592653589793238462643383279502884
using namespace std;

const int M = (int)2e5 + 5;
const int mod = (int)1e9 + 7;

int n, m, k, ans = 0;
int a[M], b[M];
int prea[M], preb[M];

void solve() {
  cin >> n >> m >> k;
  f(i, 0, n) cin >> a[i];
  f(i, 0, m) cin >> b[i];
  f(i, 0, n) prea[i + 1] = prea[i] + a[i];
  f(i, 0, m) preb[i + 1] = preb[i] + b[i];
  f(i, 0, n + 1) {
    if (prea[i] > k)
      break;
    ans = max(ans, i + (int)(UB(preb, preb + m + 1, k - prea[i]) - preb) - 1);
  }
  cout << ans << endl;
}

int32_t main() {
  IOS int t = 1;
  // cin>>t;
  f(tno, 1, t + 1) { solve(); }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  {                                                                            \\
    ios::sync_with_stdio(0);                                                   \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define endl "\\n"
#define int long long int
#define f(i, a, b) for (int i = a; i < b; i++)
#define fb(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define S(x) (int)x.size()
#define LB lower_bound
#define UB upper_bound
#define fp(x, y) fixed << setprecision(y) << x
#define PI 3.141592653589793238462643383279502884
using namespace std;

const int M = (int)2e5 + 5;
const int mod = (int)1e9 + 7;

int n, m, k;
int a[M], b[M];
map<tuple<int, int, int>, int> mp;

int recur(int k, int i, int j) {
  if (mp.find({k, i, j}) != mp.end())
    return mp[{k, i, j}];
  int x = 0, y = 0;
  if (k < a[i] && k < b[j])
    return 0;
  if (i < n && k >= a[i]) {
    if (mp.find({k - a[i], i + 1, j}) != mp.end())
      x = mp[{k - a[i], i + 1, j}];
    else {
      x = recur(k - a[i], i + 1, j) + 1;
      mp[{k - a[i], i + 1, j}] = x;
    }
  }
  if (j < m && k >= b[j]) {
    if (mp.find({k - b[j], i, j + 1}) != mp.end())
      y = mp[{k - b[j], i, j + 1}];
    else {
      y = recur(k - b[j], i, j + 1) + 1;
      mp[{k - b[j], i, j + 1}] = y;
    }
  }
  return max(x, y);
}

void solve() {
  cin >> n >> m >> k;
  f(i, 0, n) cin >> a[i];
  f(i, 0, m) cin >> b[i];
  cout << recur(k, 0, 0) << endl;
}

int32_t main() {
  IOS int t = 1;
  // cin>>t;
  f(tno, 1, t + 1) { solve(); }
  return 0;
}