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

#include <bits/stdc++.h>

typedef long long LL;
typedef unsigned long long LONG;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int MAXINT = 2147483647;
const int mod = 1000000007;
const long long MAXLONG = 9223372036854775807;
const LL INT32 = (LL(MAXINT) + 1) * 2;
using namespace std;
typedef pair<int, int> PII;
#define ft fisrt
#define sd second
#define eb emplace_back
#define END '\\n'

template <class T> inline bool Read(T &ret) {
  char c;
  int sgn;
  if (static_cast<void>(c = getchar()), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (static_cast<void>(c = getchar()), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}

template <class T> inline void Write(T x) {
  if (x < 0)
    putchar('-'), Write(x);
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + '0');
}
LL a[N * 2], b[N * 2];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  LL n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  LL ans = 0, cur = m;
  for (LL i = 0; i <= n; ++i) {
    if (a[i] > k)
      break;
    while (b[cur] > (k - a[i])) {
      cur--;
    }
    ans = max(ans, i + cur);
  }
  cout << ans << endl;
  // system("pause");
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

typedef long long LL;
typedef unsigned long long LONG;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int MAXINT = 2147483647;
const int mod = 1000000007;
const long long MAXLONG = 9223372036854775807;
const LL INT32 = (LL(MAXINT) + 1) * 2;
using namespace std;
typedef pair<int, int> PII;
#define ft fisrt
#define sd second
#define eb emplace_back
#define END '\\n'

template <class T> inline bool Read(T &ret) {
  char c;
  int sgn;
  if (static_cast<void>(c = getchar()), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (static_cast<void>(c = getchar()), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}

template <class T> inline void Write(T x) {
  if (x < 0)
    putchar('-'), Write(x);
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + '0');
}

int n, m, k;
vector<int> a, b;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  LL sum = 0;
  a.resize(n + 2);
  b.resize(m + 2);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], sum += a[i];
  for (int i = 1; i <= m; ++i)
    cin >> b[i], sum += b[i];
  a[n + 1] = b[m + 1] = INF;
  LL cur = 0;
  LL ans = 0;
  LL l = 1, r = 1;
  if (sum <= k)
    cout << m + n << endl;
  else {
    while (cur < k) {
      while (l <= n && r <= m && cur < k) {
        if (a[l + 1] <= b[r])
          cur += a[l++];
        else if (b[r + 1] <= a[l])
          cur += b[r++];
        else {
          if (a[l] <= b[r])
            cur += a[l++];
          else
            cur += b[r++];
        }
        ans++;
      }
      if (l != n && cur < k)
        cur += a[l++], ans++;
      if (r != m && cur < k)
        cur += b[r++], ans++;
    }
    cout << ans - 1 << endl;
  }
  // system("pause");
  return 0;
}