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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\\n"
#define pb push_back
#define sq(a) (a) * (a)
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \\
  cerr << #v << " : ";                                                         \\
  for (auto x : v)                                                             \\
    cerr << x << ' ';                                                          \\
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n) {
    if (n & 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n >> 1;
  }
  return result;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS

      int n,
      m, k;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  ll sa[n + 1], sb[m + 1];
  sa[0] = sb[0] = 0;
  a[0] = b[0] = 0;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sa[i] = sa[i - 1] + a[i];
  }

  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    sb[i] = sb[i - 1] + b[i];
  }

  int ans = 0;

  // int l=0,r=0;
  // while((l<n)||(r<m))
  // {
  // 	ll temp=min(a[l],b[r]);

  // 	if(a[l]>b[r])
  // 		r++;
  // 	else
  // 		l++;
  // 	if((tot+temp)<=k)
  // 	{
  // 		ans++;
  // 		tot+=temp;
  // 	}
  // 	else
  // 		break;
  // }

  // for(int i=0;i<=min(k,n);i++)
  // {
  // 	tot+=a[i];
  // 	ll temp=tot,curr=i;

  // 	for(int j=1;j<=min(k-i,m);j++)
  // 	{
  // 		if((temp+b[j])<=k)
  // 		{
  // 			temp+=b[j];
  // 			curr++;
  // 		}
  // 		else
  // 			break;
  // 	}

  // 	if(temp<=k)
  // 		ans=max(ans,curr);
  // }

  int r = 0;
  for (int i = n; i >= 0; i--) {
    while ((r <= m) && (sa[i] + sb[r] <= k))
      r++;

    if (r)
      r--;

    if (sa[i] + sb[r] <= k)
      ans = max(ans, i + r);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\\n"
#define pb push_back
#define sq(a) (a) * (a)
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \\
  cerr << #v << " : ";                                                         \\
  for (auto x : v)                                                             \\
    cerr << x << ' ';                                                          \\
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n) {
    if (n & 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n >> 1;
  }
  return result;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS

      int n,
      m, k;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  ll sa[n + 1], sb[m + 1];
  sa[0] = sb[0] = 0;
  a[0] = b[0] = 0;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sa[i] = sa[i - 1] + a[i];
  }

  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    sb[i] = sb[i - 1] + b[i];
  }

  int ans = 0;

  // int l=0,r=0;
  // while((l<n)||(r<m))
  // {
  // 	ll temp=min(a[l],b[r]);

  // 	if(a[l]>b[r])
  // 		r++;
  // 	else
  // 		l++;
  // 	if((tot+temp)<=k)
  // 	{
  // 		ans++;
  // 		tot+=temp;
  // 	}
  // 	else
  // 		break;
  // }

  // for(int i=0;i<=min(k,n);i++)
  // {
  // 	tot+=a[i];
  // 	ll temp=tot,curr=i;

  // 	for(int j=1;j<=min(k-i,m);j++)
  // 	{
  // 		if((temp+b[j])<=k)
  // 		{
  // 			temp+=b[j];
  // 			curr++;
  // 		}
  // 		else
  // 			break;
  // 	}

  // 	if(temp<=k)
  // 		ans=max(ans,curr);
  // }

  for (int i = n; i >= 0; i--) {
    int r = 0;

    while ((r <= m) && (sa[i] + sb[r] <= k))
      r++;
    r--;
    if (sa[i] + sb[r] <= k)
      ans = max(ans, i + r);
  }

  cout << ans << endl;

  return 0;
}