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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define rep(i, p) for (long long int i = 0; i < p; i++)
#define reep(i, p) for (long long int i = 1; i <= p; i++)
#define ll long long

using namespace std;

int main() {

  ll int N, M, K;
  cin >> N >> M >> K;
  vector<ll int> A(N);
  vector<ll int> B(M);

  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }
  ll int counterA = 0;
  // ll int counterB=0;
  ll int counter = 1;

  ll int max = 0;
  ll int time = A[0];

  long long int i = 0;
  if (time > K) {
    time -= A[0];
    counter -= 1;
    counterA = 1;
  } else {
    if (N != 1) {
      while (time + A[i + 1] <= K) {
        time += A[i + 1];
        i += 1;
        counter += 1;
        if (i + 1 == N) {
          break;
        } else {
        }
      }
    } else {
    }
  }
  long long int j = 0;
  while (time + B[j] <= K) {
    time += B[j];
    j += 1;
    counter += 1;
    if (j == M) {
      break;
    } else {
    }
  }
  max = counter;
  if (j == M || counterA == 1) {
    cout << max << endl;
    return 0;
  } else {
  }

  while (i >= 0) {
    time -= A[i];
    i -= 1;
    counter -= 1;
    while (time + B[j] <= K) {
      time += B[j];
      j += 1;
      counter += 1;
      if (j == M) {
        if (counter > max) {
          max = counter;
        } else {
        }
        cout << max << endl;
        return 0;
      } else {
      }
    }
    if (counter > max) {
      max = counter;
    } else {
    }
  }

  /*
  for (long long int i=0;i<=1<<(N+M-1);i++){
          for(long long int j=0;;j++){
                  if(i>>j&1){
                          if(time+A[counterA]<=K){
                          if(counterA == N){
                                  break;
                          }
                          else{
                          time += A[counterA];
                          counter += 1;
                          counterA+=1;
                          }
                          }
                          else{
                                  break;
                          }
                  }
                  else{
                          if(time+B[counterB]<=K){
                          if(counterB == M){
                                  break;
                          }
                          else{
                          time += B[counterB];
                          counter += 1;
                          counterB += 1;
                          }
                          }
                          else{
                                  break;
                          }
                  }

          }
          if(counter > max){
                  max = counter;
          }
          else{}


   counterA=0;
   counterB=0;
   counter=0;
   time = 0;
  }
  */
  cout << max;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define rep(i, p) for (long long int i = 0; i < p; i++)
#define reep(i, p) for (long long int i = 1; i <= p; i++)
#define ll long long

using namespace std;

int main() {

  ll int N, M, K;
  cin >> N >> M >> K;
  vector<ll int> A(N);
  vector<ll int> B(M);

  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }
  ll int counterA = 0;
  ll int counterB = 0;
  ll int counter = 1;

  ll int max = 0;
  ll int time = A[0];

  long long int i = 0;
  if (time > K) {
    time -= A[0];
    counter -= 1;
    counterA = 1;
  } else {
    while (time + A[i + 1] <= K) {
      time += A[i + 1];
      i += 1;
      counter += 1;
      if (i + 1 == N) {
        break;
      } else {
      }
    }
  }
  long long int j = 0;
  while (time + B[j] <= K) {
    time += B[j];
    j += 1;
    counter += 1;
    if (j == M) {
      break;
    } else {
    }
  }
  max = counter;
  if (j == M || counterA == 1) {
    cout << max << endl;
    return 0;
  } else {
  }

  while (i >= 0) {
    time -= A[i];
    i -= 1;
    counter -= 1;
    while (time + B[j] <= K) {
      time += B[j];
      j += 1;
      counter += 1;
      if (j == M) {
        max = counter;
        cout << max << endl;
        return 0;
      } else {
      }
    }
    if (counter > max) {
      max = counter;
    } else {
    }
  }

  /*
  for (long long int i=0;i<=1<<(N+M-1);i++){
          for(long long int j=0;;j++){
                  if(i>>j&1){
                          if(time+A[counterA]<=K){
                          if(counterA == N){
                                  break;
                          }
                          else{
                          time += A[counterA];
                          counter += 1;
                          counterA+=1;
                          }
                          }
                          else{
                                  break;
                          }
                  }
                  else{
                          if(time+B[counterB]<=K){
                          if(counterB == M){
                                  break;
                          }
                          else{
                          time += B[counterB];
                          counter += 1;
                          counterB += 1;
                          }
                          }
                          else{
                                  break;
                          }
                  }

          }
          if(counter > max){
                  max = counter;
          }
          else{}


   counterA=0;
   counterB=0;
   counter=0;
   time = 0;
  }
  */
  cout << max;

  return 0;
}