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

#include <bits/stdc++.h>
#include <gmpxx.h>

#define NREP() for (ll i = 0; i < n; i++)
#define MREP() for (ll j = 0; j < m; j++)
#define REP(i, x, y) for (ll i = x; i < y; i++)
#define ALL(x) (x).begin(), (x).end()
#define MSG(x) cout << x << endl;
#define MSGF(x, n) MSG(fixed << setprecision(n) << x)
#define END(x)                                                                 \\
  cout << x << endl;                                                           \\
  return (0);
#define IPT(v, n)                                                              \\
  REP(i, 0, n) { cin >> v[i]; }
#define YN(x) x ? cout << "YES" << endl : cout << "NO" << endl;
#define Yn(x) x ? cout << "Yes" << endl : cout << "No" << endl;
#define yn(x) x ? cout << "yes" << endl : cout << "no" << endl;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<ull> vul;
typedef vector<vector<ull>> vvul;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;

const static ll MOD = 1e9 + 7;
const static ll INF = 1LL << 30;

ll a, b, c, d, k, l, m, n, h, w, q, x, y;
string s, t;

ll ans = 0;
ll sum = 0;
ll cnt = 0;
ll cur = 0;
ll tmp = 0;
ll mini = INF;
ll maxi = 0;

double ave = 0.0;

ll mod_pow(ll base, ll power) {
  if (power == 0)
    return 1;
  if (power == 1)
    return base % MOD;
  if (power % 2 == 0)
    return ((mod_pow(base % MOD, (power / 2)) % MOD) *
            (mod_pow(base, (power / 2)) % MOD)) %
           MOD;
  return (base % MOD * (mod_pow(base % MOD, power - 1) % MOD)) % MOD;
}

ll mod_fact(ll number) {
  ll ret = 1;
  ll count = 1;
  while (count <= number) {
    ret *= (count % MOD);
    ret %= MOD;
    count++;
  }
  return ret % MOD;
}

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
  cin >> n >> m >> k;
  vl a(n);
  vl b(m);

  IPT(a, n)
  IPT(b, m)

  vl sum_a(n);
  vl sum_b(m);

  sum_a.at(0) = a.at(0);
  sum_b.at(0) = b.at(0);

  REP(i, 1, n) { sum_a[i] = sum_a.at(i - 1) + a.at(i); }

  REP(i, 1, m) { sum_b[i] = sum_b.at(i - 1) + b.at(i); }

  auto itr = upper_bound(ALL(sum_b), k);
  ans = distance(sum_b.begin(), itr);

  NREP() {
    if (sum_a.at(i) > k) {
      break;
    }

    auto b_itr = upper_bound(ALL(sum_b), k - sum_a.at(i));

    ll a_count = i + 1;
    ll b_count = distance(sum_b.begin(), b_itr);

    if (b_count < 0)
      b_count = 0;
    ans = max(ans, a_count + b_count);
  }
  MSG(ans)
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <gmpxx.h>

#define NREP() for (ll i = 0; i < n; i++)
#define MREP() for (ll j = 0; j < m; j++)
#define REP(i, x, y) for (ll i = x; i < y; i++)
#define ALL(x) (x).begin(), (x).end()
#define MSG(x) cout << x << endl;
#define MSGF(x, n) MSG(fixed << setprecision(n) << x)
#define END(x)                                                                 \\
  cout << x << endl;                                                           \\
  return (0);
#define IPT(v, n)                                                              \\
  REP(i, 0, n) { cin >> v[i]; }
#define YN(x) x ? cout << "YES" << endl : cout << "NO" << endl;
#define Yn(x) x ? cout << "Yes" << endl : cout << "No" << endl;
#define yn(x) x ? cout << "yes" << endl : cout << "no" << endl;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<ull> vul;
typedef vector<vector<ull>> vvul;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;

const static ll MOD = 1e9 + 7;
const static ll INF = 1LL << 30;

ll a, b, c, d, k, l, m, n, h, w, q, x, y;
string s, t;

ll ans = 0;
ll sum = 0;
ll cnt = 0;
ll cur = 0;
ll tmp = 0;
ll mini = INF;
ll maxi = 0;

double ave = 0.0;

ll mod_pow(ll base, ll power) {
  if (power == 0)
    return 1;
  if (power == 1)
    return base % MOD;
  if (power % 2 == 0)
    return ((mod_pow(base % MOD, (power / 2)) % MOD) *
            (mod_pow(base, (power / 2)) % MOD)) %
           MOD;
  return (base % MOD * (mod_pow(base % MOD, power - 1) % MOD)) % MOD;
}

ll mod_fact(ll number) {
  ll ret = 1;
  ll count = 1;
  while (count <= number) {
    ret *= (count % MOD);
    ret %= MOD;
    count++;
  }
  return ret % MOD;
}

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
  cin >> n >> m >> k;
  vl a(n);
  vl b(m);

  IPT(a, n)
  IPT(b, m)

  vl sum_a(n);
  vl sum_b(m);

  sum_a.at(0) = a.at(0);
  sum_b.at(0) = b.at(0);

  REP(i, 1, n) { sum_a[i] = sum_a.at(i - 1) + a.at(i); }

  REP(i, 1, m) { sum_b[i] = sum_b.at(i - 1) + b.at(i); }

  ans = 0;
  NREP() {
    if (sum_a.at(i) > k) {
      break;
    }
    ll search = k - sum_a.at(i);
    ll idx_b = m - 1;
    while (sum_b.at(idx_b) > search) {
      idx_b--;
    }

    ans = max(ans, i + 1 + idx_b + 1);
  }
  MSG(ans)
}