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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define in cin >>
#define out cout <<
#define br cout << "\\n";

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n];
  int b[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int sum = 0;
  int suma = 0;
  int count = 0;
  int maxcount = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
    count++;
    suma++;
    if (sum > k) {
      count--;
      suma--;
      sum -= a[i];
      break;
    }
  }
  maxcount = count;
  for (int i = 0; i < m; i++) {
    sum += b[i];
    count++;
    if (sum > k) {
      count--;
      if (suma == 0) {
        break;
      }
      count--;
      sum -= b[i];
      sum -= a[suma - 1];
      suma--;
      i--;
    }
    maxcount = max(maxcount, count);
  }
  cout << maxcount << "\\n";
}

int32_t main() {
  FASTIO
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

#ifndef ONLINE_JUDGE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\\n";
#endif

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define in cin >>
#define out cout <<
#define br cout << "\\n";

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n];
  int b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int suma = 0;
  int counta = 0;
  for (int i = 0; i < n; i++) {
    suma += a[i];
    counta++;
    if (suma > k) {
      suma -= a[i];
      counta--;
      break;
    }
  }
  int sumb = 0;
  int countb = 0;
  for (int i = 0; i < m; i++) {
    sumb += b[i];
    countb++;
    if (sumb > k) {
      sumb -= b[i];
      countb--;
      break;
    }
  }
  // cout<<counta<<" "<<countb<<"\\n";
  if (countb > counta) {
    for (int i = 0; i < n; i++) {
      sumb += a[i];
      countb++;
      if (sumb > k) {
        sumb -= a[i];
        countb--;
        break;
      }
    }
    cout << countb << "\\n";
    return;
  } else if (counta > countb) {
    for (int i = 0; i < m; i++) {
      suma += b[i];
      counta++;
      if (suma > k) {
        suma -= a[i];
        counta--;
        break;
      }
    }
    cout << counta << "\\n";
    return;
  }
  if (suma == sumb) {
    int findmin = -1;
    for (int i = 0; i < min(n, m); i++) {
      if (a[i] > b[i]) {
        findmin = 2;
        break;
      } else if (b[i] > a[i]) {
        findmin = 1;
        break;
      }
    }
    if (findmin == -1) {
      if (m > n)
        findmin = 2;
      else if (n > m)
        findmin = 1;
    }
    if (findmin == 2) {
      for (int i = 0; i < n; i++) {
        sumb += a[i];
        countb++;
        if (sumb > k) {
          sumb -= a[i];
          countb--;
          break;
        }
      }
      cout << countb << "\\n";
      return;
    } else {
      for (int i = 0; i < m; i++) {
        suma += b[i];
        counta++;
        if (suma > k) {
          suma -= a[i];
          counta--;
          break;
        }
      }
      cout << counta << "\\n";
      return;
    }
  } else {
    if (suma > sumb) {
      for (int i = 0; i < n; i++) {
        sumb += a[i];
        countb++;
        if (sumb > k) {
          sumb -= a[i];
          countb--;
          break;
        }
      }
      cout << countb << "\\n";
      return;
    } else {
      for (int i = 0; i < m; i++) {
        suma += b[i];
        counta++;
        if (suma > k) {
          suma -= a[i];
          counta--;
          break;
        }
      }
      cout << counta << "\\n";
      return;
    }
  }
}

int32_t main() {
  FASTIO
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

#ifndef ONLINE_JUDGE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\\n";
#endif

  return 0;
}