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

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const ll INF = 1e18;

int main() {
  ll n, k, m, ans = 0, idxa = -1, idxb = -1;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), sa(n + 1, 0), sb(m + 1, 0);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  // ll dp[n + 1][m + 1]={};

  rep(i, n) sa[i + 1] = sa[i] + a[i];
  rep(i, m) sb[i + 1] = sb[i] + b[i];

  rep(i, n + 1) {
    ll t = k - sa[i];
    ans = max(ans, upper_bound(sb.begin(), sb.end(), t) - sb.begin() + i - 1);
    if (t < 0)
      break;
    // cout << ans << endl;
  }

  // rep(i, idxa) rep(j, idxb) {
  //   if (sa[i] + sb[j] > k) break;
  //   ans = max(ans, i + j);
  // }

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


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const ll INF = 1e18;

int main() {
  ll n, k, m, ans = 0, idxa = -1, idxb = -1;
  cin >> n >> m >> k;
  vector<ll> a(n), b(n), sa(n + 1, 0), sb(n + 1, 0);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  // ll dp[n + 1][m + 1]={};

  rep(i, n) {
    sa[i + 1] = sa[i] + a[i];
    if (sa[i + 1] > k) {
      idxa = i + 1;
      break;
    }
  }
  if (idxa == -1)
    idxa = n + 1;

  rep(i, m) {
    sb[i + 1] = sb[i] + b[i];
    if (sb[i + 1] > k) {
      idxb = i + 1;
      break;
    }
  }

  if (idxb == -1)
    idxb = m + 1;

  rep(i, idxa) rep(j, idxb) {
    if (sa[i] + sb[j] > k)
      break;
    ans = max(ans, i + j);
  }

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
  }
}
template <class T1, class T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
  }
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;

inline int max_B(const vector<uint64_t> &B, const uint64_t key, const int M) {
  int left = -1;
  int right = M + 1;
  while (right - left > 1) {
    int mid = (left + right) / 2;
    if (B[mid] <= key) {
      left = mid;
    } else {
      right = mid;
    }
  }
  return left;
}
int main() {
  int N, M;
  uint64_t K;
  cin >> N >> M >> K;
  vector<uint64_t> A(N + 1), B(M + 1);
  A[0] = 0;
  B[0] = 0;
  for (int i = 1; i < N + 1; ++i) {
    uint64_t num;
    cin >> num;
    A[i] = A[i - 1] + num;
  }
  for (int i = 1; i < M + 1; ++i) {
    uint64_t num;
    cin >> num;
    B[i] = B[i - 1] + num;
  }

  ll ans = 0;

  rep(i, N + 1) {
    if (K < A[i]) {
      break;
    }

    int b = max_B(B, K - A[i], M);
    if (b != -1) {
      chmax(ans, i + b);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
  }
}
template <class T1, class T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
  }
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;

int main() {
  uint64_t N, M, K;
  cin >> N >> M >> K;
  vector<uint64_t> A(N + 1), B(N + 1);
  A[0] = 0;
  B[0] = 0;
  for (uint64_t i = 1; i < N + 1; ++i) {
    uint64_t num;
    cin >> num;
    A[i] = A[i - 1] + num;
  }
  for (uint64_t i = 1; i < M + 1; ++i) {
    uint64_t num;
    cin >> num;
    B[i] = B[i - 1] + num;
  }

  ll ans = 0;

  rep(i, N + 1) {
    rep(j, M + 1) {
      if (A[i] + B[j] <= K) {
        chmax(ans, i + j);
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *    author:  yuya1234
 *    created: 30.06.2020 10:57:25
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

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

  int n, m, k;
  cin >> n >> m >> k;

  ll a[200001], b[200001];
  ll aMax = 1, bMax = 1, tmp, sum = 0;
  a[0] = b[0] = 0;
  FOR(i, 1, n) {
    cin >> tmp;
    sum += tmp;
    a[i] = sum;
    if (sum <= k) {
      aMax = i + 1;
    }
  }

  sum = 0;
  FOR(i, 1, m) {
    cin >> tmp;
    sum += tmp;
    b[i] = sum;
    if (sum <= k) {
      bMax = i + 1;
    }
  }

  ll ans = 0;

  REP(i, aMax) {
    REPD(j, bMax) {
      if (k >= a[i] + b[j]) {
        ans = max(ans, i + j);
        bMax = j + 1;
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

/**
 *    author:  yuya1234
 *    created: 30.06.2020 10:57:25
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

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

  int n, m, k;
  cin >> n >> m >> k;

  ll a[200001], b[200001];
  ll aMax = 1, bMax = 1, tmp, sum = 0;
  a[0] = b[0] = 0;
  FOR(i, 1, n) {
    cin >> tmp;
    sum += tmp;
    a[i] = sum;
    if (sum <= k) {
      aMax = i + 1;
    }
  }

  sum = 0;
  FOR(i, 1, m) {
    cin >> tmp;
    sum += tmp;
    b[i] = sum;
    if (sum <= k) {
      bMax = i + 1;
    }
  }

  ll ans = 0;

  REP(i, aMax) {
    REPD(j, bMax) {
      if (k >= a[i] + b[j]) {
        ans = max(ans, i + j);
        break;
      }
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  ll a[200005], b[200005], sum[200005], sa[200005], sb[200005];
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  sa[0] = 0;
  sb[0];
  rep(i, n) sa[i + 1] = sa[i] + a[i];
  rep(i, m) sb[i + 1] = sb[i] + b[i];

  ll ans = 0;
  rep(ba, n + 1) {
    ll time = sa[ba];
    ll book = ba;
    if (time > k)
      continue;
    int l = 0, r = m + 1;
    while (r - l > 1) {
      int c = (l + r) / 2;
      if (time + sb[c] > k) {
        r = c;
      } else {
        l = c;
      }
    }
    book += l;
    ans = max(ans, book);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

  ll ans = 0;
  rep(num_a, n + 1) {
    ll time = 0;
    ll book = 0;
    rep(i, num_a) {
      if (time + a[i] > k)
        break;
      time += a[i];
      book++;
    }
    rep(i, m) {
      if (time + b[i] > k)
        break;
      time += b[i];
      book++;
    }
    ans = max(ans, book);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000007
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n, m;
  long long int k;
  cin >> n >> m >> k;

  vector<long long int> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  long long int x;
  rep(i, 0, n) {
    cin >> x;
    a[i + 1] = a[i] + x;
  }
  rep(i, 0, m) {
    cin >> x;
    b[i + 1] = b[i] + x;
  }
  int ans = 0;
  rep(i, 0, n + 1) {
    for (int j = m; j >= 0; j--) {
      if (a[i] + b[j] <= k) {
        ans = max(ans, i + j);
        m = j;
        break;
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000007
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n, m;
  long long int k;
  cin >> n >> m >> k;

  vector<long long int> nt(n + 1);
  vector<long long int> mt(m + 1);
  vector<long long int> ntotal(n + 1);
  vector<long long int> mtotal(m + 1);
  ntotal[0] = 0;
  mtotal[0] = 0;
  rep(i, 1, n + 1) {
    cin >> nt[i];
    ntotal[i] = nt[i] + ntotal[i - 1];
  }
  rep(i, 1, m + 1) {
    cin >> mt[i];
    mtotal[i] = mt[i] + mtotal[i - 1];
  }
  int ans = 0;
  for (int j = m; j >= 0; j--) {
    int x = 0;
    rep(i, 0, n + 1) {
      if (mtotal[j] + ntotal[i] > k)
        break;
      x = j + i;
    }
    ans = max(ans, x);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

int main() {
  i64 N, M, K;
  cin >> N >> M >> K;
  vector<i64> A(N), B(M);
  for (i64 i = 0; i < N; i++)
    cin >> A[i];
  for (i64 i = 0; i < M; i++)
    cin >> B[i];
  for (i64 i = 0; i < N - 1; i++)
    A[i + 1] += A[i];
  for (i64 i = 0; i < M - 1; i++)
    B[i + 1] += B[i];
  i64 ans = 0;
  i64 l = M;
  for (i64 i = 0; i <= N; i++)
    for (i64 j = l; 0 <= j; j--) {
      i64 t = 0;
      if (i != 0)
        t += A[i - 1];
      if (j != 0)
        t += B[j - 1];
      if (t <= K) {
        ans = max(ans, i + j);
        break;
      } else
        l = j;
    }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

int main() {
  i64 N, M, K;
  cin >> N >> M >> K;
  vector<i64> A(N), B(M);
  for (i64 i = 0; i < N; i++)
    cin >> A[i];
  for (i64 i = 0; i < M; i++)
    cin >> B[i];
  for (i64 i = 0; i < N - 1; i++)
    A[i + 1] += A[i];
  for (i64 i = 0; i < M - 1; i++)
    B[i + 1] += B[i];
  i64 ans = 0;
  i64 l = M;
  for (i64 i = 0; i <= N; i++)
    for (i64 j = l; 0 <= j; j--) {
      i64 t = 0;
      if (i != 0)
        t += A[i - 1];
      if (j != 0)
        t += B[j - 1];
      if (t <= K)
        ans = max(ans, i + j);
      else
        l = j;
    }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define endl '\\n'

int main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m, k;

  cin >> n >> m >> k;

  vector<long long> prefA(n + 1, 0);
  vector<long long> prefB(m + 1, 0);

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    prefA[i + 1] = prefA[i] + x;
  }

  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    prefB[i + 1] = prefB[i] + x;
  }

  int ans = 0;

  for (int i = 0; i <= n; i++) {
    if (k < prefA[i])
      continue;

    long long rest = k - prefA[i];
    int j = upper_bound(prefB.begin(), prefB.end(), rest) - prefB.begin() - 1;

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

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl '\\n'

vector<long long> a;
vector<long long> b;
int ans;
map<pair<int, int>, bool> vis;

void solve(int i, int j, int k) {
  if (vis.count({i, j}))
    return;
  vis[{i, j}] = true;
  if (a[i] <= k)
    solve(i + 1, j, k - a[i]);
  if (b[j] <= k)
    solve(i, j + 1, k - b[j]);

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

int main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m, k;

  cin >> n >> m >> k;

  a.resize(n + 1);
  b.resize(m + 1);

  a[n] = b[m] = 1e9 + 7;

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

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

  solve(0, 0, k);

  cout << ans << endl;

  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> A(N + 1);
  vector<int64_t> B(M + 1);

  A[0] = 0, B[0] = 0;
  for (int i = 1; i < N + 1; i++) {
    int64_t a;
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i < M + 1; i++) {
    int64_t b;
    cin >> b;
    B[i] = B[i - 1] + b;
  }

  int j = M;
  int read_max = 0;
  for (int i = 0; i < N + 1; i++) {
    if (A[i] > K)
      break;
    while (B[j] > K - A[i]) {
      j--;
    }
    // cout<<i<<' '<<j<<endl;
    if (read_max < (i + j)) {
      read_max = i + j;
    }
  }
  cout << read_max << endl;
}

Error: TLE

Fixed submission:

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

const int NUM = 200000;
const int64_t INF = 1e9;
int64_t N, M, K;
vector<int64_t> A(NUM, 0);
vector<int64_t> B(NUM, 0);
int64_t max_read = 0;

void dfs(int64_t ai, int64_t bi, int64_t res) {
  int64_t read = ai + bi;
  if (read > max_read) {
    max_read = read;
  }

  // printf("%d %d %d\\n", ai, bi, res);
  if (ai == N && bi == M)
    return;

  if (ai < N) {
    if (res >= A.at(ai)) {
      dfs(ai + 1, bi, res - A.at(ai));
    } else {
      return;
    }
  }

  if (bi < M) {
    if (res >= B.at(bi)) {
      dfs(ai, bi + 1, res - B.at(bi));
    } else {
      return;
    }
  }
  return;
}

int main() {
  cin >> N >> M >> K;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }
  dfs(0, 0, K);

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

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define _for(i, a, b) for (int i = (a); i < (b); i++)
#define endl '\\n'
using namespace std;
const int mod = 1e9 + 7;
const int MAX = 1e6 + 7;
unsigned ll a[MAX], b[MAX], x[MAX], y[MAX];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m, k, cnt;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  for (int i = 1; i < m; i++)
    b[i] += b[i - 1];
  unsigned ll ans1 = 0, ans2 = 0, sum = 0;
  for (ll i = 0; i < n; i++) {
    sum = a[i];
    unsigned ll temp = k - sum;
    if (temp > k)
      break;
    unsigned ll pos = upper_bound(b, b + m, temp) - b;
    ans1 = max(pos + i + 1, ans1);
  }
  sum = 0;
  for (ll i = 0; i < m; i++) {
    sum = b[i];
    unsigned ll temp = k - sum;
    if (temp > k)
      break;
    unsigned ll pos = upper_bound(a, a + n, temp) - a;
    ans2 = max(pos + i + 1, ans2);
  }
  cout << max(ans1, ans2);
  return 0;
}


Error: WA

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define _for(i, a, b) for (int i = (a); i < (b); i++)
#define endl '\\n'
using namespace std;
const int mod = 1e9 + 7;
const int MAX = 1e6 + 7;
ll a[MAX], b[MAX], x[MAX], y[MAX];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m, k, cnt;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> cnt;
    a[i] = a[i - 1] + cnt;
  }
  for (int i = 1; i <= m; i++) {
    cin >> cnt;
    b[i] = b[i - 1] + cnt;
  }
  if (a[n] + b[m] <= k)
    cout << n + m;
  else if (a[1] > k && b[1] > k)
    cout << 0;
  else {
    ll ans = 0, la = 1, lb = 1, sum = 0;
    int fa = 0, fb = 0;
    while (sum <= k) {
      if (a[la] * lb <= b[lb] * la) {
        if (a[la] + b[lb] <= k) {
          sum = a[la] + b[lb];
          fa = 1;
          la++;
        } else
          break;
      } else {
        if (a[la] + b[lb] <= k) {
          sum = a[la] + b[lb];
          fb = 1;
          lb++;
        } else
          break;
      }
    }
    cout << la + lb - fa - fb;
  }

  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++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M), SA(N + 1), SB(M + 1);
  rep(i, N) { cin >> A[i]; }
  rep(i, N) { SA[i + 1] = SA[i] + A[i]; }
  rep(i, M) { cin >> B[i]; }
  rep(i, M) { SB[i + 1] = SB[i] + B[i]; }
  ll ans = 0;
  rep(i, N + 1) {
    ll sum = SA[i];
    if (sum > K)
      continue;
    ll cap = K - sum;
    ll id = upper_bound(SB.begin(), SB.end(), cap) - SB.begin();
    id--;
    cmax(ans, i + id);
  }
  cout << ans << endl;
  /*
  ll sum = 0;
  ll ind1 = 0;
  while(ind1 < N && sum + A[ind1] <= K){
      sum += A[ind1];
      ind1++;
  }
  ll ind2 = 0;
  while(ind2 < M && sum + B[ind2] <= K) {
      sum += B[ind2];
      ind2++;
  }
  ll ans = ind1 + ind2;
  for(ll i = ind1-1;i>=0;i--){
      sum -= A[i];
      while(ind2 < M && sum + B[ind2] <= K){
          sum += B[ind2];
          ind2++;
      }
      ind1--;
      //cout << ans << " " << sum << endl;
      cmax(ans, ll(ind1+ind2));
  }
  cout << ans << endl;*/
}

Error: 0

Fixed submission:


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

const int Maxn = 2e5 + 5;
int a[Maxn], b[Maxn];
int n, m, t;

int main() {
  cin >> n >> m >> t;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= m; ++i)
    cin >> b[i];

  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);

  int j = 1, k = 1, sum = 0;
  for (int i = 1; i <= n + m; ++i) {
    if (a[j] < b[k]) {
      sum += a[j];
      if (sum > t) {
        cout << i - 1 << endl;
        return 0;
      }
      j++;
    } else {
      sum += b[k];
      if (sum > t) {
        cout << i - 1 << endl;
        return 0;
      }
      k++;
    }
  }

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

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

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[i];
  for (int i = 0; i < M; ++i)
    cin >> B[i];
  ll sumA = 0;
  int ia = 0;
  ll sumB = 0;
  int ib = 0;
  while (ia < N && sumA + A[ia] <= K)
    sumA += A[ia++];
  int ans = ia;
  for (int i = ia; i >= 0; --i) {
    ll rt = K - sumA;
    while (ib < M && sumB + B[ib] <= rt)
      sumB += B[ib++];
    ans = max(ans, i + ib);
    if (i > 0)
      sumA -= A[i - 1];
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

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[i];
  for (int i = 0; i < M; ++i)
    cin >> B[i];
  vector<ll> sumA(N + 1, 0), sumB(M + 1, 0);
  for (int i = 0; i < N; ++i)
    sumA[i + 1] = sumA[i] + A[i];
  for (int i = 0; i < M; ++i)
    sumB[i + 1] = sumB[i] + A[i];
  int pa = upper_bound(sumA.begin(), sumA.end(), K) - sumA.begin() - 1;
  int ans = pa;
  for (int i = pa; i >= 0; --i) {
    ll rest_t = K - sumA[i];
    int pb = upper_bound(sumB.begin(), sumB.end(), rest_t) - sumB.begin() - 1;
    pb = max(pb, 0);
    ans = max(ans, i + pb);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using llint = long long int;
using u64 = std::uint_fast64_t;

int read_from_B(const std::vector<llint> cumb, llint k) {
  auto pt = std::upper_bound(cumb.begin(), cumb.end(), k);
  return pt - cumb.begin();
}

void solve(void) {

  llint n, m, k;
  std::cin >> n >> m >> k;

  std::vector<llint> A(n), B(m);
  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
  }
  for (int j = 0; j < m; j++) {
    std::cin >> B[j];
  }

  std::vector<llint> cumB(m);
  for (int i = 0; i < m; i++) {
    if (i == 0)
      cumB[i] = B[i];
    else
      cumB[i] = cumB[i - 1] + B[i];
  }

  int j = read_from_B(cumB, k);
  int best = 0 + read_from_B(cumB, k);
  for (int i = 0; i < n; i++) {
    k -= A[i];
    if (k >= 0) {
      while (j > 0) {
        if (cumB[j - 1] > k)
          j--;
        else
          break;
      }
      // std::cout << i << " " << j << std::endl;
      best = std::max(i + 1 + j, best);
    } else
      break;
  }

  std::cout << best << std::endl;
}

int main(void) {

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using llint = long long int;
using u64 = std::uint_fast64_t;

int read_from_B(const std::vector<llint> cumb, llint k) {
  auto pt = std::upper_bound(cumb.begin(), cumb.end(), k);
  return pt - cumb.begin();
}

void solve(void) {

  llint n, m, k;
  std::cin >> n >> m >> k;

  std::vector<llint> A(n), B(m);
  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
  }
  for (int j = 0; j < m; j++) {
    std::cin >> B[j];
  }

  std::vector<llint> cumB(m);
  for (int i = 0; i < m; i++) {
    if (i == 0)
      cumB[i] = B[i];
    else
      cumB[i] = cumB[i - 1] + B[i];
  }

  llint best = read_from_B(cumB, k);
  for (llint i = 0; i < n; i++) {
    k -= A[i];
    if (k >= 0)
      best = std::max(best, i + 1 + read_from_B(cumB, k));
    else
      break;
  }

  std::cout << best << std::endl;
}

int main(void) {

  solve();

  return 0;
}