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

//============================================================================
// Name        : template.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string.h> // for memset in CF judge.
#include <vector>
using namespace std;
#define _CRT_SECURE_NO_DEPRECATE // suppress some compilation warning messages
                                 // (for VC++ users)
// Shortcuts for "common" data types in contests
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
// To simplify repetitions/loops, Note: define your loop style and stick with
// it!
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define REP(i, a, b)                                                           \\
  for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define forn(i, n) for (int i = 0; i < (n); i++)
#define TRvi(c, it) for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) for (msi::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 2000000000 // 2 billion
// If you need to recall how to use memset:
#define MEMSET_INF 127     // about 2B
#define MEMSET_HALF_INF 63 // about 1B
// memset(dist, MEMSET_INF, sizeof dist); // useful to initialize shortest path
// distances memset(dp_memo, -1, sizeof dp_memo); // useful to initialize DP
// memoization table memset(arr, 0, sizeof arr); // useful to clear array of
// integers

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;

  vector<unsigned long long int> a(n + 1, 0);
  vector<unsigned long long int> b(m + 1, 0);

  REP(i, 1, n) {
    ll x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  REP(i, 1, m) {
    ll x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }

  int i = n;
  while (i > 0 && a[i] > k) {
    i--;
  }
  int ans = i;
  int j = 0;
  bool gone = false;
  while (i >= 0 && j <= m) {
    if (a[i] + b[j] > k) {
      i--;
    } else {
      ans = max(ans, i + j);
      j++;
    }
  }
  if (i >= 0 && j <= m && a[i] + b[j] <= k)
    ans = max(ans, i + j);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

//============================================================================
// Name        : template.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string.h> // for memset in CF judge.
#include <vector>
using namespace std;
#define _CRT_SECURE_NO_DEPRECATE // suppress some compilation warning messages
                                 // (for VC++ users)
// Shortcuts for "common" data types in contests
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
// To simplify repetitions/loops, Note: define your loop style and stick with
// it!
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define REP(i, a, b)                                                           \\
  for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define forn(i, n) for (int i = 0; i < (n); i++)
#define TRvi(c, it) for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) for (msi::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 2000000000 // 2 billion
// If you need to recall how to use memset:
#define MEMSET_INF 127     // about 2B
#define MEMSET_HALF_INF 63 // about 1B
// memset(dist, MEMSET_INF, sizeof dist); // useful to initialize shortest path
// distances memset(dp_memo, -1, sizeof dp_memo); // useful to initialize DP
// memoization table memset(arr, 0, sizeof arr); // useful to clear array of
// integers

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;

  vector<unsigned long long int> a(n + 1, 0);
  vector<unsigned long long int> b(m + 1, 0);

  REP(i, 1, n) {
    ll x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  REP(i, 1, m) {
    ll x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }

  int i = n;
  while (i > 0 && a[i] > k) {
    i--;
  }
  int ans = i;
  int j = 0;
  bool gone = false;
  while (i >= 0 && j <= n) {
    if (a[i] + b[j] > k) {
      i--;
    } else {
      ans = max(ans, i + j);
      j++;
    }
  }
  if (a[i] + b[j] <= k)
    ans = max(ans, i + j);
  cout << ans << endl;
  return 0;
}