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

#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  long long int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<long long int> a(n); // size
  std::vector<long long int> b(m); // size

  for (int i = 0; i < n; i++)
    std::cin >> a[i];

  for (int i = 0; i < m; i++)
    std::cin >> b[i];

  // by using perfix sum
  std::vector<long long int> aSUM(n + 1); // size
  std::vector<long long int> bSUM(m + 1); // size
  int count = 0;
  for (int i = 0; i < n; i++)
    aSUM[i + 1] = aSUM[i] + a[i];

  for (int i = 0; i < m; i++)
    bSUM[i + 1] = bSUM[i] + b[i];

  // 0 60 and so on
  for (int i = 0; i <= n; i++) {
    long long int other = k - aSUM[i];
    if (other < 0)
      continue;
    // here get the position of other from bSum
    int l = lower_bound(bSUM.begin(), bSUM.end(), other) - bSUM.begin();
    if (l > m || bSUM[l] > other)
      --l;
    count = std::max(count, i + l);
  }
  std::cout << count << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  long long int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<long long int> a(n); // size
  std::vector<long long int> b(m); // size

  for (int i = 0; i < n; i++)
    std::cin >> a[i];

  for (int i = 0; i < m; i++)
    std::cin >> b[i];

  // by using perfix sum
  std::vector<long long int> aSUM(n + 1); // size
  std::vector<long long int> bSUM(n + 1); // size
  int count = 0;
  for (int i = 0; i < n; i++)
    aSUM[i + 1] = aSUM[i] + a[i];

  for (int i = 0; i < m; i++)
    bSUM[i + 1] = bSUM[i] + b[i];

  // 0 60 and so on
  for (int i = 0; i <= n; i++) {
    long long int other = k - aSUM[i];
    if (other < 0)
      continue;
    // here get the position of other from bSum
    int l = lower_bound(bSUM.begin(), bSUM.end(), other) - bSUM.begin();
    if (l > m || bSUM[l] > other)
      --l;
    count = std::max(count, i + l);
  }
  std::cout << count << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, l, r) for (int i = l; i <= (int)(r); i++)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define MOD (int)(1e9 + 7)
#define INF (int)1e9
#define LLINF (ll)1e18

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  rep(i, n) cin >> a[i];
  rep(i, n - 1) { a[i + 1] += a[i]; }
  rep(i, m) cin >> b[i];
  rep(i, m - 1) { b[i + 1] += b[i]; }

  int ans = upper_bound(b, b + m, k) - b;
  // printf("a: %d, b: %d\\n", 0, ans);
  rep(i, n) {
    if (a[i] > k)
      break;
    int tmp = upper_bound(b, b + m, k - a[i]) - b;
    // printf("a: %d, b: %d\\n", i+1, tmp);
    chmax(ans, tmp + i + 1);
  }

  cout << ans << endl;
  // printf("%d\\n", N);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, l, r) for (int i = l; i <= (int)(r); i++)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define MOD (int)(1e9 + 7)
#define INF (int)1e9
#define LLINF (ll)1e18

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  queue<int> qa, qb;
  rep(i, n) {
    int a;
    cin >> a;
    qa.push(a);
  }
  rep(i, m) {
    int b;
    cin >> b;
    qb.push(b);
  }

  int msum = 0;
  int ans = 0;
  rep(i, n + m) {
    int ta = qa.front();
    int tb = qb.front();
    if (ta < tb) {
      qa.pop();
      msum += ta;
    } else {
      qb.pop();
      msum += tb;
    }
    if (msum > k)
      break;
    ans++;
  }
  cout << ans << endl;
  // printf("%d\\n", N);
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  long long N, M, K;
  cin >> N >> M >> K;
  long long a, b;
  long long a_idx = N - 1;
  vector<long long> asum(N), bsum(M);
  for (long long i = 0; i < N; i++) {
    if (i == 0) {
      cin >> asum.at(i);
    } else {
      cin >> a;
      asum.at(i) = asum.at(i - 1) + a;
      if (a_idx == N - 1 && asum.at(i) > K) {
        a_idx = i - 1;
      }
    }
  }
  long long rest = K - asum.at(a_idx);
  long long pre_b_idx = 0;
  for (long long i = 0; i < M; i++) {
    if (i == 0) {
      cin >> bsum.at(i);
    } else {
      cin >> b;
      bsum.at(i) = bsum.at(i - 1) + b;
      if (rest >= bsum.at(i)) {
        pre_b_idx = i;
      }
    }
  }

  int i, j;
  long long ans = 0;
  for (i = a_idx; i >= -1; i--) {
    if (i >= 0)
      rest = K - asum.at(i);
    else
      rest = K;
    if (rest < 0)
      continue;
    for (j = pre_b_idx; j < M; j++) {
      if (rest < bsum.at(j)) {
        pre_b_idx = j;
        break;
      }
    }
    j--;
    ans = max(ans, (long long)(i + 1) + (j + 1));
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  long long N, M, K;
  cin >> N >> M >> K;
  long long a, b;
  long long a_idx = N - 1;
  vector<long long> asum(N), bsum(M);
  for (long long i = 0; i < N; i++) {
    if (i == 0) {
      cin >> asum.at(i);
    } else {
      cin >> a;
      asum.at(i) = asum.at(i - 1) + a;
      if (a_idx == N - 1 && asum.at(i) > K) {
        a_idx = i - 1;
      }
    }
  }
  for (long long i = 0; i < M; i++) {
    if (i == 0) {
      cin >> bsum.at(i);
    } else {
      cin >> b;
      bsum.at(i) = bsum.at(i - 1) + b;
    }
  }

  int i, j;
  long long ans = 0;
  long long pre_b_idx = 0;
  for (i = a_idx; i >= -1; i--) {
    long long rest;
    if (i >= 0)
      rest = K - asum.at(i);
    else
      rest = K;
    if (rest < 0)
      continue;
    for (j = pre_b_idx; j < M; j++) {
      if (rest < bsum.at(j)) {
        pre_b_idx = j;
        break;
      }
    }
    j--;
    ans = max(ans, (long long)(i + 1) + (j + 1));
  }

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

#include <algorithm>
#include <iostream>
#include <math.h>

using namespace std;

int main() {
  long long n, m, k, cnt = 0;
  long long a[300000] = {0};
  long long b[300000] = {0};

  int debug;
  cin >> n >> m >> k;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    // a[i] = 1000000000;
    if (i == 0)
      continue;
    if (2147483646 - a[i] < a[i - 1])
      a[i] = 2147483646;
    else if (i != 0)
      a[i] += a[i - 1];
  }
  // cout << "HERE" << endl;

  for (long long i = 0; i < m; i++) {
    cin >> b[i];
    // b[i] = 1000000000;
    if (i == 0)
      continue;
    if (2147483646 - b[i] < b[i - 1])
      b[i] = 2147483646;
    else if (i != 0)
      b[i] += b[i - 1];
  }
  // cout << "HERE2" << endl;

  long long nn = n, mm = m;
  for (long long i = 0; i < n; i++) {
    if (a[i] > k) {
      nn = i;
      break;
    }
  }
  for (long long i = 0; i < m; i++) {
    if (b[i] > k) {
      mm = i;
      break;
    }
  }
  // if(mm == 0) mm = m;
  // if (nn == 0) nn = n;
  long long jj = mm - 1;
  for (long long i = 0; i < nn; i++) {
    for (long long j = jj; j >= 0; j--) {
      // cout << i << ' ' << j << endl;
      if (2147483646 - a[i] < b[j])
        continue;
      if (a[i] + b[j] <= k) {
        jj = j;
        cnt = max(cnt, i + j + 2);
        break;
      }
    }
  }

  // cout << "HERE3" << endl;

  for (long long i = nn - 1; i >= 0; i--) {
    if (a[i] <= k) {
      cnt = max(cnt, i + 1);
      break;
    }
  }
  for (long long i = mm - 1; i >= 0; i--) {
    if (b[i] <= k) {
      cnt = max(cnt, i + 1);
      break;
    }
  }

  cout << cnt;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>

using namespace std;

int main() {
  long long n, m, k, cnt = 0;
  long long a[300000] = {0};
  long long b[300000] = {0};

  cin >> n >> m >> k;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0)
      continue;
    if (2147483646 - a[i] < a[i - 1])
      a[i] = 2147483646;
    else if (i != 0)
      a[i] += a[i - 1];
  }

  for (long long i = 0; i < m; i++) {
    cin >> b[i];
    if (i == 0)
      continue;
    if (2147483646 - b[i] < b[i - 1])
      b[i] = 2147483646;
    else if (i != 0)
      b[i] += b[i - 1];
  }
  long long nn = n, mm = m;
  for (long long i = 0; i < n; i++) {
    if (a[i] > k)
      nn = i;
  }
  for (long long i = 0; i < m; i++) {
    if (b[i] > k)
      mm = i;
  }
  if (mm == 0)
    mm = m;
  if (nn == 0)
    nn = n;
  long long jj = mm - 1;
  for (long long i = 0; i < n; i++) {
    for (long long j = jj; j >= 0; j--) {
      if (2147483646 - a[i] < b[j])
        continue;
      if (a[i] + b[j] <= k) {
        jj = j;
        cnt = max(cnt, i + j + 2);
        break;
      }
    }
  }

  for (long long i = nn - 1; i >= 0; i--) {
    if (a[i] <= k) {
      cnt = max(cnt, i + 1);
      break;
    }
  }
  for (long long i = mm - 1; i >= 0; i--) {
    if (b[i] <= k) {
      cnt = max(cnt, i + 1);
      break;
    }
  }

  cout << cnt;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n], b[m];
  std::vector<long long int> va, vb; // sum of a[i],b[i]

  long long int tmp = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    tmp += a[i];
    va.push_back(tmp);
  }
  tmp = 0;
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    tmp += b[i];
    vb.push_back(tmp);
  }

  long long int ans = 0;
  for (int i = 0; i <= n; i++) {
    long long int num = k;
    if (i != 0)
      num = k - va[i - 1];
    if (num < 0)
      continue;

    std::vector<long long int>::iterator itr =
        std::upper_bound(vb.begin(), vb.end(), num);
    std::size_t pos = std::distance(vb.begin(), itr);

    long long int sum;
    if (pos == 0) {
      sum = i; // Bからは取れない
    } else {
      sum = i + pos;
    }

    if (ans < sum)
      ans = sum;
    // cout << num << ":" << i << ":" << pos << ":" << *itr << ":" << ans <<
    // endl;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n], b[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }

  long long int samA = 0;
  long long int samB = 0;
  unsigned long long int sumMAX = 0;
  long long int ans = 0;

  for (int i = 0; i <= n; i++) {
    if (i != 0)
      samA += a[i - 1];
    for (int j = 0; j <= m; j++) {
      if (j != 0)
        samB += b[j - 1];
      // cout << "sum:" << samA+samB << endl;
      if (k >= samA + samB) {
        if (i + j > ans)
          ans = i + j;
        // cout << ans << endl;
      }
    }
    samB = 0;
  }

  cout << ans << endl;

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

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

void printVector(vector<long long int> v) {
  for (auto x : v) {
    cout << x << ' ';
  }
  cout << '\\n';
}

int main() {
  long long int n, m, k;
  cin >> n >> m >> k;
  vector<long long int> A, B;
  for (int i = 0; i <= n - 1; i++) {
    long long int a;
    cin >> a;
    A.push_back(a);
  }
  for (int i = 0; i <= m - 1; i++) {
    long long int a;
    cin >> a;
    B.push_back(a);
  }
  // printVector(A);
  // printVector(B);
  vector<long long int> A_Acc;
  vector<long long int> B_Acc;
  A_Acc.push_back(0);
  B_Acc.push_back(0);
  for (int i = 0; i <= n - 1; i++) {
    A_Acc.push_back(A_Acc[i] + A[i]);
  }
  for (int i = 0; i <= m - 1; i++) {
    B_Acc.push_back(B_Acc[i] + B[i]);
  }
  // printVector(A_Acc);
  // printVector(B_Acc);
  long long int j = m;
  long long int result = 0;
  for (int i = 0; i <= n; i++) {
    if (A_Acc[i] > k)
      break;
    while (A_Acc[i] + B_Acc[j] > k) {
      j--;
    }
    result = max(result, i + j);
  }
  cout << result << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

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

vector<long long int> A, B;
long long int k;
int debugger = 0;

long long int solve(long long int totalLivro, long long int totalTempo,
                    long long int i, long long int j) {

  // cout << totalLivro << '\\n';
  // cout << totalTempo + A[i] << ' ' << totalTempo + B[j] << '\\n';
  /*
  debugger++;
  if (debugger > 30) {
      return 0;
  }
  */
  if (totalTempo + A[i] <= k && totalTempo + B[j] <= k) {
    // cout << "First" << '\\n';
    return max(solve(totalLivro + 1, totalTempo + A[i], i + 1, j),
               solve(totalLivro + 1, totalTempo + B[j], i, j + 1));
  } else if (totalTempo + A[i] <= k && totalTempo + B[j] > k) {
    // cout << "Second" << '\\n';
    return solve(totalLivro + 1, totalTempo + A[i], i + 1, j);
  } else if (totalTempo + A[i] > k && totalTempo + B[j] <= k) {
    // cout << "Third" << '\\n';
    return solve(totalLivro + 1, totalTempo + B[j], i, j + 1);
  } else {
    // cout << "HERE" << '\\n';
    return totalLivro;
  }
}

void printVector(vector<long long int> v) {
  for (auto x : v) {
    cout << x << ' ';
  }
  cout << '\\n';
}

int main() {
  long long int n, m;
  cin >> n >> m >> k;
  for (int i = 0; i <= n - 1; i++) {
    long long int a;
    cin >> a;
    A.push_back(a);
  }
  for (int i = 0; i <= m - 1; i++) {
    long long int a;
    cin >> a;
    B.push_back(a);
  }
  A.push_back(pow(10, 9) + 1);
  B.push_back(pow(10, 9) + 1);
  // printVector(A);
  // printVector(B);
  long long int totalLivro, totalTempo;
  totalLivro = 0;
  totalTempo = 0;
  long long int result = solve(totalLivro, totalTempo, 0, 0);
  cout << result << '\\n';
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define SZ(c) int(c.size())
#define pb push_back
#define loop(i, start, n) for (int i = start; i < n; i++)
#define rloop(i, start, n) for (int i = start; i > n; i--)

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
const ll inf = 2e9 + 3;

void solve() {
  int tc = 1;
  // cin >> tc;
  while (tc--) {
    ll n, m, k, res = 0;
    cin >> n >> m >> k;
    vll va(n + 1), vb(m + 1);
    ll A = 0, B = 0;
    loop(i, 1, n + 1) {
      cin >> va[i];
      va[i] = va[i] + va[i - 1];
    }
    loop(i, 1, m + 1) {
      cin >> vb[i];
      vb[i] = vb[i] + vb[i - 1];
    }
    ll b = m, a = n;
    loop(i, 1, n + 1) if (va[i] <= k) a = i;
    loop(i, 1, m + 1) if (vb[i] <= k) b = i;
    loop(i, 0, a + 1) {
      if (vb[b] <= k - va[i])
        res = max(res, i + b);
      else
        b--;
      /*loop(j,0,b+1) {
          if (va[i] + vb[j] <= k) res = max(res,(ll)(i+j));
      }*/
    }
    cout << res;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define SZ(c) int(c.size())
#define pb push_back
#define loop(i, start, n) for (int i = start; i < n; i++)
#define rloop(i, start, n) for (int i = start; i > n; i--)

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
const ll inf = 2e9 + 3;

void solve() {
  int tc = 1;
  // cin >> tc;
  while (tc--) {
    ll n, m, k, res = 0;
    cin >> n >> m >> k;
    vll va(n + 1), vb(m + 1);
    ll A = 0, B = 0;
    loop(i, 1, n + 1) {
      cin >> va[i];
      va[i] = va[i] + va[i - 1];
    }
    loop(i, 1, m + 1) {
      cin >> vb[i];
      vb[i] = vb[i] + vb[i - 1];
    }
    ll b = m, a = n;
    loop(i, 1, n + 1) if (va[i] <= k) a = i;
    loop(i, 1, m + 1) if (vb[i] <= k) b = i;
    // res = a;
    rloop(i, a, -1) {
      loop(j, 0, b + 1) {
        if (va[i] + vb[j] <= k)
          res = max(res, (ll)(i + j));
      }
    }
    cout << res;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  vector<ll> c(n + 1), d(m + 1);
  rep(i, n) c[i + 1] = c[i] + a[i];
  rep(j, m) d[j + 1] = d[j] + b[j];

  ll ans = 0;
  rep(i, n + 1) {
    ll time = c[i];
    if (time > k)
      continue;
    int p = upper_bound(d.begin(), d.end(), k - time) - d.begin();
    if (p == m && time + d[m] <= k)
      ans = max(ans, (ll)p + i);
    else
      ans = max(ans, (ll)p + i - 1);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  vector<ll> c(n + 1), d(n + 1);
  rep(i, n) c[i + 1] = c[i] + a[i];
  rep(j, m) d[j + 1] = d[j] + b[j];

  ll ans = 0;
  rep(i, n + 1) {
    ll time = c[i];
    if (time > k)
      continue;
    int p = upper_bound(d.begin(), d.end(), k - time) - d.begin();
    if (p == m && time + d[m] <= k)
      ans = max(ans, (ll)p + i);
    else
      ans = max(ans, (ll)p + i - 1);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, M, K;

int main() {
  cin >> N >> M >> K;
  vector<long long> A(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i + 1];
    A[i + 1] += A[i];
  }

  vector<long long> B(M + 1);
  for (int i = 0; i < M; i++) {
    cin >> B[i + 1];
    B[i + 1] += B[i];
  }
  int ans = 0;
  int last = M;
  for (int i = 0; i <= N; i++) {
    if (A[i] > K)
      break;
    while (B[last] > K - A[i]) {
      last -= 1;
    }
    ans = max(ans, i + last);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int N, M, K;

int recurrent(int T, int i, int j, vector<int> &A, vector<int> &B) {
  if (T > K)
    return i + j - 1;
  else if (i == N && j == M)
    return N + M;
  else if (i == N)
    return recurrent(T + B[j], i, j + 1, A, B);
  else if (j == M)
    return recurrent(T + A[i], i + 1, j, A, B);
  else
    return max(recurrent(T + A[i], i + 1, j, A, B),
               recurrent(T + B[j], i, j + 1, A, B));
}

int main() {
  cin >> N >> M >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<int> B(M);
  for (int i = 0; i < M; i++)
    cin >> B[i];
  // vector<vector<vector<int>>> dp(N,vector<vector<int>>(M,vector<int>(K+1)));
  cout << recurrent(0, 0, 0, A, B) << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);
  ll asum = 0, bsum = 0;
  for (int i = 1; i <= n; i++) {
    ll ai;
    cin >> ai;
    asum += ai;
    a[i] = asum;
  }
  for (int i = 1; i <= m; i++) {
    ll bi;
    cin >> bi;
    bsum += bi;
    b[i] = bsum;
  }

  ll ans = 0;
  ll cnt = m;
  for (ll i = 0; i <= n; i++) {
    ll ka = k - a[i];
    if (ka == 0) {
      ans = max(ans, i);
      cout << ans << endl;
      return 0;
    } else if (ka < 0) {
      cout << ans << endl;
      return 0;
    }
    for (ll j = cnt; j >= 0; j--) {
      ll kab = ka - b[j];
      if (kab >= 0) {
        ans = max(ans, i + j);
        cnt = j;
        break;
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);
  ll asum = 0, bsum = 0;
  for (int i = 1; i <= n; i++) {
    ll ai;
    cin >> ai;
    asum += ai;
    a[i] = asum;
  }
  for (int i = 1; i <= m; i++) {
    ll bi;
    cin >> bi;
    bsum += bi;
    b[i] = bsum;
  }

  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll ka = k - a[i];
    if (ka == 0) {
      ans = max(ans, i);
      cout << ans << endl;
      return 0;
    } else if (ka < 0) {
      cout << ans << endl;
      return 0;
    }
    for (ll j = 0; j <= m; j++) {
      ll kab = ka - b[j];
      if (kab >= 0)
        ans = max(ans, i + j);
      else if (kab < 0)
        break;
    }
  }

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

#include <climits>
#include <iostream>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n], b[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }

  int t = 0, la = n, lb = m;
  for (int i = 0; i < n; i++) {
    if (t + a[i] <= k) {
      t += a[i];
    } else {
      la = i;
      break;
    }
  }
  for (int i = 0; i < m; i++) {
    if (t + b[i] <= k) {
      t += b[i];
    } else {
      lb = i;
      break;
    }
  }
  int ans = la + lb;
  const int la1 = la, lb1 = lb;

  for (int j = 0; j < la1; j++) {
    la--;
    t -= a[la];
    for (int i = lb; i < m; i++) {
      if (t + b[i] <= k) {
        t += b[i];
      } else {
        lb = i;
        break;
      }
      if (i == m - 1)
        lb = m;
    }
    if (la + lb > ans)
      ans = la + lb;
  }

  cout << ans;

  return 0;
}

Error: -11

Fixed submission:

#include <climits>
#include <iostream>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n], b[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }

  int t = 0, la = n, lb = m;
  for (int i = 0; i < n; i++) {
    if (t + a[i] <= k) {
      t += a[i];
    } else {
      la = i;
      break;
    }
  }
  for (int i = 0; i < m; i++) {
    if (t + b[i] <= k) {
      t += b[i];
    } else {
      lb = i;
      break;
    }
  }
  int ans = la + lb;

  for (int j = 0; j < la; j--) {
    la--;
    t -= a[la];
    for (int i = lb; i < m; i++) {
      if (t + b[i] <= k) {
        t += b[i];
      } else {
        lb = i;
        break;
      }
    }
    if (la + lb < ans)
      ans = la + lb;
  }

  cout << ans;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N, M;
  ll K, p;
  cin >> N >> M >> K;
  vector<ll> a(N + 1), b(M + 1);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < N; i++) {
    cin >> p;
    a[i + 1] = a[i] + p;
  }
  for (int i = 0; i < M; i++) {
    cin >> p;
    b[i + 1] = b[i] + p;
  }
  int ans = 0, j = M;
  for (int i = 0; i <= N; i++) {
    if (a[i] > K)
      break;
    while (b[j] > K - a[i])
      j--;
    ans = max(ans, i + j);
  }
  cout << ans;
}


Error: WA

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, k, p, sum = 0;
  cin >> n >> m >> k;
  vector<int> v, w;
  for (int i = 0; i < n; i++) {
    cin >> p;
    v.push_back(p);
  }
  for (int i = 0; i < m; i++) {
    cin >> p;
    w.push_back(p);
  }
  int indxa = 0, indxb = 0, cnt = 0;
  while (sum <= k && cnt < n + m) {
    if (v[indxa] < w[indxb] && v[indxa] != v.size()) {
      sum += v[indxa];
      indxa++;
    } else {
      sum += w[indxb];
      indxb++;
    }
    cnt++;
  }
  if (sum <= k)
    cout << cnt;
  else
    cout << cnt - 1;
}