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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
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;
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

const ll INF = 1LL << 60;
const ll MOD = 1000000007;

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

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

  vector<ll> A(N + 1);
  vector<ll> B(M + 1);
  for (ll i = 0; i < N; ++i)
    cin >> A[i + 1];
  for (ll i = 0; i < M; ++i)
    cin >> B[i + 1];

  ll pA = 0;
  ll pB = 0;

  ll kA = 0;
  ll kB = 0;

  for (pB = 0; pB <= M; ++pB) {
    kB += B[pB];
    if (kB > K) {
      kB -= B[pB];
      pB--;
      break;
    }
  }

  pB = max(pB, 0LL);
  pB = min(pB, M);

  ll ans = 0;
  // ll ans = pB + 1;

  for (pA = 0; pA <= N; ++pA) {
    kA += A[pA];

    while (kA + kB > K) {
      if (pB <= 0)
        break;

      kB -= B[pB];
      pB--;
    }

    if (pB <= 0 && kA + kB > K) {
      kA -= B[pA];
      pA--;

      break;
    }

    ans = max(ans, min(max(pA, 0LL), N) + min(max(pB, 0LL), M));
  }

  ans = max(ans, min(max(pA, 0LL), N) + min(max(pB, 0LL), M));

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
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;
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

const ll INF = 1LL << 60;
const ll MOD = 1000000007;

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

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

  vector<ll> A(N);
  vector<ll> B(M);
  for (ll i = 0; i < N; ++i)
    cin >> A[i];
  for (ll i = 0; i < M; ++i)
    cin >> B[i];

  vector<ll> rA(N + 1, 0);
  vector<ll> rB(M + 1, 0);

  ll tmp = 0;
  for (ll i = 1; i <= N; ++i) {
    tmp += A[i - 1];
    rA[i] = tmp;
  }

  tmp = 0;
  for (ll i = 1; i <= M; ++i) {
    tmp += B[i - 1];
    rB[i] = tmp;
  }

  ll ans = 0;

  for (ll i = 0; i <= N; ++i) {
    ll t_ans = 0;
    ll k = 0;

    if (rA[i] <= K) {
      t_ans = i;
      k = rA[i];
    }

    ll room = K - k;

    for (ll j = 0; j <= M; ++j) {
      ll jj = M - j;
      if (rB[jj] <= room) {
        t_ans += jj;
        break;
      }
    }
    // cout << room << " " << i << " " << lb - rB.begin() << endl;

    ans = max(ans, t_ans);
  }

  cout << ans << endl;
}