Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll; // [9,223,372,036,854,775,807 to -9.....808] 19 digits
typedef long double ld;
#define inc(i, a, b) for (long long int i = a; i <= b; i++)
#define dec(i, a, b) for (long long int i = a; i >= b; i--)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define lv vector<ll>
#define cv vector<char>
#define dv vector<ld>
#define rall(c) (c).rbegin(), (c).rend()
#define lpr pair<ll, ll>
#define mp make_pair
#define cin(c)                                                                 \\
  ll c;                                                                        \\
  cin >> c;
#define lmap unordered_map<ll, ll>
#define vinp(a, n)                                                             \\
  inc(i, 1, n) {                                                               \\
    cin(k);                                                                    \\
    a.pb(k);                                                                   \\
  }
#define LIM 1000000007
#define F first
#define S second
#define fr(i, n) inc(i, 0, n - 1)
#define fri(n, a)                                                              \\
  inc(i, 0, n - 1) { cin >> a[i]; }
#define debug(p) cout << p;
#define umap unordered_map
#define uset unordered_set
#define fill(a, val, n)                                                        \\
  inc(i, 0, n - 1) { a[i] = val; }
void i_love_Accepted() {
  ll n, m, ms;
  cin >> n >> m >> ms;
  ll A[n], B[m];
  for (ll A_i = 0; A_i < n; A_i++) {
    cin >> A[A_i];
  }
  for (ll B_i = 0; B_i < m; B_i++) {
    cin >> B[B_i];
  }

  ll sum = 0;
  ll x = 0, y = 0;

  while (x < n && sum + A[x] <= ms) {
    sum += A[x++];
  }

  ll ans = x;

  while (y < m && x >= 0) {
    sum += B[y++];
    while (sum > ms && x > 0) {
      sum -= A[--x];
    }

    if (sum <= ms && ans < x + y) {
      ans = x + y;
    }
  }

  cout << ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast;
  ll t = 1;
  // cin>>t;
  while (t--) {
    i_love_Accepted();
    // cout<<"\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll; // [9,223,372,036,854,775,807 to -9.....808] 19 digits
typedef long double ld;
#define inc(i, a, b) for (long long int i = a; i <= b; i++)
#define dec(i, a, b) for (long long int i = a; i >= b; i--)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define lv vector<ll>
#define cv vector<char>
#define dv vector<ld>
#define rall(c) (c).rbegin(), (c).rend()
#define lpr pair<ll, ll>
#define mp make_pair
#define cin(c)                                                                 \\
  ll c;                                                                        \\
  cin >> c;
#define lmap unordered_map<ll, ll>
#define vinp(a, n)                                                             \\
  inc(i, 1, n) {                                                               \\
    cin(k);                                                                    \\
    a.pb(k);                                                                   \\
  }
#define LIM 1000000007
#define F first
#define S second
#define fr(i, n) inc(i, 0, n - 1)
#define fri(n, a)                                                              \\
  inc(i, 0, n - 1) { cin >> a[i]; }
#define debug(p) cout << p;
#define umap unordered_map
#define uset unordered_set
#define fill(a, val, n)                                                        \\
  inc(i, 0, n - 1) { a[i] = val; }
ll A[100002], B[100002];
void i_love_Accepted() {
  ll n, m, ms;
  cin >> n >> m >> ms;

  for (ll A_i = 0; A_i < n; A_i++) {
    cin >> A[A_i];
  }
  for (ll B_i = 0; B_i < m; B_i++) {
    cin >> B[B_i];
  }

  ll sum = 0;
  ll x = 0, y = 0;

  while (x < n && sum + A[x] <= ms) {
    sum += A[x++];
  }

  ll ans = x;

  while (y < m && x >= 0) {
    sum += B[y++];
    while (sum > ms && x > 0) {
      sum -= A[--x];
    }

    if (sum <= ms && ans < x + y) {
      ans = x + y;
    }
  }

  cout << ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast;
  ll t = 1;
  // cin>>t;
  while (t--) {
    i_love_Accepted();
    // cout<<"\\n";
  }
}