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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
#define _GLIBCXX_DEBUG
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// 約数列挙(√N)
vector<int64_t> divisor(int64_t n) {
  vector<int64_t> ret;
  for (int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}
// 素因数分解(√N)
map<int64_t, int> prime_factor(int64_t n) {
  map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}
// 素数テーブル(NloglogN)
vector<bool> prime_table(int n) {
  vector<bool> prime(n + 1, true);
  if (n >= 0)
    prime[0] = false;
  if (n >= 1)
    prime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i) {
      prime[j] = false;
    }
  }
  return prime;
}
// 二項係数(K)
template <typename T> T binomial(int64_t N, int64_t K) {
  if (K < 0 || N < K)
    return 0;
  T ret = 1;
  for (T i = 1; i <= K; ++i) {
    ret *= N--;
    ret /= i;
  }
  return ret;
}
// 二項係数テーブル(N^2)
template <typename T> vector<vector<T>> binomial_table(int N) {
  vector<vector<T>> mat(N + 1, vector<T>(N + 1));
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == 0 || j == i)
        mat[i][j] = 1;
      else
        mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
    }
  }
  return mat;
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> sa(n + 1), sb(m + 1), a(n), b(m);
  rep(x, n) cin >> a.at(x);
  rep(x, m) cin >> b.at(x);
  rep(i, n) {
    sa.at(i + 1) += sa.at(i);
    sa.at(i + 1) += a.at(i);
  }
  rep(i, m) {
    sb.at(i + 1) += sb.at(i);
    sb.at(i + 1) += b.at(i);
  }
  int ans = 0;
  rep(i, n + 1) {
    int h = lower_bound(all(sb), k - sa.at(i) + 1) - sb.begin();
    if (h == 0)
      continue;
    ans = max(ans, h + i - 1);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
#define _GLIBCXX_DEBUG
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// 約数列挙(√N)
vector<int64_t> divisor(int64_t n) {
  vector<int64_t> ret;
  for (int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}
// 素因数分解(√N)
map<int64_t, int> prime_factor(int64_t n) {
  map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}
// 素数テーブル(NloglogN)
vector<bool> prime_table(int n) {
  vector<bool> prime(n + 1, true);
  if (n >= 0)
    prime[0] = false;
  if (n >= 1)
    prime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i) {
      prime[j] = false;
    }
  }
  return prime;
}
// 二項係数(K)
template <typename T> T binomial(int64_t N, int64_t K) {
  if (K < 0 || N < K)
    return 0;
  T ret = 1;
  for (T i = 1; i <= K; ++i) {
    ret *= N--;
    ret /= i;
  }
  return ret;
}
// 二項係数テーブル(N^2)
template <typename T> vector<vector<T>> binomial_table(int N) {
  vector<vector<T>> mat(N + 1, vector<T>(N + 1));
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == 0 || j == i)
        mat[i][j] = 1;
      else
        mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
    }
  }
  return mat;
}
int dfs(vector<int> &a, vector<int> &b, int k, int i, int j) {
  if (k < a.at(i) && k < b.at(j))
    return i + j;
  if (k >= a.at(i) && k >= b.at(j))
    return max(dfs(a, b, k - a.at(i), i + 1, j),
               dfs(a, b, k - b.at(j), i, j + 1));
  if (k >= a.at(i) && k < b.at(j))
    return dfs(a, b, k - a.at(i), i + 1, j);
  return dfs(a, b, k - b.at(j), i, j + 1);
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n + 1), b(m + 1);
  rep(x, n) cin >> a.at(x);
  a.at(n) = 2000000000;
  rep(x, m) cin >> b.at(x);
  b.at(m) = 2000000000;
  cout << dfs(a, b, k, 0, 0) << endl;
}