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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define INF (1LL << 60)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n) {
  ll s = sqrt(n);
  ll r = 0;
  for (ll i = 2; i <= s; i++) {
    if ((n % i) == 0) {
      r = 0;
      do {
        r++;
        n = n / i;
      } while ((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if (n > s) {
    prim.push_back({n, 1});
  }
}

vector<ll> a(200100), b(200100);
ll N, M, K;
ll bmax;

void solve() {
  cin >> N >> M >> K;
  rep(i, N) cin >> a[i];
  rep(i, M) cin >> b[i];
  bmax = 0;
  ll ai = 0, bi = 0;

  ll t = 0;
  for (ai = 0; ai < N && t + a[ai] <= K; ai++)
    t += a[ai];
  // cerr << ai << ":" << bi << ":" << t << endl;
  bmax = ai;
  while (ai >= 0) {
    while (bi < M && t + b[bi] <= K) {
      t += b[bi];
      bi++;
      chmax(bmax, ai + bi);
    }
    // cerr << ai << ":" << bi << ":" << t << endl;
    ai--;
    t -= a[ai];
  }

  cout << bmax << endl;
}

int main(void) {
  // ll t; cin >> t; rep(i, t)
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define INF (1LL << 60)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n) {
  ll s = sqrt(n);
  ll r = 0;
  for (ll i = 2; i <= s; i++) {
    if ((n % i) == 0) {
      r = 0;
      do {
        r++;
        n = n / i;
      } while ((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if (n > s) {
    prim.push_back({n, 1});
  }
}

vector<ll> a(200100), b(200100);
ll N, M, K;
ll bmax;

void rec(ll ai, ll bi, ll t) {
  //  cerr << ai << ":" << bi << ":" << t << endl;
  if (t > K)
    return;
  if (ai < N && t + a[ai] <= K) {
    chmax(bmax, ai + bi + 1);
    rec(ai + 1, bi, t + a[ai]);
  }
  if (bi < M && t + b[bi] <= K) {
    chmax(bmax, ai + bi + 1);
    rec(ai, bi + 1, t + b[bi]);
  }
}

void solve() {
  // ll N; cin >> N;
  cin >> N >> M >> K;
  // ll x, y, z; cin >> x >> y >> z;
  // ll a, b, c; cin >> a >> b >> c;
  // string s; cin >> s;
  rep(i, N) cin >> a[i];
  rep(i, M) cin >> b[i];
  bmax = 0;

  rec(0, 0, 0);

  cout << bmax << endl;
}

int main(void) {
  // ll t; cin >> t; rep(i, t)
  solve();
}