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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
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;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, M, K;
  cin >> N >> M >> K;

  vl a(N), b(M);
  REP(i, N) cin >> a[i];
  REP(i, M) cin >> b[i];

  vl ca(N + 1);
  vl cb(M + 1);
  ca[0] = 0;
  cb[0] = 0;
  REP(i, N) ca[i + 1] = ca[i] + a[i];
  REP(i, M) cb[i + 1] = cb[i] + b[i];

  int ans = 0;
  REP(i, N + 1) if (ca[i] <= K) {
    int j = distance(cb.begin(), upper_bound(ALL(cb), K - ca[i]));
    ans = max(ans, i + j - 1);
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
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;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int N, M, K;
vi dp(400001, -1);
vi a, b;

void dfs(int ai, int bi, int r) {
  if (r > K)
    return;

  if (dp[ai + bi] == -1) {
    dp[ai + bi] = r;
  } else {
    dp[ai + bi] = min(dp[ai + bi], r);
  }

  if (ai < N) {
    dfs(ai + 1, bi, r + a[ai]);
  }
  if (bi < M) {
    dfs(ai, bi + 1, r + b[bi]);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M >> K;
  a.resize(N);
  b.resize(M);

  REP(i, N) cin >> a[i];
  REP(i, M) cin >> b[i];

  dfs(0, 0, 0);
  // REP(i, 10) {
  //     cout << i << ": " << dp[i] << endl;
  // }

  ll ans = 0;
  REP(i, 400001) {
    if (dp[i] == -1) {
      ans = i - 1;
      break;
    }
  }

  cout << ans << endl;
  return 0;
}