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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var)                                                             \\
  SORT(var);                                                                   \\
  REVERSE(var)
#define OPTIMIZE_STDIO                                                         \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.precision(10);                                                          \\
  cout << fixed
#define endl '\\n'

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

void solve(istream &cin, ostream &cout) {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  FOR(i, 1, n) cin >> a[i];
  FOR(i, 1, m) cin >> b[i];
  FOR(i, 1, n) a[i] += a[i - 1];
  FOR(i, 1, m) b[i] += b[i - 1];
  int best;
  RFOR(j, m, 0) {
    if (b[j] <= k) {
      best = j;
      break;
    }
  }
  int ans = 0;
  int j = best;
  FOR(i, 0, n) {
    if (a[i] > k)
      break;
    while (b[j] > k - a[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
  return 0;
}
#endif


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var)                                                             \\
  SORT(var);                                                                   \\
  REVERSE(var)
#define OPTIMIZE_STDIO                                                         \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.precision(10);                                                          \\
  cout << fixed
#define endl '\\n'

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

void solve(istream &cin, ostream &cout) {
  int n, m, k;
  cin >> n >> m >> k;
  queue<int> qa, qb;
  REP(i, n) {
    int a;
    cin >> a;
    qa.push(a);
  }
  REP(j, m) {
    int b;
    cin >> b;
    qb.push(b);
  }
  int time = 0;
  int cnt = 0;
  while (true) {
    int now;
    int na = qa.front();
    int nb = qb.front();
    if (na > nb) {
      now = nb;
      qb.pop();
    } else {
      now = na;
      qa.pop();
    }
    time += now;
    cnt++;
    if (time > k) {
      cnt--;
      cout << cnt << endl;
      return;
    }
  }
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
  return 0;
}
#endif