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

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define fo(i, s, e) for (i = s; i < e; i++)
#define rfo(i, s, e) for (i = s; i > e; i--)
#define LLI long long int
#define LI long int
const LI MOD = 1e9 + 7;
#define pb push_back
#define pob pop_back()
#define sp " "
#define ff first
#define ss second
//            When something is important enough, you do it even if the odds are
//            not in your favor.
long long int binarySearch(LLI arr[], long long int end, long long int target) {
  long long int start = 0;
  end--;
  long long int ans = -1;
  while (start <= end) {
    long long int mid = (start + end) / 2;
    if (arr[mid] > target) {
      end = mid - 1;
    } else {
      ans = mid;
      start = mid + 1;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // code goes here
  int t = 1; // cin>>t;
  while (t--) {
    LLI n, m, k, i;
    cin >> n >> m >> k;
    LLI a[n], b[m], ans = 0;
    fo(i, 0, n) cin >> a[i];
    fo(i, 0, m) cin >> b[i];
    fo(i, 1, m) b[i] += b[i - 1];
    LLI sum = 0;
    fo(i, 0, n + 1) {
      if (i == 0) {
        LLI in1 = binarySearch(b, m, k);
        if (in1 == -1)
          in1 = 0;
        else
          in1++;
        ans = max(ans, i + in1);
      } else {
        sum += a[i - 1];
        LLI in1 = binarySearch(b, m, k - sum);
        if (in1 == -1)
          in1 = 0;
        else
          in1++;
        if (sum > k)
          break;
        ans = max(ans, i + in1);
      }
    }
    cout << ans;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define fo(i, s, e) for (i = s; i < e; i++)
#define rfo(i, s, e) for (i = s; i > e; i--)
#define LLI long long int
#define LI long int
const LI MOD = 1e9 + 7;
#define pb push_back
#define pob pop_back()
#define sp " "
#define ff first
#define ss second
//            When something is important enough, you do it even if the odds are
//            not in your favor.
long long int binarySearch(LLI arr[], long long int end, long long int target) {
  long long int start = 0;
  end--;
  long long int ans = -1;
  while (start <= end) {
    long long int mid = (start + end) / 2;
    if (arr[mid] > target) {
      end = mid - 1;
    } else {
      ans = mid;
      start = mid + 1;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // code goes here
  int t = 1; // cin>>t;
  while (t--) {
    LLI n, m, k, i;
    cin >> n >> m >> k;
    LLI a[n], b[m], ans = 0;
    fo(i, 0, n) cin >> a[i];
    fo(i, 0, m) cin >> b[i];
    fo(i, 1, n) a[i] += a[i - 1];
    fo(i, 1, m) b[i] += b[i - 1];
    fo(i, 0, k + 1) {
      LLI in = binarySearch(a, n, i);
      if (in == -1)
        in = 0;
      else
        in++;
      LLI in1 = binarySearch(b, m, k - i);
      if (in1 == -1)
        in1 = 0;
      else
        in1++;
      ans = max(ans, in + in1);
    }
    cout << ans;
  }
  return 0;
}