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

// Tsundoku
#include <algorithm>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int INF = 1 << 29;

template <class T> bool isOK(vector<T> a, int index, T key) {
  // ここの比較でupper boundかlower bound か決まる
  if (a[index] > key) {
    return true;
  } else {
    return false;
  }
}

template <class T> T binarysearch(vector<T> a, T key) {
  int left = -1;
  int right = (int)a.size();
  while (abs(right - left) > 1) {
    int mid = left + (right - left) / 2;
    /*
    cout << "left " << left << endl;
    cout << "right " << right << endl;
    cout << "mid " << mid << endl;
    */
    if (isOK(a, mid, key)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  // cout << "return " << right << endl;
  return right;
}

int main() {
  unsigned long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N);
  vector<long long> accA(N + 1, 0);
  // stack<int>A;

  // 累積和をつくる
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    accA[i + 1] = accA[i] + A[i];
  }
  vector<long long> B(M);
  vector<long long> accB(M + 1, 0);
  // stack<int> B;
  for (int j = 0; j < M; j++) {
    cin >> B[j];
    accB[j + 1] = accB[j] + B[j];
  }
  long long ans = 0;
  int ok = M;
  for (int i = 0; i <= N; i++) {
    if (accA[i] > K) {
      break;
    }
    while (ok >= 0 && accA[i] + accB[ok] > K) {
      ok--;
    }
    if (ok >= 0) {
      ans = max(ans, (long long)i + ok);
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// Tsundoku
#include <algorithm>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int INF = 1 << 29;

template <class T> bool isOK(vector<T> a, int index, T key) {
  // ここの比較でupper boundかlower bound か決まる
  if (a[index] > key) {
    return true;
  } else {
    return false;
  }
}

template <class T> T binarysearch(vector<T> a, T key) {
  int left = -1;
  int right = (int)a.size();
  while (abs(right - left) > 1) {
    int mid = left + (right - left) / 2;
    /*
    cout << "left " << left << endl;
    cout << "right " << right << endl;
    cout << "mid " << mid << endl;
    */
    if (isOK(a, mid, key)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  // cout << "return " << right << endl;
  return right;
}

int main() {
  unsigned long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N);
  vector<long long> accA(N + 1, 0);
  // stack<int>A;

  // 累積和をつくる
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    accA[i + 1] = accA[i] + A[i];
  }
  vector<long long> B(M);
  vector<long long> accB(M + 1, 0);
  // stack<int> B;
  for (int j = 0; j < M; j++) {
    cin >> B[j];
    accB[j + 1] = accB[j] + B[j];
  }
  long long ans = 0;

  for (int i = 0; i <= N; i++) {
    int ok = M;

    if (accA[i] > K) {
      break;
    }
    while (ok >= 0 && accA[i] + accB[ok] > K) {
      ok--;
    }
    if (ok >= 0) {
      ans = max(ans, (long long)i + ok);
    }
  }

  cout << ans << endl;
  return 0;
}