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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define INF 1LL << 60;
#define LLINF 1e12
#define PI 3.14159265359
#define ALL(a) (a).begin(), (a).end()
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
int dy8[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dx8[] = {0, 1, 0, -1, 1, 1, -1, -1};
int ny, nx;
typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll lcm(ll m, ll n) {
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n);
}

ll llpow(ll x, ll y) {
  ll ans = 1;
  REP(i, y)
  ans *= x;
  return ans;
}

ll llmin(ll x, ll y) { return x < y ? x : y; }
ll llmax(ll x, ll y) { return x > y ? x : y; }

unsigned GetDigit(unsigned num) { return to_string(num).length(); }

template <typename Container>
bool exist_in(const Container &c, const typename Container::value_type &v) {
  return (c.end() != std::find(c.begin(), c.end(), v));
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll nCr(int n, int r) {
  if (n == r or n == 0)
    return 1;
  if (n < r)
    return 0;
  if (r == 1)
    return n;
  return nCr(n - 1, r - 1) + nCr(n - 1, r);
}

// 素因数分解
vector<ll> decom;

void decompositPrime(ll n) {
  ll a = 2;
  while (n >= a * a) {
    if (n % a == 0) {
      decom.push_back(a);
      n /= a;
    } else {
      a++;
    }
  }
  decom.push_back(n);
}

// std::cout << std::defaultfloat << std::setprecision(10);
////////////////////////////////////////
int main() {

  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N), B(M);
  REP(i, N) cin >> A[i];
  REP(i, M) cin >> B[i];

  vector<ll> Asum(N + 1, 0), Bsum(M + 1, 0);
  REP(i, N) Asum[i + 1] = A[i] + Asum[i];
  REP(i, M) Bsum[i + 1] = B[i] + Bsum[i];

  ll ans = 0, j = M;
  REP(i, N + 1) {
    if (Asum[i] > K)
      break;
    while (Bsum[j] > K - Asum[i])
      j--;
    ans = max(ans, i + j);
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define INF 1LL << 60;
#define LLINF 1e12
#define PI 3.14159265359
#define ALL(a) (a).begin(), (a).end()
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
int dy8[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dx8[] = {0, 1, 0, -1, 1, 1, -1, -1};
int ny, nx;
typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll lcm(ll m, ll n) {
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n);
}

ll llpow(ll x, ll y) {
  ll ans = 1;
  REP(i, y)
  ans *= x;
  return ans;
}

ll llmin(ll x, ll y) { return x < y ? x : y; }
ll llmax(ll x, ll y) { return x > y ? x : y; }

unsigned GetDigit(unsigned num) { return to_string(num).length(); }

template <typename Container>
bool exist_in(const Container &c, const typename Container::value_type &v) {
  return (c.end() != std::find(c.begin(), c.end(), v));
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll nCr(int n, int r) {
  if (n == r or n == 0)
    return 1;
  if (n < r)
    return 0;
  if (r == 1)
    return n;
  return nCr(n - 1, r - 1) + nCr(n - 1, r);
}

// 素因数分解
vector<ll> decom;

void decompositPrime(ll n) {
  ll a = 2;
  while (n >= a * a) {
    if (n % a == 0) {
      decom.push_back(a);
      n /= a;
    } else {
      a++;
    }
  }
  decom.push_back(n);
}

// std::cout << std::defaultfloat << std::setprecision(10);
////////////////////////////////////////
int main() {

  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  reverse(A.begin(), A.end());

  vector<ll> B(M);
  REP(i, M) cin >> B[i];
  reverse(B.begin(), B.end());

  ll res = 0, cnt = 0;

  if (accumulate(A.begin(), A.end(), 0LL) +
          accumulate(B.begin(), B.end(), 0LL) <
      K) {
    cout << N + M << endl;
    return 0;
  }

  ll less =
      llmin(*min_element(A.begin(), A.end()), *min_element(B.begin(), B.end()));
  if (less > K) {
    cout << 0 << endl;
    return 0;
  }

  while (res < K) {
    int iterA = A.size() - 1, iterB = B.size() - 1; // 末尾参照
    if (B.size() == 0 and res + B[iterA] <= K) {
      res += A[iterA];
      A.pop_back();
      cnt++;
    } else if (A.size() == 0 and res + A[iterA] <= K) {
      res += B[iterB];
      B.pop_back();
      cnt++;
    } else if (res == K) {
      cout << cnt << endl;
      return 0;
    } else {
      if (res + llmin(A[iterA], B[iterB]) <= K) {
        if (A[iterA] < B[iterB]) {
          res += A[iterA];
          A.pop_back();
          cnt++;
        } else {
          res += B[iterB];
          B.pop_back();
          cnt++;
        }
      } else {
        cout << cnt << endl;
        return 0;
      }
    }
    if (res + llmin(A[A.size() - 1], B[B.size() - 1]) > K) {
      cout << cnt << endl;
      return 0;
    }
    // cout << "cnt= " << cnt << " res= " << res << endl;
  }

  cout << cnt << endl;

  return 0;
}