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);
  ll count = distance(sum_b.begin(), itr);
  ans = count;
  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>

#pragma region Macro
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define SORT(v) sort(v.begin(), v.end())
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define vvi vector<vector<int>>
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#pragma endregion

#pragma region Debug
void debug_out() { cout << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}

#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
#pragma endregion

// コンパイル時、エイリアス「debug」を使用 --> マクロが使えるように

void solve() {
#define int ll
  // ここに処理を記述
  // コンパイル時はエイリアス「gpp」--> c++17でコンパイル
  int n, m, k;
  cin >> n >> m >> k;
  vi a(n), b(m);
  rep(i, n) { cin >> a[i]; }
  rep(i, m) { cin >> b[i]; }
  int ans = 0;

  vi a_sum(n);
  a_sum[0] = a[0];
  rep(i, n - 1) { a_sum[i + 1] = a_sum[i] + a[i + 1]; }
  vi b_sum(m);
  b_sum[0] = b[0];
  rep(i, m - 1) { b_sum[i + 1] = b_sum[i] + b[i + 1]; }

  int b_itr = m - 1;
  rep(a_itr, n) {
    if (a_sum[a_itr] > k) {
      break;
    }
    while (b_sum[b_itr] >= k - a_sum[a_itr]) {
      b_itr--;
    }
    chmax(ans, a_itr + b_itr + 2);
  }

  cout << ans << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  solve();
  return 0;
}