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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

const ll LINF = 2e18;
const int INF = 1e9 + 7;

void solve(ll N, ll M, ll K, std::vector<ll> A, std::vector<ll> B) {
  vector<ll> accA(N + 1, INF);
  vector<ll> accB(M + 1, INF);

  ll sum = 0;
  accA.at(0) = 0;
  FOR(i, 1, N + 1) {
    sum += A.at(i - 1);
    if (sum > K)
      break;
    accA.at(i) = sum;
  }
  sum = 0;
  accB.at(0) = 0;
  FOR(i, 1, M + 1) {
    sum += B.at(i - 1);
    if (sum > K)
      break;
    accB.at(i) = sum;
  }

  ll mx = 0;

  FOR(i, 0, N + 1) {
    if (accA.at(i) > K) {
      break;
    }
    auto idx = upper_bound(accB.begin(), accB.end(), K - accA.at(i)) -
               accB.begin() - 1;
    if (accB.at(idx) == INF)
      continue;
    mx = std::max(mx, (ll)i + idx);
  }

  cout << mx << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  scanf("%lld", &N);
  ll M;
  scanf("%lld", &M);
  ll K;
  scanf("%lld", &K);
  std::vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  std::vector<ll> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &B[i]);
  }
  solve(N, M, K, std::move(A), std::move(B));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

void solve(ll N, ll M, ll K, std::vector<ll> A, std::vector<ll> B) {
  vector<ll> accA(N + 1);
  vector<ll> accB(M + 1);

  ll sum = 0;
  FOR(i, 1, N + 1) {
    sum += A.at(i - 1);
    accA.at(i) = sum;
  }
  sum = 0;
  FOR(i, 1, M + 1) {
    sum += B.at(i - 1);
    accB.at(i) = sum;
  }

  ll mx = 0;

  FOR(i, 0, N + 1) {
    if (accA.at(i) > K) {
      break;
    }
    FOR(j, 0, M + 1) {
      ll v = accA.at(i) + accB.at(j);
      if (v <= K) {
        mx = std::max(mx, (ll)i + j);
      } else {
        break;
      }
    }
  }

  cout << mx << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll N;
  scanf("%lld", &N);
  ll M;
  scanf("%lld", &M);
  ll K;
  scanf("%lld", &K);
  std::vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  std::vector<ll> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &B[i]);
  }
  solve(N, M, K, std::move(A), std::move(B));
  return 0;
}