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

#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for (i = a; i < b; i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define hell 1000000007
using namespace std;
const int N = 500;
void solve() {
  ll i, j, k, n, m, c = 0, ans = 0;
  string s;
  cin >> n >> m >> k;
  vector<ll> v1(n), v2(m);
  rep(i, 0, n) cin >> v1[i];
  rep(i, 0, m) cin >> v2[i];
  vector<ll> pre(n), pre1(m);
  pre[0] = v1[0];
  rep(i, 1, n) { pre[i] = pre[i - 1] + v1[i]; }
  pre1[0] = v2[0];
  rep(i, 1, m) { pre1[i] = pre1[i - 1] + v2[i]; }
  int id0 = 0, id1 = 0;
  ans = 0;
  rep(i, 0, n) {
    if (pre[i] <= k) {
      ans++;
      id0 = i;
    } else
      break;
  }
  c = 0;
  rep(i, 0, m) {
    if (pre1[i] <= k) {
      c++;
      id1 = i;
    } else
      break;
  }
  ans = max(ans, c);
  if (k >= pre[n - 1] + pre1[m - 1]) {
    cout << n + m;
    return;
  }
  if (ans == 0) {
    cout << 0;
    return;
  }
  j = 0;
  rep(i, 0, m) {
    if (pre[id0] + pre1[i] <= k)
      ans = max(ans, id0 + 1 + i + 1);
    else
      break;
  }
  // cout<<ans<<endl;
  // cout<<id0<<" ";
  rep(i, 0, m) {
    ll a1 = 0;
    // cout<<id0<<" ";
    if (id0 - 1 - i >= 0) {
      a1 = pre[id0 - 1 - i];
      ll rem = k - a1;
      // cout<<a1<<" "<<rem<<endl;
      while (j < m and rem >= pre1[j]) {
        j++;
      }
      // if(j<m and rem<pre[j]) j--;
      // cout<<j<<" ";
      // cout<<id0+j<<endl;

      ans = max(ans, id0 - i + j);
    } else
      break;
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for (i = a; i < b; i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define hell 1000000007
using namespace std;
const int N = 500;
void solve() {
  ll i, j, k, n, m, c = 0, ans = 0;
  string s;
  cin >> n >> m >> k;
  vector<ll> v1(n), v2(m);
  rep(i, 0, n) cin >> v1[i];
  rep(i, 0, m) cin >> v2[i];
  vector<ll> pre(n + 1), pre1(m + 1);
  pre[0] = v1[0];
  rep(i, 1, n) { pre[i] = pre[i - 1] + v1[i]; }
  pre1[0] = v2[0];
  rep(i, 1, m) { pre1[i] = pre1[i - 1] + v2[i]; }
  int id0 = 0, id1 = 0;
  rep(i, 0, n) {
    if (pre[i] <= k) {
      ans++;
      id0 = i;
    } else
      break;
  }
  c = 0;
  rep(i, 0, n) {
    if (pre1[i] <= k) {
      c++;
      id1 = i;
    } else
      break;
  }
  ans = max(ans, c);
  if (k >= pre[n - 1] + pre1[m - 1]) {
    cout << n + m;
    return;
  }
  if (ans == 0) {
    cout << 0;
    return;
  }
  j = 0;
  rep(i, 0, m) {
    if (pre[id1] + pre1[i] <= k)
      ans = max(ans, id1 + 1 + i + 1);
    else
      break;
  }
  rep(i, 0, m) {
    ll a1 = 0;
    // cout<<id1<<" ";
    if (id1 - 1 - i >= 0) {
      a1 = pre[id1 - 1 - i];
      ll rem = k - a1;
      // cout<<a1<<" "<<rem<<endl;
      while (j < m and rem <= pre1[j]) {
        j++;
      }
      ans = max(ans, id1 + j);
    } else
      break;
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}