alexjercan/bugnet huggingface dataset:
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  ll N;
  cin >> N;
  ll M;
  cin >> M;
  ll K;
  cin >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  vector<ll> B(M);
  for (ll i = 0; i < M; i++)
    cin >> B[i];

  vector<ll> sumA(N + 1, 0);
  vector<ll> sumB(M + 1, 0);
  for (ll i = 0; i < N; i++)
    sumA[i + 1] = sumA[i] + A[i];
  for (ll i = 0; i < M; i++)
    sumB[i + 1] = sumB[i] + B[i];

  ll ans = 0;
  ll b = 0;
  for (ll i = N; i >= 0; i--) {
    while (sumA[i] + sumB[b] <= K) {
      ans = max(ans, i + b);
      b++;
      if (b > M) {
        b = M;
        break;
      }
    }
  }

  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

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

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

  ll N;
  cin >> N;
  ll M;
  cin >> M;
  ll K;
  cin >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  vector<ll> B(M);
  for (ll i = 0; i < M; i++)
    cin >> B[i];

  vector<ll> sumA(N + 1, 0);
  vector<ll> sumB(M + 1, 0);
  for (ll i = 0; i < N; i++)
    sumA[i + 1] = sumA[i] + A[i];
  for (ll i = 0; i < M; i++)
    sumB[i + 1] = sumB[i] + B[i];

  ll ans = 0;
  ll b = 0;
  for (ll i = N; i >= 0; i--) {
    while (sumA[i] + sumB[b] <= K) {
      ans = max(ans, i + b);
      b++;
      b = min(b, M);
    }
  }

  cout << ans << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define endl '\\n'
#define int long long
#define eps 1e-9
#define oo 1e7 + 5

using namespace std;

typedef unsigned long long ull;
typedef vector<pair<int, pair<int, int>>> dirgraph;
typedef vector<vector<int>> graph;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n + 1);
  vector<int> b(m + 1);

  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }

  for (int i = 1; i < m + 1; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }

  int ans = 0;

  for (int i = 0; i <= n; i++) {
    if (a[i] <= k) {
      ans = max(ans, i);
      int l = 0;
      int r = m + 1;
      while (l + 1 < r) {
        int mid = (l + r) >> 1;
        if (b[mid] + a[i] <= k) {
          ans = max(ans, i + mid);
          l = mid;
        } else
          r = mid;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define int long long
#define eps 1e-9
#define oo 1e7 + 5

using namespace std;

typedef unsigned long long ull;
typedef vector<pair<int, pair<int, int>>> dirgraph;
typedef vector<vector<int>> graph;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i)
      a[i] += a[i - 1];
  }

  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i)
      b[i] += b[i - 1];
  }

  int ans = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] + b[j] <= k) {
        ans = max(ans, (i + 1 + j + 1));
      } else if (a[i] <= k) {
        ans = max(ans, i + 1);
      } else if (b[i] <= k) {
        ans = max(ans, j + 1);
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define ll long long
#define us unsigned short int
#define fin freopen("/home/imtiaz/Desktop/in.txt", "r", stdin)
#define fout freopen("/home/imtiaz/Desktop/out.txt", "w", stdout)
#define dbg(x) cerr << #x << ": " << x << endl
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  ll freqa[n + 1], freqb[m + 1];
  freqa[0] = freqb[0] = 0;

  // for (ll &tmp : a) //0 based input
  //     cin >> tmp;
  // for (ll &tmp : b)
  //     cin >> tmp;

  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &a[i - 1]);
    freqa[i] = freqa[i - 1] + a[i - 1]; // 1 based freqa
  }

  for (ll i = 1; i <= m; i++) {
    scanf("%lld", &b[i - 1]);
    freqb[i] = freqb[i - 1] + b[i - 1];
  }

  ll ans = 0;

  for (ll i = 0; i <= n and freqa[i] <= k; i++) {
    ll j = lower_bound(freqb, freqb + m, k - freqa[i]) - freqb;
    if (j > m or freqa[i] + freqb[j] > k)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define us unsigned short int
#define fin freopen("/home/imtiaz/Desktop/in.txt", "r", stdin)
#define fout freopen("/home/imtiaz/Desktop/out.txt", "w", stdout)
#define dbg(x) cerr << #x << ": " << x << endl
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  ll freqa[n + 1], freqb[m + 1];
  freqa[0] = freqb[0] = 0;

  for (ll &tmp : a) // 0 based input
    cin >> tmp;
  for (ll &tmp : b)
    cin >> tmp;

  for (ll i = 1; i <= n; i++)
    freqa[i] = freqa[i - 1] + a[i - 1]; // 1 based freqa
  for (ll i = 1; i <= m; i++)
    freqb[i] = freqb[i - 1] + b[i - 1];

  ll ans = 0;

  for (ll i = 0; i <= n and freqa[i] <= k; i++) {
    for (ll j = 0; j <= m and freqa[i] + freqb[j] <= k; j++) {
      ans = max(ans, i + j);
    }
  }
  cout << ans << endl;

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

#include <algorithm>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define rep(cnt, a, n)                                                         \\
  for (ll cnt = a; cnt < n + a; cnt++) // aから始めてn回ループ
#define vector2(type, name, a, b) vector<vector<type>> name(a, vector<type>(b))
#define PI 3.141592653589793

/*vector<ll> c;
vector<vector<ll>> s;
vector<ll>last;
vector<ll>t;
vector<bool>did;*/

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  vector<ll> a_sum(n + 1);
  vector<ll> b_sum(m + 1);
  rep(cnt, 0, n) { cin >> a.at(cnt); }
  rep(cnt, 0, m) { cin >> b.at(cnt); }
  a_sum[0] = 0;
  b_sum[0] = 0;
  rep(cnt, 0, n) { a_sum.at(cnt + 1) = a_sum.at(cnt) + a.at(cnt); }
  rep(cnt, 0, m) { b_sum.at(cnt + 1) = b_sum.at(cnt) + b.at(cnt); }

  ll a_read = 0;
  ll b_read = 0;
  ll best = 0;
  for (a_read = 0; a_read <= n; a_read++) {
    if (k >= a_sum.at(a_read)) {

    } else {
      break;
    }
  }
  a_read--;
  best = a_read;

  for (ll i = 1; i <= m; i++) {
    if (b_sum.at(i) > k) {
      break;
    }
    while (b_sum.at(i) + a_sum.at(a_read) > k) {
      a_read--;
      if (a_read < 0) {
        a_read = 0;
        break;
      }
    }
    best = max(best, i + a_read);
  }
  cout << best << endl;

  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define rep(cnt, a, n)                                                         \\
  for (ll cnt = a; cnt < n + a; cnt++) // aから始めてn回ループ
#define vector2(type, name, a, b) vector<vector<type>> name(a, vector<type>(b))
#define PI 3.141592653589793

/*vector<ll> c;
vector<vector<ll>> s;
vector<ll>last;
vector<ll>t;
vector<bool>did;*/

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  vector<ll> a_sum(n + 1);
  vector<ll> b_sum(m + 1);
  rep(cnt, 0, n) { cin >> a.at(cnt); }
  rep(cnt, 0, m) { cin >> b.at(cnt); }
  a_sum[0] = 0;
  b_sum[0] = 0;
  rep(cnt, 0, n) { a_sum.at(cnt + 1) = a_sum.at(cnt) + a.at(cnt); }
  rep(cnt, 0, m) { b_sum.at(cnt + 1) = b_sum.at(cnt) + b.at(cnt); }

  ll a_read = 0;
  ll b_read = 0;
  ll best = 0;
  for (a_read = 0; a_read <= n; a_read++) {
    if (k >= a_sum.at(a_read)) {

    } else {
      break;
    }
  }
  a_read--;
  /*rep(cnt, 0, m) {
      if (b_sum.at(b_read) + a_sum.at(a_read) <= k) {
          b_read++;

      }
      else {
          break;
      }
  }
  best = a_read + b_read;*/
  for (ll cnt = a_read; cnt >= 0; cnt--) {
    for (b_read = 0; b_read <= m; b_read++) {
      if (b_sum.at(b_read) + a_sum.at(cnt) <= k) {
      } else {
        break;
      }
    }
    b_read--;

    best = max(best, cnt + b_read);
  }

  cout << best << endl;

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

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

int main() {
  int n, m, c, r;
  uint64_t k, s = 0;
  vector<uint64_t> a;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    uint64_t t;
    cin >> t;
    s += t;
    if (s <= k) {
      a.push_back(s);
    }
  }

  s = 0;
  c = a.size();
  r = a.size();
  for (int i = 1; i <= m; i++) {
    uint64_t t;
    cin >> t;
    s += t;
    if (s <= k) {
      r = max(r, i);
      for (; c >= 1; c--) {
        if (a[c - 1] + s <= k) {
          r = max(r, c + i);
          break;
        }
      }
    }
  }
  cout << r << endl;
}


Error: WA

Fixed submission:

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

int main() {
  uint64_t n, m, r, c, k, s = 0;
  vector<uint64_t> a;
  cin >> n >> m >> k;
  for (uint64_t i = 0; i < n; i++) {
    uint64_t t;
    cin >> t;
    s += t;
    if (s <= k) {
      a.push_back(s);
    }
  }

  s = 0;
  c = a.size();
  r = a.size();
  for (uint64_t i = 0; i < m; i++) {
    uint64_t t;
    cin >> t;
    s += t;
    if (s <= k) {
      for (; c >= 0; c--) {
        if (c == 0) {
          if (i + 1 > r) {
            r = i + 1;
            break;
          }
        } else if (a[c - 1] + s <= k && c + i + 1 > r) {
          r = c + i + 1;
          break;
        }
      }
    }
  }
  cout << r << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
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;
  int ans = 0;
  int j = m - 1;
  rep(i, m) t += b[i];
  for (int i = 0; i < n + 1; i++) {
    while (t > k && j >= 0) {
      t -= b[j];
      j--;
    }
    if (t > k)
      break;
    ans = max(ans, i + j + 1);
    if (i == n)
      break;
    t += a[i];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
int main() {
  ll n, m;
  cin >> n >> m;
  ll k;
  cin >> k;
  queue<ll> a;
  queue<ll> b;
  rep(i, n) {
    ll x;
    cin >> x;
    a.push(x);
  }
  rep(i, m) {
    ll x;
    cin >> x;
    b.push(x);
  }
  ll cnt = 0;
  ll ans = 0;
  while (cnt < k && (!a.empty() || !b.empty())) {
    if (b.empty()) {
      cnt += a.front();
      ans++;
      a.pop();
    }
    if (a.front() < b.front() && !a.empty()) {
      cnt += a.front();
      ans++;
      a.pop();
    } else {
      cnt += b.front();
      ans++;
      b.pop();
    }
  }
  if (cnt > k)
    ans--;
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr int64_t LINF = LLONG_MAX / 2;

int main() {
  int64_t N, M, K;
  cin >> N >> M >> K;
  vector<int64_t> v(N), w(M);
  vector<int64_t> vv(N), ww(M);
  rep(i, N) {
    cin >> v.at(i);
    if (i == 0) {
      vv.at(i) = v.at(i);
    } else {
      vv.at(i) = v.at(i) + vv.at(i - 1);
    }
  }
  rep(i, M) {
    cin >> w.at(i);
    if (i == 0) {
      ww.at(i) = w.at(i);
    } else {
      ww.at(i) = w.at(i) + ww.at(i - 1);
    }
  }
  if (v.at(0) > K && w.at(0) > K) {
    std::cout << "0" << '\\n';
    return 0;
  }

  int64_t a = 0, b = 0, c = 0, x, y, z;
  rep(i, N + 1) {
    if (i == 0)
      x = 0;
    else
      x = vv.at(i - 1);
    if (x > K)
      continue;
    y = 0;
    z = M;
    do {
      if (x + ww.at(0) > K) {
        y = -1;
        break;
      }
      // std::cout << ((y+z)/2) << " " << ww.at((y+z)/2)  << '\\n';
      if (x + ww.at((y + z) / 2) <= K) {
        y = (y + z) / 2;
      } else {
        z = (y + z) / 2;
      }
    } while (y + 1 != z);
    chmax(a, i + y + 1);
    // std::cout << a << " " << y << z << '\\n';
  }

  std::cout << a << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define all(v) v.begin(), v.end()
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr double PI = 3.14159265358979323846;
constexpr int INF = INT_MAX / 2;

int main() {
  int64_t N, M, K;
  cin >> N >> M >> K;
  vector<int64_t> v(N), w(M);
  rep(i, N) { cin >> v.at(i); }
  rep(i, M) { cin >> w.at(i); }

  int64_t a = 0, b = 0, c = 0, x, y, z;
  rep(i, N + M) {
    x = INF;
    y = INF;
    if (a < N) {
      x = v.at(a);
    }
    if (b < M) {
      y = w.at(a);
    }
    if (x < y) {
      z = x;
      a++;
    } else {
      z = y;
      b++;
    }
    if (K < z) {
      break;
    } else {
      K -= z;
      c++;
    }
    if (a == N && b == M) {
      break;
    }
  }

  std::cout << c << '\\n';
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> a(n + 1), b(m + 1);

  a.at(0) = 0;
  for (long long i = 1; i <= n; ++i) {
    long long ai;
    cin >> ai;
    a.at(i) = a.at(i - 1) + ai;
  }
  b.at(0) = 0;
  for (long long i = 1; i <= m; ++i) {
    long long bi;
    cin >> bi;
    b.at(i) = b.at(i - 1) + bi;
  }
  long long ans = 0;
  long long j = m;
  for (long long i = 0; i <= n; ++i) {
    while (b.at(j) + a.at(i) > k) {
      --j;
      if (j < 0)
        break;
    }
    if (j >= 0 && i + j > ans)
      ans = i + j;
    if (j < 0)
      break;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; ++i)
    cin >> a.at(i);
  for (int i = 0; i < m; ++i)
    cin >> b.at(i);

  queue<pair<pair<int, int>, int>> q;
  int ans = 0;
  q.push(make_pair(make_pair(-1, -1), k));
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    int i = v.first.first;
    int j = v.first.second;
    int kk = v.second;
    if (kk >= 0) {
      if (i + 1 < n) {
        q.push(make_pair(make_pair(i + 1, j), kk - a.at(i + 1)));
      }
      if (j + 1 < m) {
        q.push(make_pair(make_pair(i, j + 1), kk - b.at(j + 1)));
      }
    } else {
      if (i + j + 1 > ans)
        ans = i + j + 1;
    }
    if (i == n - 1 && j == m - 1) {
      if (i + j + 2 > ans)
        ans = i + j + 2;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define pb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define pii pair<int, int>

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (auto &c : a)
    cin >> c;
  for (auto &c : b)
    cin >> c;
  ll now = 0;
  for (auto &c : a)
    now += c;
  int j = 0, ans = 0;
  for (int i = n; i >= 0; i--) {
    if (i < n)
      now -= a[i];
    while (j < m && now + b[j] <= k) {
      now += b[j];
      j++;
    }
    if (now <= k)
      ans = max(ans, i + j);
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define pii pair<int, int>

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (auto &c : a)
    cin >> c;
  for (auto &c : b)
    cin >> c;
  ll now = 0;
  for (auto &c : a)
    now += c;
  int j = 0, ans = 0;
  for (int i = n; i >= 0; i--) {
    if (i < n)
      now -= a[i];
    while (j < m && now + a[j] <= k) {
      now += a[j];
      j++;
    }
    if (now <= k)
      ans = max(ans, i + j);
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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.at(i);
  }
  for (int i = 0; i < m; i++) {
    cin >> b.at(i);
  }

  ll t = 0;
  for (int i = 0; i < m; i++) {
    t += b.at(i);
  }

  int j = m;
  int ans = 0;
  for (int i = 0; i < n + 1; i++) {
    while (j && t > k) {
      j--;
      t -= b.at(j);
    }
    if (t > k) {
      break;
    }
    ans = max(ans, i + j);
    if (i == n) {
      break;
    }
    t += a.at(i);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a, b;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.push_back(x);
  }
  for (int i = 0; i < m; i++) {
    ll x;
    cin >> x;
    b.push_back(x);
  }
  ll sum = 0;
  while (a.size() || b.size()) {
    ll c = 0;
    ll d = 0;
    if (a.size()) {
      c = a.at(0);
    }
    if (b.size()) {
      d = b.at(0);
    }
    if (k < min(c, d)) {
      break;
    }
    if (c && d) {
      if (c < d) {
        k -= c;
        a.erase(a.begin());
      } else if (c > d) {
        k -= d;
        b.erase(b.begin());
      } else {
        k -= c;
        if (a.at(1) <= b.at(1)) {
          a.erase(a.begin());
        } else {
          b.erase(b.begin());
        }
      }
    } else if (c) {
      k -= c;
      a.erase(a.begin());
    } else if (d) {
      k -= d;
      b.erase(b.begin());
    }
    sum++;
  }
  cout << sum << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INF = 1001001001;

int main() {
  int N, M, K;
  int ans = 0;

  cin >> N >> M >> K;

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

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

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

  vector<ll> a(N + 1, 0), b(M + 1, 0);

  for (int i = 1; i <= N; i++) {
    a[i] = a[i - 1] + A[i - 1];
  }

  for (int i = 1; i <= M; i++) {
    b[i] = b[i - 1] + B[i - 1];
  }

  int 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 << endl;

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INF = 1001001001;

int main() {
  ll N, M, K, k = 0;
  int ans = 0;

  cin >> N >> M >> K;

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

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

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

  vector<ll> a(N, 0), b(M, 0);

  for (int i = 1; i <= N; i++) {
    a[i] = a[i - 1] + A[i];
  }

  for (int i = 1; i <= M; i++) {
    b[i] = b[i - 1] + B[i];
  }

  int j = M;

  for (int i = 0; i <= N; i++) {
    if (a[i] > K)
      break;
    while (b[j] > K - a[i])
      j -= 1;
    ans = max(ans, i + j);
  }

  cout << ans << endl;

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

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

ll max_bc = 0;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> A(n + 1);
  vector<ll> B(m + 1);

  for (ll i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> B[i];
  }

  vector<ll> a;
  a.push_back(0);
  vector<ll> b;
  b.push_back(0);

  for (ll i = 0; i < n; i++) {
    a.push_back(a[i] + A[i]);
  }
  for (ll i = 0; i < m; i++) {
    b.push_back(b[i] + B[i]);
  }

  ll ans = 0;
  ll j = m;

  for (ll i = 0; i < (n + 1); i++) {
    if (a[i] > k)
      break;

    while (b[j] > (k - a[i])) {
      j--;
    }

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

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

Error: TLE

Fixed submission:

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

ll max_bc = 0;

int dfs(vector<ll> a, vector<ll> b, ll ai, ll bi, ll bc, ll r, ll k, ll n,
        ll m) {

  if (r > k) {
    return 0;
  }
  if (ai > n || bi > m) {
    return 0;
  }
  max_bc = max(bc, max_bc);

  // cout << ai << " " << bi << " " << bc << " " << r << endl;
  if (ai <= n - 1) {
    dfs(a, b, ai + 1, bi, bc + 1, r + a[ai], k, n, m);
  }
  if (bi <= m - 1) {
    dfs(a, b, ai, bi + 1, bc + 1, r + b[bi], k, n, m);
  }
  return 1;
}

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

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }

  ll ai, bi, bc, r;
  ai = 0, bi = 0, bc = 1, r = a[ai];
  dfs(a, b, ++ai, bi, bc, r, k, n, m);
  ai = 0, bi = 0, bc = 1, r = b[bi];
  dfs(a, b, ai, ++bi, bc, r, k, n, m);

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  vector<ll> pa, pb;
  pa.push_back(0);
  pb.push_back(0);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (ll i = 0; i < n; i++) {
    pa.push_back(pa[i] + a[i]);
  }
  for (ll i = 0; i < m; i++) {
    pb.push_back(pb[i] + b[i]);
  }

  ll ans = 0;
  ll j = m;
  for (ll i = 0; i <= n; i++) {
    if (pa[i] > k) {
      break;
    }
    while (pa[i] + pb[j] > k) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  vector<ll> pa, pb;
  pa.push_back(0);
  pb.push_back(0);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (ll i = 0; i < n; i++) {
    pa.push_back(pa[i] + a[i]);
  }
  for (ll i = 0; i < m; i++) {
    pb.push_back(pb[i] + b[i]);
  }

  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (pa[i] > k) {
      break;
    }
    ll bcnt = 0;
    for (ll j = 0; j <= m; j++) {
      if (pa[i] + pb[j] > k) {
        break;
      }
      bcnt = j;
    }
    ans = max(ans, i + bcnt);
  }
  cout << ans << endl;
  return 0;
}