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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

typedef long long llong;
typedef unsigned long long ullong;
// -2.1e-9 <= int <= 2.1e9
// -9.2e-18 <= long long <= 9.2e18
// 0 <= unsigned int <= 4.2e9
// 0 <= unsigned long long <= 18.4e18
// cout << fixed << setprecision(10);

int main() {
  uint N, M;
  llong K;
  cin >> N >> M >> K;
  vector<int> A(N, 0);
  vector<int> B(M, 0);
  for (auto &a : A)
    cin >> a;
  for (auto &b : B)
    cin >> b;

  uint numAmax = N;
  vector<llong> SumA(N + 1, 0);
  bool setnum = false;
  for (uint i = 1; i < SumA.size(); i++) {
    SumA[i] = SumA[i - 1] + static_cast<llong>(A[i - 1]);
    if (!setnum) {
      if (SumA[i] == K) {
        numAmax = i;
        setnum = true;
      } else if (SumA[i] > K) {
        numAmax = i - 1;
        setnum = true;
      }
    }
  }

  vector<llong> SumB(M + 1, 0);
  for (uint i = 1; i < SumB.size(); i++) {
    SumB[i] = SumB[i - 1] + static_cast<llong>(B[i - 1]);
  }

  vector<uint> numRead(numAmax + 1);
  uint numReadMax = 0;
  auto begin = SumB.cbegin();
  auto end = SumB.cend();

  for (uint numA = 0; numA <= numAmax; numA++) {
    llong K2 = K - SumA[numA];
    numRead[numA] = numA;
    auto pos = lower_bound(begin, end, K2);
    int d;
    if (pos == end) {
      d = SumB.size() - 1;
    } else {
      llong thresh = *pos;
      if (thresh > K2) {
        d = distance(begin, pos) - 1;
        if (d < 0)
          d = 0;
      } else {
        d = distance(begin, pos);
      }
    }
    numRead[numA] += d;
    if (numRead[numA] > numReadMax) {
      numReadMax = numRead[numA];
    }
  }
  cout << numReadMax << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

typedef long long llong;
typedef unsigned long long ullong;
// -2.1e-9 <= int <= 2.1e9
// -9.2e-18 <= long long <= 9.2e18
// 0 <= unsigned int <= 4.2e9
// 0 <= unsigned long long <= 18.4e18
// cout << fixed << setprecision(10);

int main() {
  uint N, M;
  int K;
  cin >> N >> M >> K;
  vector<int> A(N, 0);
  vector<int> B(M, 0);
  for (auto &a : A)
    cin >> a;
  for (auto &b : B)
    cin >> b;

  uint numAmax = N;
  vector<llong> SumA(N + 1, 0);
  bool setnum = false;
  for (uint i = 1; i < SumA.size(); i++) {
    SumA[i] = SumA[i - 1] + static_cast<llong>(A[i - 1]);
    if (!setnum) {
      if (SumA[i] > K) {
        numAmax = i - 1;
        setnum = true;
      }
    }
  }

  vector<uint> numRead(numAmax + 1);
  uint numReadMax = 0;
  for (uint numA = 0; numA <= numAmax; numA++) {
    int K2 = K - SumA[numA];
    numRead[numA] = numA;
    for (uint j = 0; j < B.size(); j++) {
      if (B[j] <= K2) {
        K2 -= B[j];
        ++numRead[numA];
      } else {
        break;
      }
    }
    if (numRead[numA] > numReadMax) {
      numReadMax = numRead[numA];
    }
  }
  cout << numReadMax << endl;
  return 0;
}