Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mi map<int, int>
#define pi pair<int, int>

#define Fori(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)

#define pb push_back
#define mp make_pair

#define read(a, n)                                                             \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define print(x) cout << "X: == " << x << endl;
#define printMap(a)                                                            \
  for (auto it = a.begin(); it != a.end(); it++) {                             \
    cout << it->first << "  " << it->second << endl;                           \
  }
#define printVector(a)                                                         \
  for (auto it = a.begin(); it != a.end(); it++) {                             \
    cout << *it << endl;                                                       \
  }

using namespace std;
int main() {
  IOS ll n, m, k;
  cin >> n >> m >> k;
  vl a(n + 1), b(m + 1);
  Fori(i, 1, n + 1) {
    ll x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  Fori(i, 1, m + 1) {
    ll x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }
  ll total = 0;
  Fori(i, 0, n + 1) {
    if (a[i] > k) {
      continue;
    }
    ll other = k - a[i];
    ll index = lower_bound(b.begin(), b.end(), other) - b.begin();
    if (index == n + 1 || a[index] > other) {
      index--;
    }
    total = max(total, i + index);
  }
  cout << total << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mi map<int, int>
#define pi pair<int, int>

#define Fori(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)

#define pb push_back
#define mp make_pair

#define read(a, n)                                                             \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define print(x) cout << "X: == " << x << endl;
#define printMap(a)                                                            \
  for (auto it = a.begin(); it != a.end(); it++) {                             \
    cout << it->first << "  " << it->second << endl;                           \
  }
#define printVector(a)                                                         \
  for (auto it = a.begin(); it != a.end(); it++) {                             \
    cout << *it << endl;                                                       \
  }

using namespace std;
int main() {
  IOS ll n, m, k;
  cin >> n >> m >> k;
  vl a(n + 1), b(m + 1);
  Fori(i, 1, n + 1) {
    ll x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  Fori(i, 1, m + 1) {
    ll x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }
  ll total = 0;
  Fori(i, 0, n + 1) {
    if (a[i] > k) {
      continue;
    }
    ll other = k - a[i];
    ll index = lower_bound(b.begin(), b.end(), other) - b.begin();
    if (index == m + 1 || b[index] > other) {
      index--;
    }
    total = max(total, i + index);
  }
  cout << total << endl;
  return 0;
}