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

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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;

#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define deb(x) cout << #x << " - " << x << "\\n";
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define D2(x)                                                                  \\
  cout << "[" << #x << ": ";                                                   \\
  for (auto it : x)                                                            \\
    cout << it << " ";                                                         \\
  cout << "]\\n";
#define D2p(x)                                                                 \\
  cout << #x << " ~ [ ";                                                       \\
  for (auto n : x)                                                             \\
    cout << n.first << "-" << n.second << " / ";                               \\
  cout << "]"                                                                  \\
       << "\\n";
#define ps(x, y) fixed << setprecision(y) << x

#define PI 3.141592653589793238462643383279502884L
const ll MOD = 1e9 + 7;
const ll INF = 1000000000;
const ll MAX_N = 1e5 + 2;

int main() {
  IOS;
  ll n, m, sum, res = 0;
  cin >> n >> m >> sum;
  vll v1(n), v2(m);
  rep(i, 0, n) cin >> v1[i];
  rep(i, 0, m) cin >> v2[i];

  rep(i, 1, n) v1[i] += v1[i - 1];
  rep(i, 1, m) v2[i] += v2[i - 1];

  ll cnt = 0;
  for (ll i = n - 1; i >= 0; i--) {
    ll remain = sum - v1[i];
    if (remain < 0)
      continue;
    auto it = upper_bound(all(v2), remain) - v2.begin();
    cnt = i + it + 1;
    res = max(res, cnt);
  }

  auto last1 = upper_bound(all(v2), sum) - v2.begin();
  auto last2 = upper_bound(all(v1), sum) - v1.begin();
  res = max(res, (ll)last1);
  res = max(res, (ll)last2);
  cout << res;
}

Error: WA

Fixed submission:

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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;

#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define deb(x) cout << #x << " - " << x << "\\n";
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define D2(x)                                                                  \\
  cout << "[" << #x << ": ";                                                   \\
  for (auto it : x)                                                            \\
    cout << it << " ";                                                         \\
  cout << "]\\n";
#define D2p(x)                                                                 \\
  cout << #x << " ~ [ ";                                                       \\
  for (auto n : x)                                                             \\
    cout << n.first << "-" << n.second << " / ";                               \\
  cout << "]"                                                                  \\
       << "\\n";
#define ps(x, y) fixed << setprecision(y) << x

#define PI 3.141592653589793238462643383279502884L
const ll MOD = 1e9 + 7;
const ll INF = 1000000000;
const ll MAX_N = 1e5 + 2;

int main() {
  IOS;
  ll n, m, sum;
  cin >> n >> m >> sum;
  vll v1(n), v2(m);
  rep(i, 0, n) cin >> v1[i];
  rep(i, 0, m) cin >> v2[i];

  rep(i, 1, n) v1[i] += v1[i - 1];
  rep(i, 1, m) v2[i] += v1[i - 1];

  auto itx = upper_bound(all(v1), sum);
  ll start, res = 0;
  if (itx == v1.end())
    start = n - 1;
  else
    start = itx - v1.begin() - 1;

  ll cnt = 0;
  for (ll i = start; i >= 0; i--) {
    ll remain = sum - v1[i];
    auto it = upper_bound(all(v2), remain) - v2.begin();
    if (it < 0)
      continue;
    cnt = i + it;
    res = max(res, cnt);
  }
  auto last1 = upper_bound(all(v2), sum) - v2.begin();
  auto last2 = upper_bound(all(v1), sum) - v1.begin();
  res = max(res, (ll)last1);
  res = max(res, (ll)last2);
  cout << res;
}