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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;

#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i, a, N) for ((i) = (a); (i) < (N); (i)++)
#define rrp(i, N, a) for ((i) = (N)-1; (i) >= (a); (i)--)
#define llfor ll i, j
#define sc(a) cin >> a
#define pr(a) cout << a << endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\\n")
#define all(a) a.begin(), a.end()
#define push(a, b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define size(a, b) (a, vector<ll>(b))
#define llpvec vector<pair<ll, ll>>
llfor;
ll mod = 1000000007;
/*pi*/ double pi = acos(-1);
/*繰り上げ除算*/ ll cei(ll x, ll y) {
  ll ans = x / y;
  if (x % y != 0)
    ans++;
  return ans;
}
/*最大公約数*/ ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
/*最小公倍数*/ ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
/*n乗*/ ll llpow(ll x, ll n) {
  ll ans = 1;
  rep(i, 0, n) {
    ans *= x;
    ans %= mod;
  }
  return ans;
}
/*階乗*/ ll fact(ll x) {
  ll ans = 1;
  rep(i, 0, x) ans *= (x - i);
  return ans;
}
/*nCr*/ ll ncr(ll n, ll r) {
  ll ans = 1;
  rrp(i, n - r + 1, n + 1) {
    ans *= i;
    ans %= mod;
  }
  return ans /= fact(r);
}
/*nPr*/ ll npr(ll n, ll r) { return fact(n) / fact(n - r); }
/*primejudge*/ bool prime(ll a) {
  if (a <= 1)
    return false;
  for (i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
/*Fibonacci数列*/ ll fib(ll x) {
  ll fibo[x + 10];
  fibo[0] = 1;
  fibo[1] = 1;
  fibo[2] = 1;
  rep(i, 3, x + 1) fibo[i] = fibo[i - 1] + fibo[i - 2];
  return fibo[x];
}
/*桁数*/ ll dig(ll n) {
  ll d = 1, tmp = n;
  while (tmp / 10 > 0) {
    tmp /= 10;
    d++;
  }
  return d;
}
ll ans = 0;
////////////////////////////////////////////////////////////
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a, b;
  ll ai, bi;
  ans = 0;
  rep(i, 0, n) {
    sc(ai);
    push(a, ai);
    if (i != 0)
      a.at(i) += a.at(i - 1);
    if (a[i] <= k)
      ans = max(ans, i + 1);
    //  cout<<ans<<endl;
  }
  // pnn;
  rep(i, 0, m) {
    sc(bi);
    push(b, bi);
    if (i != 0)
      b.at(i) += b.at(i - 1);
    if (b[i] <= k)
      ans = max(ans, i + 1);
    //  cout<<ans<<endl;
  }
  // pnn;
  ll st;
  rep(i, 0, n) {
    if (ans - i - 1 > 0)
      st = ans - i - 1;
    else
      st = 0;
    rep(j, st, m) {
      // cout<<ans<<" "<<a[i]+b[j]<<" "<<i+j+2<<endl;
      if (a[i] + b[j] <= k)
        ans = max(ans, i + j + 2);
      else
        break;
    }
  }
  pr(ans);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;

#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i, a, N) for ((i) = (a); (i) < (N); (i)++)
#define rrp(i, N, a) for ((i) = (N)-1; (i) >= (a); (i)--)
#define llfor ll i, j
#define sc(a) cin >> a
#define pr(a) cout << a << endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\\n")
#define all(a) a.begin(), a.end()
#define push(a, b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define size(a, b) (a, vector<ll>(b))
#define llpvec vector<pair<ll, ll>>
llfor;
ll mod = 1000000007;
/*pi*/ double pi = acos(-1);
/*繰り上げ除算*/ ll cei(ll x, ll y) {
  ll ans = x / y;
  if (x % y != 0)
    ans++;
  return ans;
}
/*最大公約数*/ ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
/*最小公倍数*/ ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
/*n乗*/ ll llpow(ll x, ll n) {
  ll ans = 1;
  rep(i, 0, n) {
    ans *= x;
    ans %= mod;
  }
  return ans;
}
/*階乗*/ ll fact(ll x) {
  ll ans = 1;
  rep(i, 0, x) ans *= (x - i);
  return ans;
}
/*nCr*/ ll ncr(ll n, ll r) {
  ll ans = 1;
  rrp(i, n - r + 1, n + 1) {
    ans *= i;
    ans %= mod;
  }
  return ans /= fact(r);
}
/*nPr*/ ll npr(ll n, ll r) { return fact(n) / fact(n - r); }
/*primejudge*/ bool prime(ll a) {
  if (a <= 1)
    return false;
  for (i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
/*Fibonacci数列*/ ll fib(ll x) {
  ll fibo[x + 10];
  fibo[0] = 1;
  fibo[1] = 1;
  fibo[2] = 1;
  rep(i, 3, x + 1) fibo[i] = fibo[i - 1] + fibo[i - 2];
  return fibo[x];
}
/*桁数*/ ll dig(ll n) {
  ll d = 1, tmp = n;
  while (tmp / 10 > 0) {
    tmp /= 10;
    d++;
  }
  return d;
}
ll ans = 0;
////////////////////////////////////////////////////////////
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a, b;
  ll ai, bi;
  ans = 0;
  rep(i, 0, n) {
    sc(ai);
    push(a, ai);
    if (i != 0)
      a.at(i) += a.at(i - 1);
    if (a[i] <= k)
      ans = max(ans, i + 1);
    //  cout<<ans<<endl;
  }
  // pnn;
  rep(i, 0, m) {
    sc(bi);
    push(b, bi);
    if (i != 0)
      b.at(i) += b.at(i - 1);
    if (b[i] <= k)
      ans = max(ans, i + 1);
    //  cout<<ans<<endl;
  }
  // pnn;
  ll st;
  rep(i, 0, n) {
    if (ans > 0)
      st = ans - i - 1;
    else
      st = 0;
    rep(j, st, m) {
      // cout<<ans<<" "<<a[i]+b[j]<<" "<<i+j+2<<endl;
      if (a[i] + b[j] <= k)
        ans = max(ans, i + j + 2);
      else
        break;
    }
  }
  pr(ans);
  return 0;
}