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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
// const int64 INF = 1LL << 58;
#define dame                                                                   \\
  {                                                                            \\
    puts("-1");                                                                \\
    return 0;                                                                  \\
  }
#define YES                                                                    \\
  {                                                                            \\
    cout << "YES" << endl;                                                     \\
    return 0;                                                                  \\
  }
#define NO                                                                     \\
  {                                                                            \\
    cout << "NO" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define Yes                                                                    \\
  {                                                                            \\
    cout << "Yes" << endl;                                                     \\
    return 0;                                                                  \\
  }
#define No                                                                     \\
  {                                                                            \\
    cout << "No" << endl;                                                      \\
    return 0;                                                                  \\
  }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m, k;
  cin >> n >> m >> k;
  vector<unsigned long> a(n + 1);
  vector<unsigned long> b(m + 1);

  a[0] = 0;
  b[0] = 0;
  int al = n, bl = m;
  rep1(i, n + 1) {
    unsigned long x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }

  rep1(i, m + 1) {
    unsigned long x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }
  if (a[n] + b[m] <= k) {
    cout << n + m << endl;
    return 0;
  }
  pair<unsigned long, ll> max = {0, 0};

  rep(i, n + 1) {
    ll keys = k - a[i];
    int x = upper_bound(all(b), keys) - b.begin() - 1;
    if (a[i] + b[x] <= k && max.sc < i + x) {
      max = {a[i] + b[x], i + x};
    }
  }
  cout << max.sc << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
// const int64 INF = 1LL << 58;
#define dame                                                                   \\
  {                                                                            \\
    puts("-1");                                                                \\
    return 0;                                                                  \\
  }
#define YES                                                                    \\
  {                                                                            \\
    cout << "YES" << endl;                                                     \\
    return 0;                                                                  \\
  }
#define NO                                                                     \\
  {                                                                            \\
    cout << "NO" << endl;                                                      \\
    return 0;                                                                  \\
  }
#define Yes                                                                    \\
  {                                                                            \\
    cout << "Yes" << endl;                                                     \\
    return 0;                                                                  \\
  }
#define No                                                                     \\
  {                                                                            \\
    cout << "No" << endl;                                                      \\
    return 0;                                                                  \\
  }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  unsigned long n, m, k;
  cin >> n >> m >> k;
  vector<pair<unsigned long, ll>> a(n + 1);
  vector<pair<unsigned long, ll>> b(m + 1);

  a[0] = {0, 0};
  b[0] = {0, 0};
  int al = n, bl = m;
  rep1(i, n + 1) {
    unsigned long x;
    cin >> x;
    a[i] = {a[i - 1].fs + x, i};
    if (a[i].fs > k) {
      al = min(i, al);
    }
  }

  rep1(i, m + 1) {
    unsigned long x;
    cin >> x;
    b[i] = {b[i - 1].fs + x, i};
    if (b[i].fs > k) {
      bl = min(i, bl);
    }
  }
  if (a[n].fs + b[m].fs <= k) {
    cout << n + m << endl;
    return 0;
  }
  pair<unsigned long, ll> max = {0, 0};
  rep(i, al + 1) {
    rep(j, bl + 1) {
      if (a[i].fs + b[j].fs > k)
        break;
      if (a[i].fs + b[j].fs <= k && max.sc < a[i].sc + b[j].sc) {
        max = {a[i].fs + b[j].fs, a[i].sc + b[j].sc};
      }
    }
  }
  cout << max.sc << endl;
  return 0;
}