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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD 998244353
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, ll> &firstElof,
                       const pair<double, ll> &secondElof) {
  return firstElof.second < secondElof.second;
}
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll n, m, k, ans = 0;
ll a[210000], b[210000];

int main() {
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  ll a[n], b[m];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < m; i++)
    cin >> b[i];
  vector<ll> ruix(n + 2);
  vector<ll> ruiy(m + 2);
  ruix[0] = 0;
  ruiy[0] = 0;
  ruix[n + 1] = INF;
  ruiy[m + 1] = INF;
  for (ll i = 0; i < n; i++) {
    ruix[i + 1] = ruix[i] + a[i];
  }
  for (ll i = 0; i < m; i++) {
    ruiy[i + 1] = ruiy[i] + b[i];
  }
  for (ll i = 0; i <= n; i++) {
    if (k - ruix[i] < 0)
      continue;
    auto ite = upper_bound(ruiy.begin(), ruiy.end(), k - ruix[i]);
    ll p = ite - ruiy.begin();
    ans = max(ans, p + i - 1);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD 998244353
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, ll> &firstElof,
                       const pair<double, ll> &secondElof) {
  return firstElof.second < secondElof.second;
}
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll n, m, k, ans = 0;
ll a[210000], b[210000];
void dfs(ll x, ll y, ll k, ll c) {
  if (k < 0) {
    ans = max(c - 1, ans);
    return;
  }
  if (x < n) {
    dfs(x + 1, y, k - a[x], c + 1);
  }
  if (y < m) {
    dfs(x, y + 1, k - b[y], c + 1);
  }
  if (y == m and x == n) {
    ans = max(c, ans);
  }
}

int main() {
  cin >> n >> m >> k;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < m; i++)
    cin >> b[i];
  dfs(0, 0, k, 0);
  cout << ans << endl;
  return 0;
}