Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ll int64_t

using namespace std;

// memory exceed
/*
int main() {
        int64_t n,m,k;
        cin >> n>>m>>k;

        vector<unsigned int> A(n+5);
        vector<unsigned int> B(m+5);

        rep(i,n){
                cin >> A[i];

        }
        rep(i,m){
                cin >> B[i];
        }

        vector<vector<unsigned int>> dp(n+5,vector<unsigned int>(m+5,0));
        int ans = 0;
        dp[1][0] = A[0];
        rep(i,n+1){
                if(i>1){
                        dp[i][0] = dp[i-1][0] + A[i-1];
                }
                if(dp[i][0] > k)break;
                if(ans < i){
                        ans = i;
                }
                rep(j,m+1){
                        if(j==0)continue;
                        dp[i][j] = dp[i][j-1] + B[j-1];
                        if(dp[i][j] > k)break;

                        if(ans < i+j){
                                ans = i+j;
                        }
                }
        }

        cout << ans;

        return 0;

}

*/

// time exceed
/*
int main(){

        int64_t n,m,k;
        cin >> n>>m>>k;

        vector<int> A(n+5);
        vector<int> B(m+5);
        vector<int> Bsum(m+5);

        rep(i,n){
                cin >> A[i];

        }
        rep(i,m){
                cin >> B[i];
                if(i!=0){
                        Bsum[i] = Bsum[i-1] + B[i-1];
                }
        }
        Bsum[m] = Bsum[m-1]+B[m-1];

        int64_t asum =0;
        int ans =0;

        rep(i,n+1){
                if(i!=0){
                        asum = asum + A[i-1];
                }
                if(asum > k)break;

                rep(j,m+1){

                        if(asum + Bsum[j] > k)break;
                        if(ans < i+j)ans = i+j;

                }
        }
        cout << ans;

        return 0;

}

*/

int main() {

  int64_t n, m, k;
  cin >> n >> m >> k;

  vector<int> A(n + 5);
  vector<int> B(m + 5);

  ll sum = 0;

  rep(i, n) cin >> A[i + 1];
  rep(i, m) cin >> B[i + 1];

  int j = 0;

  while (sum < k || j < m + 1) {
    sum += B[j];
    j++;
  }
  j--;
  int ans = 0;

  rep(i, n + 1) {
    sum += A[i];
    while (true) {
      if (j == 0 || sum <= k)
        break;
      sum -= B[j];
      j--;
    }

    if (sum > k)
      break;
    ans = max(ans, i + j);
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ll int64_t

using namespace std;

// memory exceed
/*
int main() {
        int64_t n,m,k;
        cin >> n>>m>>k;

        vector<unsigned int> A(n+5);
        vector<unsigned int> B(m+5);

        rep(i,n){
                cin >> A[i];

        }
        rep(i,m){
                cin >> B[i];
        }

        vector<vector<unsigned int>> dp(n+5,vector<unsigned int>(m+5,0));
        int ans = 0;
        dp[1][0] = A[0];
        rep(i,n+1){
                if(i>1){
                        dp[i][0] = dp[i-1][0] + A[i-1];
                }
                if(dp[i][0] > k)break;
                if(ans < i){
                        ans = i;
                }
                rep(j,m+1){
                        if(j==0)continue;
                        dp[i][j] = dp[i][j-1] + B[j-1];
                        if(dp[i][j] > k)break;

                        if(ans < i+j){
                                ans = i+j;
                        }
                }
        }

        cout << ans;

        return 0;

}

*/

// time exceed
/*
int main(){

        int64_t n,m,k;
        cin >> n>>m>>k;

        vector<int> A(n+5);
        vector<int> B(m+5);
        vector<int> Bsum(m+5);

        rep(i,n){
                cin >> A[i];

        }
        rep(i,m){
                cin >> B[i];
                if(i!=0){
                        Bsum[i] = Bsum[i-1] + B[i-1];
                }
        }
        Bsum[m] = Bsum[m-1]+B[m-1];

        int64_t asum =0;
        int ans =0;

        rep(i,n+1){
                if(i!=0){
                        asum = asum + A[i-1];
                }
                if(asum > k)break;

                rep(j,m+1){

                        if(asum + Bsum[j] > k)break;
                        if(ans < i+j)ans = i+j;

                }
        }
        cout << ans;

        return 0;

}

*/

int main() {

  int64_t n, m, k;
  cin >> n >> m >> k;

  vector<int> A(n + 5);
  vector<int> B(m + 5);

  ll sum = 0;

  rep(i, n) cin >> A[i + 1];
  rep(i, m) cin >> B[i + 1];

  int j = 0;
  while (sum < k && j < m + 1) {
    sum += B[j];
    j++;
  }
  j--;
  int ans = 0;

  rep(i, n + 1) {
    sum += A[i];
    while (true) {
      if (j == 0 || sum <= k)
        break;
      sum -= B[j];
      j--;
    }

    if (sum > k)
      break;
    ans = max(ans, i + j);
  }
  cout << ans;

  return 0;
}