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

/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define reset(x) memset(x, -1, sizeof(x))
#define CheckBit(a, b) (a & (1ll << b))
#define SetBit(a, b) a = (a | (1ll << b))
#define UnSetBit(a, b) a = (a & (~(1ll << b)))
#define maxx 1000006
#define PI 2 * acos(0.0)
const long long INF = 2000000000000000000LL; // 2e18
const int inf = 0x3f3f3f3f;                  // 1061109567
const long double EPS = 1e-9;
bitset<maxx / 2> vis;
vector<ll> prime;
void sieve() {
  ll x = maxx / 2, y = sqrt(maxx) / 2;
  for (ll i = 1; i <= y; i++) {
    if (vis[i] == 0) {
      for (ll j = (i * (i + 1) * 2); j < x; j += (2 * i) + 1)
        vis[j] = 1;
    }
  }
  prime.push_back(2);
  for (ll i = 3; i < maxx; i += 2)
    if (vis[i / 2] == 0)
      prime.push_back(i);
}
bool isprime(ll a) {
  if (a == 2) {
    return true;
  }
  if (a % 2 == 0) {
    return false;
  }
  for (ll i = 0; i < prime.size() && prime[i] * prime[i] <= a; i++) {
    if (a % prime[i] == 0) {
      return false;
    }
  }
  return true;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
ll gcd(ll a, ll b) { return __gcd(a, b); }
ll arr[200005], arr2[200005];
ll n, m, t;
void Dufaux() {
  cin >> n >> m >> t;
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i];
    arr[i] += arr[i - 1];
  }
  for (ll i = 1; i <= m; i++) {
    cin >> arr2[i];
    arr2[i] += arr2[i - 1];
  }
  ll z = 0;
  for (int i = 0; i <= n; i++) {
    ll c = t - arr[i];
    if (c < 0)
      continue;
    ll x = upper_bound(arr2, arr2 + m + 1, c) - arr2;
    z = max(z, i + x - 1);
  }
  cout << z << endl;
}
int main() {
  FastIO;
  /// sieve();
  int test = 1;
  while (test--) {
    Dufaux();
  }
  return 0;
}


Error: TLE

Fixed submission:

/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define reset(x) memset(x, -1, sizeof(x))
#define CheckBit(a, b) (a & (1ll << b))
#define SetBit(a, b) a = (a | (1ll << b))
#define UnSetBit(a, b) a = (a & (~(1ll << b)))
#define maxx 1000006
#define PI 2 * acos(0.0)
const long long INF = 2000000000000000000LL; // 2e18
const int inf = 0x3f3f3f3f;                  // 1061109567
const long double EPS = 1e-9;
bitset<maxx / 2> vis;
vector<ll> prime;
void sieve() {
  ll x = maxx / 2, y = sqrt(maxx) / 2;
  for (ll i = 1; i <= y; i++) {
    if (vis[i] == 0) {
      for (ll j = (i * (i + 1) * 2); j < x; j += (2 * i) + 1)
        vis[j] = 1;
    }
  }
  prime.push_back(2);
  for (ll i = 3; i < maxx; i += 2)
    if (vis[i / 2] == 0)
      prime.push_back(i);
}
bool isprime(ll a) {
  if (a == 2) {
    return true;
  }
  if (a % 2 == 0) {
    return false;
  }
  for (ll i = 0; i < prime.size() && prime[i] * prime[i] <= a; i++) {
    if (a % prime[i] == 0) {
      return false;
    }
  }
  return true;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
ll gcd(ll a, ll b) { return __gcd(a, b); }
ll arr[200005], arr2[200005];
map<pll, ll> maps;
vector<ll> vec1;
ll n, m, t;
void dfs(ll i, ll j, ll sum, ll t, ll cnt) {
  if (i == n && j == m) {
    vec1.pb(cnt);
    return;
  }
  if (i >= n && arr2[j] + sum > t) {
    vec1.pb(cnt);
    return;
  }
  if (maps.find(mp(i, j)) != maps.end()) {
    return;
  }
  maps[{i, j}] = 1;
  if (j >= m && arr[i] + sum > t) {
    vec1.pb(cnt);
    return;
  }
  if (sum + arr[i] > t && sum + arr2[j] > t) {
    vec1.pb(cnt);
    return;
  }
  if (i < n && j < m) {
    if (sum + arr[i] <= t)
      dfs(i + 1, j, sum + arr[i], t, cnt + 1);
    if (sum + arr2[j] <= t)
      dfs(i, j + 1, sum + arr2[j], t, cnt + 1);
  } else {
    if (i >= n && j < m) {
      if (sum + arr2[j] <= t)
        dfs(i, j + 1, sum + arr2[j], t, cnt + 1);
    }
    if (j >= m && i < n) {
      if (sum + arr[i] <= t)
        dfs(i + 1, j, sum + arr[i], t, cnt + 1);
    }
  }
}
void Dufaux() {

  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> arr2[i];
  }
  dfs(0, 0, 0, t, 0);
  cout << *max_element(all(vec1)) << endl;
}
int main() {
  FastIO;
  /// sieve();
  int test = 1;
  while (test--) {
    Dufaux();
  }
  return 0;
}