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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define l long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define M1 1000000007
#define M2 998244353
#define ww while
#define fl(i, a, b) for (ll i = a; i < b; i++)
#define bfl(i, a, b) for (ll i = b - 1; i >= a; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define bf(i, n) for (ll i = n - 1; i >= 0; i--)

#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl

#define bn "\\n"

//------------------------------------------------------------------------
// #define V(a) vector<a>
// #define vt() vector< tuple < ll ,ll ,ll > > ;
// #define vector< ll ,ll > pv;
// vector< pair <ll,ll> > vect;

//------------------------------------------------------------------------

ll modI(ll a, ll m);
ll gcd(ll a, ll b);
ll powM(ll x, ll y, ll m);
ll swap(ll a, ll b);

void swap(ll &a, ll &b) {
  ll tp = a;
  a = b;
  b = tp;
}

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

ll powM(ll x, ll y, ll m) {
  ll ans = 1, r = 1;
  x %= m;
  ww(r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}

ll modI(ll a, ll m) {
  ll m0 = m, y = 0, x = 1;
  if (m == 1)
    return 0;
  ww(a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m;
    a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}

//---------------------------------------------------------------------------

// variable = Expression1 ? Expression2 : Expression3
// max = (n1 > n2) ? n1 : n2;
// (n>m) ? cout<<"True"<<endl:cout<<"false"<<endl ;

// ---------------------------------------------------------------------------

// ll n;cin>>n;ll a[n];for(ll i=0;i<n;i++)cin>>a[i];
// for(ll i=0;i<n;i++)cout<<a[i];
// ll a,b;cin>>a>>b;

//----------------------------------------------------------------------------

// vector< tuple < ll ,ll ,ll > > tv;
// vector< ll ,ll > pv;

//----------------------------------------------------------------------------

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  ll tt = 1;
  // cin>>tt;
  ww(tt--) {
    ll n, m;
    ll k;
    cin >> n >> m >> k;
    vector<ll> v1(n + 1);
    vector<ll> v2(m + 1);
    v1[0] = v2[0] = 0;
    fl(i, 1, n + 1) {
      cin >> v1[i];
      v1[i] = v1[i] + v1[i - 1];
    }
    fl(i, 1, m + 1) {
      cin >> v2[i];
      v2[i] = v2[i] + v2[i - 1];
    }
    ll cnt = 0;
    ll index1 = n, index2 = 0;
    ww(index1 >= 0 && index2 <= m) {
      ww(index1 >= 0 && v1[index1] + v2[index2] > k) index1--;
      ww(index2 <= m && v1[index1] + v2[index2] <= k) index2++;
      index2--;
      cnt = max(cnt, index1 + index2);
      index2++;
    }
    index1 = 0, index2 = m;
    ww(index1 <= n && index2 >= 0) {
      ww(index2 >= 0 && v1[index1] + v2[index2] > k) index2--;
      ww(index1 <= n && v1[index1] + v2[index2] <= k) index1++;
      index1--;
      cnt = max(cnt, index1 + index2);
      index1++;
    }
    //	cout<<cnt;
    p1(cnt);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define l long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define M1 1000000007
#define M2 998244353
#define ww while
#define fl(i, a, b) for (ll i = a; i < b; i++)
#define bfl(i, a, b) for (ll i = b - 1; i >= a; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define bf(i, n) for (ll i = n - 1; i >= 0; i--)

#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl

#define bn "\\n"

//------------------------------------------------------------------------
// #define V(a) vector<a>
// #define vt() vector< tuple < ll ,ll ,ll > > ;
// #define vector< ll ,ll > pv;
// vector< pair <ll,ll> > vect;

//------------------------------------------------------------------------

ll modI(ll a, ll m);
ll gcd(ll a, ll b);
ll powM(ll x, ll y, ll m);
ll swap(ll a, ll b);

void swap(ll &a, ll &b) {
  ll tp = a;
  a = b;
  b = tp;
}

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

ll powM(ll x, ll y, ll m) {
  ll ans = 1, r = 1;
  x %= m;
  ww(r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}

ll modI(ll a, ll m) {
  ll m0 = m, y = 0, x = 1;
  if (m == 1)
    return 0;
  ww(a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m;
    a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}

//---------------------------------------------------------------------------

// variable = Expression1 ? Expression2 : Expression3
// max = (n1 > n2) ? n1 : n2;
// (n>m) ? cout<<"True"<<endl:cout<<"false"<<endl ;

// ---------------------------------------------------------------------------

// ll n;cin>>n;ll a[n];for(ll i=0;i<n;i++)cin>>a[i];
// for(ll i=0;i<n;i++)cout<<a[i];
// ll a,b;cin>>a>>b;

//----------------------------------------------------------------------------

// vector< tuple < ll ,ll ,ll > > tv;
// vector< ll ,ll > pv;

//----------------------------------------------------------------------------

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  ll tt = 1;
  // cin>>tt;
  ww(tt--) {
    ll n, m;
    ll k;
    cin >> n >> m >> k;
    vector<ll> v1(n + 1);
    vector<ll> v2(n + 1);
    v1[0] = v2[0] = 0;
    fl(i, 1, n + 1) {
      cin >> v1[i];
      v1[i] = v1[i] + v1[i - 1];
    }
    fl(i, 1, m + 1) {
      cin >> v2[i];
      v2[i] = v2[i] + v2[i - 1];
    }
    ll cnt = 0;
    ll index1 = n, index2 = 0;
    ww(index1 >= 0 && index2 <= m) {
      ww(index1 >= 0 && v1[index1] + v2[index2] > k) index1--;
      ww(index2 <= m && v1[index1] + v2[index2] <= k) index2++;
      index2--;
      cnt = max(cnt, index1 + index2);
      index2++;
    }
    index1 = 0, index2 = m;
    ww(index1 <= n && index2 >= 0) {
      ww(index2 >= 0 && v1[index1] + v2[index2] > k) index2--;
      ww(index1 <= n && v1[index1] + v2[index2] <= k) index1++;
      index1--;
      cnt = max(cnt, index1 + index2);
      index1++;
    }
    //	cout<<cnt;
    p1(cnt);
  }

  return 0;
}