alexjercan/bugnet huggingface dataset:
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define _                                                                      \\
  ios::sync_with_stdio(false);                                                 \\
  cout.tie(NULL);                                                              \\
  cin.tie(NULL);
using namespace std;

int main() {
  _ ll n, m, k, sum = 0, ans = 0, max1 = 0, l = 0, r = 0;
  cin >> n >> m >> k;
  deque<ll> dq1, dq2;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    dq1.push_back(x);
  }
  for (ll i = 0; i < m; i++) {
    ll x;
    cin >> x;
    dq2.push_back(x);
  }
  while (l < n) {
    if (sum + dq1[l] <= k) {
      sum += dq1[l];
      ans++;
      l++;
    } else {
      l--;
      break;
    }
  }
  if (l == n)
    l--;
  max1 = max(max1, ans);
  while (l >= -1 && r < m) {
    if (sum + dq2[r] <= k) {
      sum += dq2[r];
      ans++;
      r++;
    } else {
      if (l < 0)
        break;
      sum -= dq1[l];
      l--;
      ans--;
    }
    max1 = max(ans, max1);
  }
  max1 = max(ans, max1);
  cout << max1 << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define _                                                                      \\
  ios::sync_with_stdio(false);                                                 \\
  cout.tie(NULL);                                                              \\
  cin.tie(NULL);
using namespace std;

int main() {
  _ ll n, m, k, sum = 0, ans = 0, max1 = 0, l = 0, r = 0;
  cin >> n >> m >> k;
  deque<ll> dq1, dq2;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    dq1.push_back(x);
  }
  for (ll i = 0; i < m; i++) {
    ll x;
    cin >> x;
    dq2.push_back(x);
  }
  while (l < n) {
    if (sum + dq1[l] <= k) {
      sum += dq1[l];
      ans++;
      l++;
    } else {
      l--;
      break;
    }
  }
  if (l == n)
    l--;
  max1 = max(max1, ans);
  while (l >= -1 && r < m) {
    if (sum + dq2[r] <= k) {
      sum += dq2[r];
      ans++;
      r++;
    } else {
      sum -= dq1[l];
      l--;
      ans--;
    }
    max1 = max(ans, max1);
  }
  max1 = max(ans, max1);
  cout << max1 << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
#define endl '\\n'
#define debug(name) DEBUG(#name, (name))
template <typename T> void DEBUG(string label, T value) {
  cerr << "[" << label << " = " << value << "]\\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  ll k;
  cin >> n >> m >> k;
  ll s1 = 0, s2 = 0;
  vector<int> one(n), two(m);
  for (int i = 0; i < n; ++i) {
    cin >> one[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> two[i];
    s2 += two[i];
  }
  int p = m - 1;
  int ans = 0;
  if (s2 <= k) {
    ans = m;
  }
  for (int i = 0; i < n; ++i) {
    s1 += one[i];
    while (p >= 0 && s2 > k - s1) {
      s2 -= two[p];
      p--;
    }
    if (s1 + s2 <= k)
      ans = max(ans, i + 1 + p + 1);
  }
  if (s1 <= k) {
    ans = max(ans, n);
  }
  s2 = 0;
  p = n - 1;
  for (int i = 0; i < m; ++i) {
    s2 += two[i];
    while (p >= 0 && s1 + s2 > k) {
      s1 -= one[p];
      p--;
    }
    if (s1 + s2 <= k)
      ans = max(ans, i + 1 + p + 1);
  }
  cout << ans << endl;
}


Error: WA

Fixed submission:

// arif//c++//vjudge
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main()

{

  long long t, e, f, c, i, d;
  int a[100];
  cin >> t >> e >> f;
  for (i = 0; i < t; i++) {
    cin >> a[i];
  }

  for (i = t; i < e + t; i++) {
    cin >> a[i];
  }
  for (i = 0; i < e + t; i++) {
    sort(a, a + e + t);
  }
  int sum = 0, l = 0;
  for (i = 0; i < e + t; i++) {
    if (sum < f) {
      sum = sum + a[i];
      ++l;
    }
  }
  if (sum > f)
    cout << l - 1;
  else
    cout << l;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long long N, M, K;
long long As[200005];
long long Bs[200005];
long long aim[200005];
int main() {
  scanf("%lld%lld%lld", &N, &M, &K);
  long long s = 0;
  int length = 1;
  aim[0] = K;
  for (int i = 1; i <= N; ++i) {
    scanf("%lld", &As[i]);
    s += As[i];
    if (s > K)
      continue;

    length++;
    aim[i] = K - s;
  }
  if (length == -1)
    length = N + 1;

  s = 0;
  int maxN = length - 1;
  int ptr = length - 1;
  for (int i = 1; i <= M; ++i) {
    scanf("%d", &Bs[i]);

    if (s > K)
      continue;

    s += Bs[i];

    while (ptr >= 0 && s > aim[ptr])
      ptr--;

    if (ptr >= 0)
      maxN = max(maxN, ptr + i);
  }

  printf("%d\\n", maxN);
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
unsigned long long int *readStack(int length) {
  unsigned long long int *stack = new unsigned long long int[length];
  for (int i = 0; i < length; i++) {
    cin >> stack[i];
    // cout << stack[i] << " ";
  }
  // cout << endl;
  return stack;
}
unsigned long long int getSum(unsigned long long int *s, int l) {
  unsigned long long int sum = 0;
  for (int i = 0; i < l; i++)
    sum += s[i];
  return sum;
}
int main() {
  int n, m;
  unsigned long long int k;
  cin >> n >> m >> k;
  unsigned long long int *aStack = readStack(n);
  unsigned long long int *bStack = readStack(m);
  // check if time sum of stack is <= k
  if (getSum(aStack, n) + getSum(bStack, m) <= k)
    cout << m + n;
  else {
    int numOfBooks = 0;
    bool overFlowFlag = false;
    for (int i = -1; i < n; i++) {
      for (int j = -1; j < m; j++) {
        unsigned long long int time = 0;
        // cout << i << " " << j << endl;
        if (i == -1 && j == -1)
          continue;
        if (i >= 0)
          time += getSum(aStack, i + 1);
        // cout << time << endl;
        if (time > k) {
          overFlowFlag = true;
          break;
        }
        if (j >= 0)
          time += getSum(bStack, j + 1);
        // cout << time << endl;
        if (time > k) {
          break;
        }
        if (time <= k && i + j + 2 > numOfBooks)
          numOfBooks = i + j + 2;
      }
      if (overFlowFlag) {
        // cout << "bye" << endl;
        break;
      }
    }
    cout << numOfBooks;
  }
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
// const ll INF = 1LL << 30;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec) {
  rep(i, 0, sz(vec)) {
    if (i)
      cout << " ";
    cout << vec[i];
  }
  coel;
}

////////////////////////////////////////////////////////

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  Vl a(n + 1), b(m + 1);
  Rep(i, 1, n) cin >> a[i];
  Rep(i, 1, m) cin >> b[i];
  Rep(i, 1, n) a[i] += a[i - 1];
  Rep(i, 1, m) b[i] += b[i - 1];
  // print(a);
  // print(b);
  ll ans = 0;
  for (int i = n; i >= 0; i--) {
    if (a[i] > k)
      continue;
    ll s = k - a[i];
    ll j;
    auto it = upper_bound(ALL(b), s);
    j = it - 1 - b.begin();
    chmax(ans, i + j);
  }
  co(ans);

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
// const ll INF = 1LL << 30;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec) {
  rep(i, 0, sz(vec)) {
    if (i)
      cout << " ";
    cout << vec[i];
  }
  coel;
}

////////////////////////////////////////////////////////

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  Vl a(n + 1), b(m + 1);
  Rep(i, 1, n) cin >> a[i];
  Rep(i, 1, m) cin >> b[i];
  Rep(i, 1, n) a[i + 1] += a[i];
  Rep(i, 1, m) b[i + 1] += b[i];
  // print(a);
  // print(b);
  ll ans = 0;
  for (int i = n; i >= 0; i--) {
    if (a[i] > k)
      continue;
    ll s = k - a[i];
    ll j;
    auto it = upper_bound(ALL(b), s);
    if (it == b.end())
      j = 0;
    else
      j = it - 1 - b.begin();
    chmax(ans, i + j);
  }
  co(ans);

  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N, M, K;
  cin >> N >> M >> K;
  vector<int64_t> AAcc(N + 1), BAcc(M + 1);
  int64_t ans = 0;
  for (int64_t i = 0; i < N; i++) {
    cin >> AAcc[i + 1];
    AAcc[i + 1] += AAcc[i];
  }
  for (int64_t i = 0; i < M; i++) {
    cin >> BAcc[i + 1];
    BAcc[i + 1] += BAcc[i];
  }
  int64_t i = 0, j = M;
  while (i <= N) {
    while (j >= 0 && AAcc[i] + BAcc[j] > K) {
      j--;
    }
    if (j >= 0) {
      ans = max(ans, i + j);
    }
    i++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N, M, K;
  cin >> N >> M >> K;
  vector<int64_t> AAcc(N + 1), BAcc(M + 1);
  int64_t ans = 0;
  for (int64_t i = 0; i < N; i++) {
    cin >> AAcc[i + 1];
    AAcc[i + 1] += AAcc[i];
  }
  for (int64_t i = 0; i < M; i++) {
    cin >> BAcc[i + 1];
    BAcc[i + 1] += BAcc[i];
  }
  for (int64_t i = 0; i <= N; i++) {
    for (int64_t j = 0; j <= M; j++) {
      if (AAcc[i] + BAcc[j] <= K) {
        ans = max(ans, i + j);
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  vector<ll> b(m + 1);
  a[0] = 0;
  b[0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (ll i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    ll rem = k - a[i], pos;
    vector<ll>::iterator it = upper_bound(b.begin(), b.end(), rem);
    if (it == b.begin())
      pos = 0;
    else
      pos = it - 1 - b.begin();
    ans = max(ans, i + pos);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> v(n + 1);
  vector<ll> u(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> v[i];
  for (ll i = 1; i <= m; i++)
    cin >> u[i];
  ll res = 0, i = 1, j = 1;
  for (; i <= n && j <= m;) {
    ll mi = min(v[i], u[j]);
    if (k < mi)
      break;
    if (v[i] >= u[j]) {
      k -= u[j];
      res++;
      j++;
    } else {
      k -= v[i];
      res++;
      i++;
    }
  }
  while (i <= n) {
    if (v[i] > k)
      break;
    res++;
    k -= v[i];
    i++;
  }
  while (j <= m) {
    if (u[j] > k)
      break;
    res++;
    k -= u[j];
    j++;
  }
  cout << res << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void YN(bool ans) {
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  v A(N), B(M);
  rep(i, 0, N) cin >> A[i];
  rep(i, 0, M) cin >> B[i];
  ll sum = 0, books = 0, mb = 0;

  while (books < N) {
    if (A[books] + sum <= K) {
      sum += A[books];
      books++;
    } else {
      break;
    }
  }
  mb = books;
  ll i = books - 1;
  bool ok = true;

  rep(j, 0, M) {
    while (B[j] + sum > K) {
      if (i == -1) {
        ok = false;
        break;
      }
      sum -= A[i];
      i--;
      books--;
    }
    sum += B[j];
    books++;
    if (ok)
      mb = max(mb, books);
  }
  cout << mb;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void YN(bool ans) {
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  v A(N), B(M);
  rep(i, 0, N) cin >> A[i];
  rep(i, 0, M) cin >> B[i];
  ll sum = 0, books = 0, mb = 0;

  while (books < N) {
    if (A[books] + sum <= K) {
      sum += A[books];
      books++;
    } else {
      break;
    }
  }
  mb = books;
  ll i = books - 1;
  bool ok = true;

  rep(j, 0, M) {
    while (B[j] + sum > K) {
      if (i == -1) {
        ok = false;
      }
      sum -= A[i];
      i--;
      books--;
    }
    sum += B[j];
    books++;
    if (ok)
      mb = max(mb, books);
  }
  cout << mb;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m + 1);
  vector<ll> sum_a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sum_a[0] = a[0];
  for (int i = 1; i < n; ++i) {
    sum_a[i] = sum_a[i - 1] + a[i];
  }
  b[0] = 0;
  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
  }
  ll ans = 0;
  for (ll i = 0; i <= m; ++i) {
    k -= b[i];
    if (k < 0)
      break;
    ll tmp = upper_bound(sum_a.begin(), sum_a.end(), k) - sum_a.begin();
    ans = max(tmp + i, ans);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
  }
  queue<pair<ll, ll>> next;
  next.push({0, 0});
  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, 0));
  dp[0][0] = 0;
  ll ans = 0;
  while (!next.empty()) {
    ll x = next.front().first;
    ll y = next.front().second;
    next.pop();
    if (x < n) {
      dp[x + 1][y] = dp[x][y] + a[x];
      if (dp[x + 1][y] < k && x < n)
        next.push({x + 1, y});
      if (dp[x + 1][y] <= k)
        ans = max(ans, x + y + 1);
    }
    if (y < m) {
      dp[x][y + 1] = dp[x][y] + b[y];
      if (dp[x][y + 1] < k && y < m)
        next.push({x, y + 1});
      if (dp[x][y + 1] <= k)
        ans = max(ans, x + y + 1);
    }
    ans = max(ans, x + y);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (int &e : a)
    cin >> e;
  for (int &e : b)
    cin >> e;
  vector<long long> as(n + 1), bs(m + 1);
  for (int i = 0; i < n; ++i)
    as[i + 1] = as[i] + a[i];
  for (int i = 0; i < m; ++i)
    bs[i + 1] = bs[i] + b[i];
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    long long sum = as[i];
    if (sum > k)
      continue;
    int t =
        distance(bs.begin(), upper_bound(bs.begin(), bs.end(), k - sum)) - 1;
    ans = max(ans, t + i);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (int &e : a)
    cin >> e;
  for (int &e : b)
    cin >> e;
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    long long sum = 0;
    int t = i;
    for (int j = 0; j < i; ++j)
      sum += a[j];
    if (sum > k)
      continue;
    for (int j = 0; j < m; ++j) {
      if (sum + b[j] > k)
        break;
      sum += b[j];
      ++t;
    }
    ans = max(ans, t);
  }
  cout << ans << endl;

  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (ll i = a; i <= b; i++)
using namespace std;

int main(int argc, char const *argv[]) {
  // ifstream cin("input.txt");
  cout.sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> A(n);
  vector<ll> B(m);

  if (n > 0)
    cin >> A[0];
  rep(i, 1, n - 1) {
    cin >> A[i];
    A[i] += A[i - 1];
  }

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

  ll count = upper_bound(A.begin(), A.end(), k) - A.begin();
  if (count > n)
    count = 0;
  rep(i, 0, m - 1) {
    ll rem = k - B[i];
    if (rem < 0)
      break;
    ll bound = upper_bound(A.begin(), A.end(), rem) - A.begin();

    count = max(count, (i + 1 + bound));
  }

  cout << count;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; i++)
using namespace std;

int main(int argc, char const *argv[]) {
  // ifstream cin("input.txt");
  cout.sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> A(n);
  vector<int> B(m);
  rep(i, 0, n - 1) cin >> A[i];

  rep(i, 0, m - 1) cin >> B[i];

  int i = 0, j = 0, count = 0;

  while (i < n || j < n) {
    if (j >= m && k - A[i] >= 0)
      k -= A[i++];
    else if (i >= n && k - B[j] >= 0)
      k -= B[j++];
    else if (A[i] < B[i] && k - A[i] >= 0)
      k = k - A[i++];
    else if (A[i] >= B[i] && k - B[i] >= 0)
      k -= B[j++];
    else
      break;
    count++;
  }

  cout << count;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++) // rep(i,回数){処理}
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  // 入力
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  // 計算
  ll t = 0;
  rep(i, m) t += b[i];
  int j = m;
  int ans = 0;
  rep(i, n + 1) {
    while (j > 0 && t > k) {
      --j;
      t -= b[j];
    }
    if (t > k) {
      break;
    }
    ans = max(ans, i + j);
    if (i == n) {
      break;
    }
    t = t + a[i];
  }
  // 答えを出力
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++) // rep(i,回数){処理}
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  // 入力
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  // 計算
  ll t = 0;
  rep(i, m) { t = t + b[i]; }
  int j = m;
  int ans = 0;
  rep(i, n + 1) {
    while (j > 0 && t > k) {
      j = j - 1;
      t = t - b[i];
    }
    if (t > k) {
      break;
    }
    ans = max(ans, i + j);
    if (i == n) {
      break;
    }
    t = t + a[i];
  }
  // 答えを出力
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long int N, M, K;
  cin >> N >> M >> K;

  vector<long long int> desk_A(N);
  vector<long long int> desk_A_total(N + 1, 0);
  vector<long long int> desk_B(M);
  vector<long long int> desk_B_total(M + 1, 0);

  for (long long int i = 0; i < N; ++i) {
    cin >> desk_A[i];
    desk_A_total[i + 1] = desk_A_total[i] + desk_A[i];
  }

  for (long long int i = 0; i < M; ++i) {
    cin >> desk_B[i];
    desk_B_total[i + 1] = desk_B_total[i] + desk_B[i];
  }

  long long int ans = 0;
  long long int j = M;

  for (long long int i = 0; i < N + 1; ++i) {
    if (desk_A_total[i] > K) {
      break;
    }

    while (desk_B_total[j] + desk_A_total[i] > K) {
      j -= 1;
    }

    ans = max(ans, i + j);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int read_book(vector<long long int> &desk_A, vector<long long int> &desk_B,
              int A_index, int B_index, long long int time, long long int &K,
              int book_num, int N, int M) {

  int result_book_num_A = 0;

  if (A_index == N) {
    result_book_num_A = book_num;
  } else {
    long long int time_A = time + desk_A[A_index];

    if (time_A > K) {
      result_book_num_A = book_num;
    } else {
      result_book_num_A = read_book(desk_A, desk_B, A_index + 1, B_index,
                                    time_A, K, book_num + 1, N, M);
    }
  }

  int result_book_num_B = 0;

  if (B_index == M) {
    result_book_num_B = book_num;
  } else {
    long long int time_B = time + desk_B[B_index];

    if (time_B > K) {
      result_book_num_B = book_num;
    } else {
      result_book_num_B = read_book(desk_A, desk_B, A_index, B_index + 1,
                                    time_B, K, book_num + 1, N, M);
    }
  }

  return max(result_book_num_A, result_book_num_B);
}

int main() {
  int N, M;
  long long int K;
  cin >> N >> M >> K;

  vector<long long int> desk_A(N);
  vector<long long int> desk_B(M);

  for (int i = 0; i < N; ++i) {
    cin >> desk_A[i];
  }

  for (int i = 0; i < M; ++i) {
    cin >> desk_B[i];
  }

  cout << read_book(desk_A, desk_B, 0, 0, 0, K, 0, N, M) << endl;
}