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

#include <bits/stdc++.h>

#define irep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep2(i, a, n) for (int i = (int)(a); i <= (int)(n); i++)
#define irrep(i, n) for (int i = (int)(n - 1); i > -1; i--)
#define irrep2(i, n, a) for (int i = (int)(n); i >= (int)(a); i--)
#define fi first
#define se second

using ll = long long;
using v_int = std::vector<int>;
using v2_int = std::vector<v_int>;
using v_ll = std::vector<ll>;
using v2_ll = std::vector<v_ll>;
using v_string = std::vector<std::string>;
using v_bool = std::vector<bool>;
using v2_bool = std::vector<v_bool>;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using mii = std::map<int, int>;
using mll = std::map<ll, ll>;

const double PI = 3.1415926535897932;
const int INF = 2 * (int)1e9;
const ll LINF = (ll)1e18;
const ll MOD = 1000000007;
const int dc[4] = {1, -1, 0, 0};
const int dr[4] = {0, 0, 1, -1};
// const int dc[8] = {1, -1, 0,  0, 1, -1,  1, -1};
// const int dr[8] = {0,  0, 1, -1, 1,  1, -1, -1};

using namespace std;

template <typename T, typename U>
inline ostream &operator<<(ostream &o, pair<T, U> &p) {
  return o << "{" << p.first << "," << p.second << "}";
}

template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
  int len = (int)v.size();
  for (int i = 0; i < len; i++) {
    is >> v[i];
  }
  return is;
}

template <typename T> inline ostream &operator<<(ostream &os, vector<T> &v) {
  int len = (int)v.size();
  for (int i = 0; i < len; i++) {
    os << v[i];
    if (i != len - 1) {
      os << ",";
    }
  }
  return os;
}

template <typename T> inline void chmin(T &a, T b) { a = min(a, b); }

template <typename T> inline void chmax(T &a, T b) { a = max(a, b); }

int main(void) {
  int n, m, k;
  cin >> n >> m >> k;

  v_ll a(n + 1), b(m + 1);
  irep(i, n) { cin >> a[i + 1]; }
  irep(i, m) { cin >> b[i + 1]; }

  irep(i, n) { a[i + 1] += a[i]; }
  irep(i, m) { b[i + 1] += b[i]; }

  int ans = -1;
  irep(i, n + 1) {
    if (a[i] > k) {
      continue;
    }
    ll left = k - a[i];
    int l = 0, r = b.size();
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (b[mid] <= left) {
        l = mid;
      } else {
        r = mid;
      }
    }
    chmax(ans, i + l);
  }
  cout << ans << endl;
  return 0;
}
/*atcoder*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define irep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep2(i, a, n) for (int i = (int)(a); i <= (int)(n); i++)
#define irrep(i, n) for (int i = (int)(n - 1); i > -1; i--)
#define irrep2(i, n, a) for (int i = (int)(n); i >= (int)(a); i--)
#define fi first
#define se second

using ll = long long;
using v_int = std::vector<int>;
using v2_int = std::vector<v_int>;
using v_ll = std::vector<ll>;
using v2_ll = std::vector<v_ll>;
using v_string = std::vector<std::string>;
using v_bool = std::vector<bool>;
using v2_bool = std::vector<v_bool>;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using mii = std::map<int, int>;
using mll = std::map<ll, ll>;

const double PI = 3.1415926535897932;
const int INF = 2 * (int)1e9;
const ll LINF = (ll)1e18;
const ll MOD = 1000000007;
const int dr[4] = {1, 0, -1, 0};
const int dc[4] = {0, 1, 0, -1};

using namespace std;

template <typename T, typename U>
inline ostream &operator<<(ostream &o, pair<T, U> &p) {
  return o << "{" << p.first << "," << p.second << "}";
}

template <typename T, typename U, typename V>
inline ostream &operator<<(ostream &o, tuple<T, U, V> &p) {
  return o << "{" << get<0>(p) << "," << get<1>(p) << "," << get<2>(p) << "}";
}

template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
  int len = (int)v.size();
  for (int i = 0; i < len; i++) {
    is >> v[i];
  }
  return is;
}

template <typename T> inline ostream &operator<<(ostream &os, vector<T> &v) {
  int len = (int)v.size();
  for (int i = 0; i < len; i++) {
    os << v[i];
    if (i != len - 1) {
      os << ",";
    }
  }
  return os;
}

template <typename T> inline void chmin(T &a, T b) { a = min(a, b); }

template <typename T> inline void chmax(T &a, T b) { a = max(a, b); }

int main(void) {
  ll n, m, k;
  cin >> n >> m >> k;

  v_int a(n), b(m);
  cin >> a >> b;

  ll time = 0;
  ll ans = 0;
  int idx_a = 0, idx_b = 0;
  while (time <= k) {
    ll i = INF, j = INF;
    if (idx_a < n) {
      i = a[idx_a];
    }
    if (idx_b < m) {
      j = b[idx_b];
    }
    if (idx_a == n && idx_b == m) {
      break;
    }
    // printf("%d,%d,%d,%d\\n", time, i, j, ans); printf("%d,%d\\n", idx_a,
    // idx_b);

    if (i == j) {
      int x = idx_a + 1, y = idx_b + 1;
      ll step_x = 0, step_y = 0;
      ll next_a = INF, next_b = INF;
      while (x < n && next_a > i) {
        step_x += a[x];
        next_a = a[x++];
      }
      while (y < m && next_b > j) {
        step_y += a[y];
        next_b = b[y++];
      }
      if (next_a != INF && next_b != INF) {
        if (step_x < step_y) {
          j = INF;
        } else {
          i = INF;
        }
      }
    }
    if (i > j) {
      if (time + j <= k) {
        ans++;
      }
      time += j;
      idx_b++;
    } else {
      if (time + i <= k) {
        ans++;
      }
      time += i;
      idx_a++;
    }
  }
  cout << ans << endl;
  return 0;
}

/*atcoder*/