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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> tup;
const int MAX = 3000000;

long long fac[MAX], finv[MAX], inv[MAX];

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

map<ll, ll> prime; // 素因数分解でそれぞれの素数がいくつ出てきたかを保存するmap

// O(√n)
// 整列済み(mapはkeyで自動で整列される)
void prime_factorize(ll n) {
  if (n <= 1)
    return;
  ll l = sqrt(n);
  for (ll i = 2; i <= (ll)(l); i++) { //
    if (n % i == 0) {
      prime_factorize(i);
      prime_factorize(ll(n / i));
      return;
    }
  }
  // mapでは存在しないkeyの場合も自動で構築される
  prime[n]++;
  return;
}

vector<ll> A, B, S, T;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;

  A.assign(N, 0);
  S.assign(N + 1, 0);
  B.assign(M, 0);
  T.assign(M + 1, 0);
  ll thresh_A = -1, thresh_B = -1;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    S[i + 1] = S[i] + A[i];
    if (S[i + 1] > K && thresh_A == -1)
      thresh_A = i;
  }
  if (thresh_A == -1)
    thresh_A = N;
  for (ll i = 0; i < M; i++) {
    cin >> B[i];
    T[i + 1] = T[i] + B[i];
    if (S[thresh_A] + T[i + 1] > K && thresh_B == -1)
      thresh_B = i;
  }
  if (thresh_B == -1)
    thresh_B = M;

  // cout << thresh_A << "," << thresh_B << endl;

  ll count = 0;
  // A取る冊数
  for (ll i = thresh_A; i >= 0; i--) {
    for (ll j = thresh_B; j <= M; j++) {
      if (S[i] + T[j] <= K)
        thresh_B = j;
      if (S[i] + T[j] > K)
        break;
      count = max(count, i + j);
    }
  }

  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> tup;
const int MAX = 3000000;

long long fac[MAX], finv[MAX], inv[MAX];

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

map<ll, ll> prime; // 素因数分解でそれぞれの素数がいくつ出てきたかを保存するmap

// O(√n)
// 整列済み(mapはkeyで自動で整列される)
void prime_factorize(ll n) {
  if (n <= 1)
    return;
  ll l = sqrt(n);
  for (ll i = 2; i <= (ll)(l); i++) { //
    if (n % i == 0) {
      prime_factorize(i);
      prime_factorize(ll(n / i));
      return;
    }
  }
  // mapでは存在しないkeyの場合も自動で構築される
  prime[n]++;
  return;
}

vector<ll> A, B, S, T;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;

  A.assign(N, 0);
  S.assign(N + 1, 0);
  B.assign(M, 0);
  T.assign(M + 1, 0);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    S[i + 1] = S[i] + A[i];
  }
  for (ll i = 0; i < M; i++) {
    cin >> B[i];
    T[i + 1] = T[i] + B[i];
  }

  ll count = 0;
  // A取る冊数
  for (ll i = 0; i <= N; i++) {
    if (S[i] > K)
      break;
    for (ll j = 0; j <= M; j++) {
      if (S[i] + T[j] > K)
        break;
      count = max(count, i + j);
    }
  }

  cout << count << endl;
  return 0;
}