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

#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <string>
#include <vector>

using namespace std;
#define MODULE 1
#define RAND(min, max) (rand() % (max - min + 1) + min)
#define LOG(log) cout << #log << " : " << log << endl;
#define DEBUG()                                                                \\
  {                                                                            \\
    cout << "DEBUG " << num_debug << endl;                                     \\
    num_debug++;                                                               \\
  }
int num_debug = 0;

int main(void) {
  srand((unsigned)time(NULL));
  // redo:
  int M, N;
  long K;
  long *A, *B;

  cin >> N >> M;
  cin >> K;
  // K = RAND(10000, 1000000000);
  // cout << K << endl;
  A = (long *)calloc(N, sizeof(long));
  B = (long *)calloc(M, sizeof(long));
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    // A[i] = RAND(1, 1000000000);
    // cout << A[i] << ' ';
  }
  // cout << endl;

  for (int i = 0; i < M; i++) {
    cin >> B[i];
    // B[i]=RAND(1, 10000000000);
    // cout << B[i] << ' ';
  }

  vector<long> at;
  vector<int> book;
  long remain = K;
  at.push_back(0);
  // DEBUG();
  // a
  for (int i = 1; i < N + 1; i++) {
    at.push_back(at[i - 1] + A[i - 1]);
    if (K < at[i]) {
      at.pop_back();
      break;
    }
  }
  // DEBUG();
  for (int i = 0; i < at.size(); i++)
    book.push_back(i);
  // b
  long bt = 0;
  int j = 0;
  for (int i = at.size() - 1; 0 <= i; i--) {
    // DEBUG();
    remain = K - at[i] - bt;
    while (1) {
      // DEBUG();
      // LOG(remain);
      // LOG(i);
      // LOG(B[i]); cout << endl;
      if (j < M) {
        remain -= B[j];
      } else {
        break;
      }
      if (remain < 0)
        break;
      bt += B[j];
      j++;
    }
    book[i] += j;
  }
  // DEBUG();

  int ans = 0;
  for (int i = 0; i < book.size(); i++) {
    if (ans < book[i])
      ans = book[i];
  }
  // DEBUG();

  cout << ans;

  free(A);
  free(B);

  // goto redo;
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <string>
#include <vector>

using namespace std;
#define MODULE 1
#define RAND(min, max) (rand() % (max - min + 1) + min)
#define LOG(log) cout << #log << " : " << log << endl;
#define DEBUG()                                                                \\
  {                                                                            \\
    cout << "DEBUG " << num_debug << endl;                                     \\
    num_debug++;                                                               \\
  }
int num_debug = 0;

int main(void) {
  srand((unsigned)time(NULL));
  // redo:
  int M, N;
  long K;
  long *A, *B;

  cin >> N >> M;
  cin >> K;
  // K = RAND(10000, 1000000000);
  // cout << K << endl;
  A = (long *)calloc(N, sizeof(long));
  B = (long *)calloc(M, sizeof(long));
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    // A[i] = RAND(1, 1000000000);
    // cout << A[i] << ' ';
  }
  // cout << endl;

  for (int i = 0; i < M; i++) {
    cin >> B[i];
    // B[i]=RAND(1, 10000000000);
    // cout << B[i] << ' ';
  }

  vector<long> v;
  vector<int> book;
  long remain = K;
  v.push_back(0);
  // DEBUG();
  // a
  for (int i = 1; i < N + 1; i++) {
    v.push_back(v[i - 1] + A[i - 1]);
    if (K < v[i]) {
      v.pop_back();
      break;
    }
  }
  // DEBUG();
  // b
  for (int i = 0; i < v.size(); i++) {
    // DEBUG();
    book.push_back(i);
    int j = 0;
    remain = K - v[i];
    while (1) {
      // DEBUG();
      // LOG(remain);
      // LOG(i);
      // LOG(B[i]);
      if (j < M) {
        remain -= B[j];
      }
      if (remain < 0 || M <= j)
        break;
      j++;
    }
    book[i] += j;
  }
  // DEBUG();

  int ans = 0;
  for (int i = 0; i < book.size(); i++) {
    if (ans < book[i])
      ans = book[i];
  }
  // DEBUG();

  cout << ans;

  free(A);
  free(B);

  // goto redo;
  return 0;
}