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

#pragma region header
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// ===============================================================
// using系
#pragma region header
using namespace std;
using lint = long long;
using ld = long double;
using ulint = unsigned long long;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
constexpr lint mod = 1000000007;
constexpr long double pi = 3.141592653589793238462643383279;
#pragma endregion
// ========================================================================
// define
#define INF (lint)10000000000000000;
#define mod (int)1000000007
#pragma endregion

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lint n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  vector<lint> x(n + 1), y(m + 1);
  y[0] = 0;
  x[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x[i + 1] = x[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    y[i + 1] = y[i] + b[i];
  }
  lint ans = 0;
  int c = m;
  for (lint i = 0; i < n + 1; i++) {
    lint cnt = 0;
    if (x[i] > k)
      break;
    for (int j = c; j >= 0; j--) {
      if (x[i] + y[j] <= k) {
        cnt = i + j;
        c = j;
        break;
      }
    }
    ans = max(ans, cnt);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

// type b
// a...いけそう
// b...自信がない
// c...お試し
#pragma region header
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// ===============================================================
// using系
#pragma region header
using namespace std;
using lint = long long;
using ld = long double;
using ulint = unsigned long long;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
constexpr lint mod = 1000000007;
constexpr long double pi = 3.141592653589793238462643383279;
#pragma endregion
// ========================================================================
// define
#define INF (lint)10000000000000000;
#define mod (int)1000000007
#pragma endregion

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lint n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  vector<lint> x(n + 1), y(m + 1);
  y[0] = 0;
  x[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x[i + 1] = x[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    y[i + 1] = y[i] + b[i];
  }
  lint ans = 0;
  for (lint i = 0; i < n + 1; i++) {
    lint cnt = 0;
    if (x[i] > k)
      break;
    for (lint j = 0; j < m + 1; j++) {
      if (x[i] + y[j] > k) {
        cnt = i + j - 1ll;
        break;
      }
      if (j == m)
        cnt = i + j;
    }
    ans = max(ans, cnt);
  }
  cout << ans << endl;

  return 0;
}