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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)
#define REV(X, Y, Z) for (ll(X) = (Y); (X) > (Z); (X)--)

using namespace std;
using ld = long double;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMax = 1e12 + 1;
constexpr ll kMod = 1e9 + 7;
constexpr long double kPI = 3.14159265358979323846;

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

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

  vll A(N + 1, 0);
  ll asum = 0;
  REP(i, 0, N) {
    ll a;
    cin >> a;
    A[i + 1] = a + asum;
    asum = A[i + 1];
  }
  vll B(M + 1, 0);
  ll bsum = 0;
  REP(i, 0, M) {
    ll b;
    cin >> b;
    B[i + 1] = b + bsum;
    bsum = B[i + 1];
  }

  ll ans = 0, j = M;
  REP(i, 0, N + 1) {
    if (A[i] > K) {
      break;
    }

    while (B[j] > K - A[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)
#define REV(X, Y, Z) for (ll(X) = (Y); (X) > (Z); (X)--)

using namespace std;
using ld = long double;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMax = 1e12 + 1;
constexpr ll kMod = 1e9 + 7;
constexpr long double kPI = 3.14159265358979323846;

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

ll s3(ll k, deque<ll> a, deque<ll> b) {
  if (!a.empty() && k >= a[0]) {
    deque<ll> dq = a;
    dq.pop_front();
    return 1 + s3(k - a[0], dq, b);
  }

  if (!b.empty() && k >= b[0]) {
    deque<ll> dq = b;
    dq.pop_front();
    return 1 + s3(k - b[0], a, dq);
  }

  return 0;
}

ll s2(ll k, deque<ll> a, deque<ll> b) {
  if (k >= a[0]) {
    deque<ll> dq = a;
    dq.pop_front();
    return 1 + s3(k - a[0], dq, b);
  }

  if (k >= b[0]) {
    deque<ll> dq = b;
    dq.pop_front();
    return 1 + s3(k - b[0], a, dq);
  }

  return 0;
}

ll search(ll k, deque<ll> a, deque<ll> b) {
  if (a.empty() && b.empty()) {
    return 0;
  }

  ll ans = 0;
  if (a.empty()) {
    while (!b.empty() && k >= b[0]) {
      ans++;
      k -= b[0];
      b.pop_front();
    }
    return ans;
  }
  if (b.empty()) {
    while (!a.empty() && k >= a[0]) {
      ans++;
      k -= a[0];
      a.pop_front();
    }
    return ans;
  }

  return s2(k, a, b);
}

int main() {
  init();

  ll N, M, K;
  cin >> N >> M >> K;
  deque<ll> A(N);
  ll asum = 0;
  REP(i, 0, N) {
    cin >> A[i];
    asum += A[i];
  }
  deque<ll> B(M);
  ll bsum = 0;
  REP(i, 0, M) {
    cin >> B[i];
    bsum += B[i];
  }

  cout << search(K, A, B) << endl;
}