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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long
#define int long long
#define ld long double
#define endl "\\n"
#define debugv(v)                                                              \\
  cerr << #v << " : ";                                                         \\
  for (auto x : v)                                                             \\
    cerr << x << ' ';                                                          \\
  cerr << endl;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define output(x) cout << (x ? "YES" : "NO") << endl;
const ll MOD = 1e9 + 7;
const int N = 2e12 + 5;
const int demo = 10;
using namespace std;
const ll cfmod = 998244353;
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

signed main() {
  int q = 1;
  // cin>>q;
  while (q--) {
    int n, m, k;
    cin >> n >> m >> k;
    int a[n + 1], b[m + 1];
    vector<int> lol1, lol2;
    lol1.push_back(0);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      sum += a[i];
      lol1.push_back(sum);
    }
    sum = 0;
    lol2.push_back(0);
    for (int i = 1; i <= m; i++) {
      cin >> b[i];
      sum += b[i];
      lol2.push_back(sum);
    }
    int maxi = 0;
    // debugv(a1)debugv(b1)
    for (int i = 0; i <= n; i++) {
      int num = lol1[i];
      int diff = k - num;
      if (diff > 0) {
        auto itr = lower_bound(lol2.begin(), lol2.end(), diff);
        if (*itr != diff) {
          itr--;
        }
        int j = itr - lol2.begin();
        maxi = max(maxi, i + j);
      } else if (diff == 0) {
        maxi = max(maxi, i);
      }
    }
    cout << maxi;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long
#define int long long
#define ld long double
#define endl "\\n"
#define debugv(v)                                                              \\
  cerr << #v << " : ";                                                         \\
  for (auto x : v)                                                             \\
    cerr << x << ' ';                                                          \\
  cerr << endl;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define output(x) cout << (x ? "YES" : "NO") << endl;
const ll MOD = 1e9 + 7;
const int N = 2e12 + 5;
const int demo = 10;
using namespace std;
const ll cfmod = 998244353;
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

signed main() {
  int q = 1;
  // cin>>q;
  while (q--) {
    int n, m, k;
    cin >> n >> m >> k;
    int a[n + 1], b[m + 1];
    int a1[n + 1], b1[m + 1];
    a1[0] = 0;
    b1[0] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      a1[i] = a1[i - 1] + a[i];
    }
    for (int i = 1; i <= m; i++) {
      cin >> b[i];
      b1[i] = b1[i - 1] + b[i];
    }
    int maxi = 0;
    // debugv(a1)debugv(b1)
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        if (a1[i] + b1[j] <= k) {
          maxi = max(maxi, i + j);
        }
      }
    }
    cout << maxi;
  }
}