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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define pb push_back
#define mp make_pair
#define N 2 * 10e5
#define precise cout << fixed << setprecision(10)
#pragma GCC optimize("O0")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
#define rep(i, a, b) for (int i = a; i < b; i++)
#define min3(a, b, c) min(a, min(b, c))
#define nl cout << "\\n";
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef vector<long long> vl;
typedef vector<pair<long, long>> vp;
void get(ll arr[], ll n) {
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
}
ll gcd(ll a, ll b);
ll binpow(ll a, ll b);
ll n, m, k;
ll a[200001], b[200001];
ll ar[200001], br[200001];
bool poss(ll mid) {
  if (mid > n) {
    for (ll i = n, j = mid - n; i >= mid - m && j <= m; i--, j++) {
      ll t = 0;
      if (i != 0)
        t += ar[i - 1];
      if (j != 0)
        t += br[j - 1];
      if (t <= k)
        return true;
    }
    return false;
  } else {
    for (ll i = mid, j = 0; i >= mid - j && j <= m; i--, j++) {
      ll t = 0;
      if (i != 0)
        t += ar[i - 1];
      if (j != 0)
        t += br[j - 1];
      if (t <= k)
        return true;
    }
    return false;
  }
}
ll bs(ll l, ll r) {
  ll ans;
  while (l <= r) {
    ll mid = (l + r) / 2;
    if (poss(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
    // cout<<mid;
  }
  return ans;
}
void solve() {
  cin >> n >> m >> k;
  // if(n<m) swap(n,m);
  get(a, n), get(b, m);
  ar[0] = a[0], br[0] = b[0];
  rep(i, 1, n) ar[i] = ar[i - 1] + a[i];
  rep(i, 1, m) br[i] = br[i - 1] + b[i];
  cout << bs(0, m + n);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC <<
  // "ms\\n";
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define pb push_back
#define mp make_pair
#define N 2 * 10e5
#define precise cout << fixed << setprecision(10)
#pragma GCC optimize("O0")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
#define rep(i, a, b) for (int i = a; i < b; i++)
#define min3(a, b, c) min(a, min(b, c))
#define nl cout << "\\n";
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef vector<long long> vl;
typedef vector<pair<long, long>> vp;
void get(ll arr[], ll n) {
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
}
ll gcd(ll a, ll b);
ll binpow(ll a, ll b);
ll n, m, k;
ll a[200001], b[200001];
ll ar[200001], br[200001];
bool poss(ll mid) {
  rep(i, 0, n + 1) {
    rep(j, 0, m + 1) {
      if (i + j == mid) {
        ll t = 0;
        if (i != 0)
          t += ar[i - 1];
        if (j != 0)
          t += br[j - 1];
        if (t <= k)
          return true;
      }
    }
  }
  return false;
}
ll bs(ll l, ll r) {
  ll ans;
  while (l <= r) {
    ll mid = (l + r) / 2;
    if (poss(mid))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
    // cout<<mid;
  }
  return ans;
}
void solve() {
  cin >> n >> m >> k;
  get(a, n), get(b, m);
  ar[0] = a[0], br[0] = b[0];
  rep(i, 1, n) ar[i] = ar[i - 1] + a[i];
  rep(i, 1, m) br[i] = br[i - 1] + b[i];
  cout << bs(0, m + n);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC <<
  // "ms\\n";
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}