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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A1(N);
  vector<int> B1(M);
  vector<int> A;
  vector<int> B;
  int cnt1 = N;
  for (int i = 0; i < N; i++) {
    cin >> A1[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> B1[i];
  }
  A.push_back(0);
  B.push_back(0);
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt = cnt + A1[i];
    if (cnt > K) {
      cnt1 = i;
    }
    A.push_back(cnt);
  }
  cnt = 0;
  for (int i = 0; i < M; i++) {
    cnt = cnt + B1[i];
    B.push_back(cnt);
  }
  int ans = 0;
  for (int i = 0; i <= cnt1; i++) {
    auto it = upper_bound(B.begin(), B.end(), K - A[i]);
    if (it == B.end()) {
      ans = max(ans, i + M);
    }
    int X = it - B.begin() - 1;
    if (X >= 0) {
      ans = max(ans, i + X);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int A[200005] = {};
int B[200005] = {};
signed main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A1(N);
  vector<int> B1(M);
  int cnt1 = N;
  int cnt2 = M;
  for (int i = 0; i < N; i++) {
    cin >> A1[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> B1[i];
  }
  for (int i = 0; i < N; i++) {
    A[i + 1] = A[i] + A1[i];
    if (A[i + 1] > K) {
      cnt1 = i;
      break;
    }
  }
  for (int i = 0; i < M; i++) {
    B[i + 1] = B[i] + B1[i];
    if (B[i + 1] > K) {
      cnt2 = i;
      break;
    }
  }
  int ans = 0;
  for (int i = 0; i <= cnt1; i++) {
    for (int j = ans - i; j <= cnt2; j++) {
      if (A[i] + B[j] <= K) {
        ans = max(ans, i + j);
      }
    }
  }
  cout << ans << 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<ll> a(n + 1), b(m + 1);
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  for (int i = 1; i <= m; i++) {
    ll x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    ll temp = a[i];
    int index = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin();
    index--;
    if (index >= 0 && b[index] <= (ll)k - a[i])
      ans = max(ans, i + index);
    // cout << index << "\\n";
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  queue<ll> a, b;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.push(x);
  }
  for (int i = 0; i < m; i++) {
    ll x;
    cin >> x;
    b.push(x);
  }
  ll time = 0;
  int ans = 0;
  while (time < k) {
    if (a.empty() && b.empty())
      break;
    if (a.empty() || a.front() >= b.front()) {
      time += b.front();
      if (time > k) {
        time -= b.front();
        break;
      }
      b.pop();
      ans++;
    } else if (b.empty() || a.front() <= b.front()) {
      time += a.front();
      if (time > k) {
        time -= a.front();
        break;
      }
      a.pop();
      ans++;
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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


Error: TLE

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0), b(m + 1, 0);
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    a[i] = a[i - 1] + t;
  }
  for (int i = 1; i <= m; i++) {
    int t;
    cin >> t;
    b[i] = b[i - 1] + t;
  }
  int ans = 1;
  for (; 1; ans++) {
    bool nok = 1;
    for (int i = 0; i <= min(n, ans); i++) {
      if (ans - i > m)
        continue;
      if (a[i] + b[ans - i] <= k) {
        nok = 0;
        break;
      }
    }
    if (nok)
      break;
  }
  cout << ans - 1 << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>
#define ll long long

using namespace std;

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];
  }

  vector<ll> A;
  vector<ll> B;
  A.push_back(0);
  B.push_back(0);
  for (ll i = 1; i <= n; i++) {
    A.push_back(A[i - 1] + a[i - 1]);
  }

  for (ll i = 1; i <= m; i++) {
    B.push_back(B[i - 1] + b[i - 1]);
  }

  ll total = 0;
  ll j = m;
  for (ll i = 0; i <= n; i++) {
    if (A[i] > k) {
      break;
    }
    while (B[j] > k - A[i]) {
      j--;
    }
    total = max(total, i + j);
  }

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

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define ll long long

using namespace std;

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 total(0);
  ll count(0);
  ll i(0);
  ll j(0);
  while (total <= k) {
    if (i < n && j < m) {
      if (a[i] < b[j]) {
        total += a[i++];
      } else {
        total += b[j++];
      }
    } else if (i < n) {
      total += a[i++];
    } else if (j < m) {
      total += a[j++];
    } else {
      break;
    }
    count++;
  }
  count = total > k ? count - 1 : count;
  cout << count << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
#define ALL(l) (l).begin(), (l).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;

// const ll mod =  1000000007;
// const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll =
    pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------

int main() {
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;

  vll a(n), b(m), c(n + 1, k + 1);
  rep(i, n) cin >> a.at(i);
  rep(i, m) cin >> b.at(i);
  c.at(0) = 0;
  ll t = 0, book = 0;
  rep(i, n) {
    t += a.at(i);
    if (t > k)
      break;
    book++;
    c.at(book) = t;
  }

  ans = upper_bound(ALL(c), k) - c.begin() - 1;

  book = 0;
  t = 0;
  rep(i, m) {
    ll t2, book2;
    t += b.at(i);
    book++;
    if (t > k)
      break;
    t2 = k - t;
    book2 = upper_bound(ALL(c), t2) - c.begin() - 1;
    // cout<<book2<<endl;
    ans = max(ans, book + book2);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
#define ALL(l) (l).begin(), (l).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;

// const ll mod =  1000000007;
// const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll =
    pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------

int main() {
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;

  vll a(n), b(m), c(k + 1, 0);
  rep(i, n) cin >> a.at(i);
  rep(i, m) cin >> b.at(i);

  ll p = 0, t = 0;
  rep(i, n) {
    p += a.at(i);
    if (p > k)
      break;
    t++;
    c.at(p) = t;
  }
  t = 0;
  rep(i, k + 1) {
    if (c.at(i) != 0)
      t++;
    c.at(i) = t;
  }
  // rep(i,k+1)cout<<i<<" "<<c.at(i)<<endl;

  p = 0;
  rep(i, m) {
    p += b.at(i);
    if (p > k)
      break;
    ans = max(ans, i + 1 + c.at(k - p));
  }

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

#include <iostream>
#include <vector>
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);
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
  }
  for (ll i = 1; i <= M; i++) {
    cin >> B[i];
  }
  ll sum_now = 0;
  ll sum_rest = 0;
  ll max_cnt = 0;
  ll j;
  for (ll i = 0; i <= N; i++) {
    if (i == 0) {
      j = 1;
      while (sum_rest + B[j] <= K) {
        sum_rest += B[j];
        j++;
        if (j > M) {
          break;
        }
      }
      j--;
      max_cnt = max(max_cnt, j);
    } else {
      sum_now += A[i];
      if (sum_now > K) {
        break;
      }
      while (sum_rest + sum_now > K) {
        sum_rest -= B[j];
        j--;
      }
      max_cnt = max(i + j, max_cnt);
    }
  }
  cout << max_cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
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);
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
  }
  for (ll i = 1; i <= M; i++) {
    cin >> B[i];
  }
  ll sum_now = 0;
  ll max_cnt = 0;
  for (ll i = 0; i <= N; i++) {
    ll sum_rest = 0;
    ll cnt = 0;
    ll j = 1;
    if (i == 0) {
      sum_rest = K;
      while (sum_rest - B[j] >= 0) {
        sum_rest -= B[j];
        cnt++;
        j++;
        if (j > M) {
          break;
        }
      }
    } else {
      sum_now += A[i];
      if (sum_now > K) {
        break;
      }
      cnt = i;
      sum_rest = K - sum_now;
      while (sum_rest - B[j] >= 0) {
        sum_rest -= B[j];
        cnt++;
        j++;
        if (j > M) {
          break;
        }
      }
    }
    max_cnt = max(cnt, max_cnt);
  }
  cout << max_cnt << endl;
  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;
  vector<long long> a(N);
  vector<long long> b(M);
  for (long long i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < M; i++) {
    cin >> b[i];
  }

  long long num = 0;
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    if (num + a[i] > K) {
      break;
    }
    ans++;
    num += a[i];
  }
  long long anum = num;
  long long aans = ans;
  long long bans = 0;

  for (long long i = 0; i < M; i++) {
    if (num + b[i] > K) {
      break;
    }
    if (i == M - 1) {
      ans = ans + 1;
      cout << ans << endl;
      exit(0);
    }
    ans++;
    num += b[i];
  }
  anum = num;
  bans = ans - aans;

  for (long long i = aans - 1; i > -1; i--) {
    anum -= a[i];
    num = anum;
    for (long long j = bans; j < M; j++) {
      if (num + b[j] > K) {
        if (j + i >= ans) {
          ans = j + i;
        }
        anum = num;
        bans = j;
        break;
      }
      if (j == M - 1) {
        if (j + i + 1 > ans) {
          ans = j + i + 1;
        }
        cout << ans << endl;
        exit(0);
      }
      num += b[j];
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> a(N);
  vector<long long> b(M);
  for (long long i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < M; i++) {
    cin >> b[i];
  }

  long long num = 0;
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    if (num + a[i] > K) {
      break;
    }
    ans++;
    num += a[i];
  }
  long long anum = num;
  long long aans = ans;
  long long bans = 0;

  for (long long i = 0; i < M; i++) {
    if (num + b[i] > K) {
      break;
    }
    if (i == M - 1) {
      ans = ans + 1;
      cout << ans << endl;
      exit(0);
    }
    ans++;
    num += b[i];
  }
  anum = num;
  bans = ans - aans;

  for (long long i = aans - 1; i > -1; i--) {
    anum -= a[i];
    num = anum;
    for (long long j = bans; j < M; j++) {
      if (num + b[j] > K) {
        if (j + i >= ans) {
          ans = j + i;
          anum = num;
          bans = j;
        }
        break;
      }
      if (j == M - 1) {
        if (j + i + 1 > ans) {
          ans = j + i + 1;
        }
        cout << ans << endl;
        exit(0);
      }
      num += b[j];
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define ssort(n) sort((n).begin(), (n).end())
#define rsort(n) sort((n).begin(), (n).end(), greater<ll>())
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
typedef pair<P, int> COST;
#define repl(i, n) for (ll i = 0; i < (n); ++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf_char = vector<vector<char>>;
using Graf_int = vector<vector<int>>;
#define MAX 100000

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

  rep(i, n) { a[i + 1] += a[i]; }
  rep(i, m) { b[i + 1] += b[i]; }
  int ans = 0;
  int lim_b = 0;
  int p, q;
  p = 0;
  q = 0;
  ll tmp = 0;
  int i = 0;
  while (a[i] <= k && i <= n) {
    ++i;
  }
  ans = i - 1;
  tmp = a[ans];
  i = 0;
  while (b[i] <= k && i <= m) {
    ++i;
  }
  lim_b = i - 1;
  p = ans;
  rep(i, lim_b + 1) {
    if (a[p] + b[i] > k) {
      ans = max(p + i - 1, ans);
      while (a[p] + b[i] > k) {
        p--;
        if (p == 0) {
          break;
        }
      }
    }
    if (p == 0 && a[p] + b[i] > k) {
      break;
    }
    if (i == lim_b && a[p] + b[i] <= k) {
      ans = max(p + i, ans);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define ssort(n) sort((n).begin(), (n).end())
#define rsort(n) sort((n).begin(), (n).end(), greater<int>())
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
typedef pair<P, int> COST;
#define repl(i, n) for (ll i = 0; i < (n); ++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf = vector<vector<int>>;
#define MAX 10000000

vector<ll> a(200001), b(200001);

int bin_search(ll x, int left, int right) {
  int middle;
  while (left != right) {
    middle = (left + right) / 2;
    if (b[middle] == x) {
      return middle;
    } else {
      if (middle == b.size() - 1 && b[middle] < x) {
        return middle;
      } else if (middle == 0 && b[middle] > x) {
        return -200000;
      } else if (b[middle] < x) {
        left = middle + 1;
      } else {
        right = middle - 1;
      }
    }
  }
  if (left == right) {

    while (b[left] > x) {
      left--;
      if (left == 0) {
        return 0;
      }
    }
    return left;
  }
}

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

  rep(i, n) { cin >> a[i + 1]; }
  rep(i, m) cin >> b[i + 1];
  a.resize(n + 1);
  b.resize(m + 1);
  FOR(i, n, 1) { a[i + 1] += a[i]; }
  FOR(i, m, 1) { b[i + 1] += b[i]; }
  int max_book = 0;
  rep(i, n + 1) {
    if (a[i] <= k)
      max_book = max(max_book, i + bin_search(k - a[i], 0, b.size()));
    else {
      break;
    }
  }
  cout << max_book << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int N, M, K;

  cin >> N >> M >> K;

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

  for (int i = 0; i < N; ++i) {
    int tmp;
    cin >> tmp;
    A[i + 1] = min(K + 1, A[i] + tmp);
  }
  for (int i = 0; i < M; ++i) {
    int tmp;
    cin >> tmp;
    B[i + 1] = min(K + 1, B[i] + tmp);
  }

  int mn = 0;
  int cb = M;
  for (int i = 0; i <= N; ++i) {
    for (int j = cb; j >= 0; --j) {
      if (B[j] + A[i] <= K) {
        mn = max(mn, j + i);
        cb = j;
        break;
      }
    }
  }

  cout << mn << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  int N, M, K;

  cin >> N >> M >> K;

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

  int aa = N;
  int bb = M;
  for (int i = 0; i < N; ++i) {
    int tmp;
    cin >> tmp;
    A[i + 1] = min(K + 1, A[i] + tmp);
    if (A[i + 1] == K + 1)
      aa = min(aa, i);
  }
  for (int i = 0; i < M; ++i) {
    int tmp;
    cin >> tmp;
    B[i + 1] = min(K + 1, B[i] + tmp);
    if (B[i + 1] == K + 1)
      bb = min(bb, i);
  }

  int mn = 0;
  for (int i = bb; i >= 0; --i) {
    for (int j = aa; j >= 0; --j) {
      if (B[i] + A[j] <= K) {
        mn = max(mn, j + i);
        break;
      }
    }
  }

  cout << mn << endl;

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

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

int main(void) {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> 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<long long> sumA(N + 1, 0);
  vector<long long> sumB(M + 1, 0);
  sumA[0] = 0;
  sumB[0] = 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] + B[i];
  }
  long long ans = 0, j = M;
  for (int i = 0; i < N + 1; i++) {
    if (sumA[i] > K)
      break;
    while (sumB[j] > K - sumA[i]) {
      j -= 1;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main(void) {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> 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<long long> sumA(N + 1, 0);
  vector<long long> sumB(N + 1, 0);
  sumA[0] = 0;
  sumB[0] = 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] + B[i];
  }
  long long ans = 0, j = M;
  for (int i = 1; i <= N; i++) {
    if (sumA[i] > K)
      break;
    while (sumB[j] > K - sumA[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>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  int cnt = 0;
  int j = m;
  rep(i, n + 1) {
    if (a[i] > k)
      break;
    ll l = k - a[i];
    while (j > 0 && b[j] > l) {
      j--;
    }
    cnt = max(cnt, i + j);
  }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  ll l = k;
  rep(i, m) {
    l -= b[i];
    if (l < 0)
      break;
    cnt++;
  }

  l = k;
  rep(i, n) {
    l -= a[i];
    if (l < 0)
      break;
    int now = i + 1;
    ll time = l;
    rep(j, m) {
      time -= b[j];
      if (time < 0)
        break;
      now++;
    }
    if (cnt < now)
      cnt = now;
  }

  cout << cnt << endl;

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

#include <stdio.h>
#define max(x, y) ((x) < (y) ? (y) : (x))

int main() {
  int n, m, k;
  scanf("%d %d %d", &n, &m, &k);
  long a[200005] = {0}, b[200005] = {0}, now;
  for (int i = 1; i <= n; i++) {
    scanf("%ld", &now);
    a[i] = a[i - 1] + now;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%ld", &now);
    b[i] = b[i - 1] + now;
  }

  long ans = 0;
  int pre = m;
  for (int i = 0; i <= n; i++) {
    while (a[i] + b[pre] > k)
      pre--;
    ans = max(ans, i + pre);
  }

  printf("%ld\\n", ans);

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define max(a, b) a < b ? b : a
int main() {
  ll n, m, k, l;
  cin >> n >> m >> k;
  ll a[200006] = {0}, b[200005] = {0};
  for (int i = 1; i <= n; i++) {

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

    cin >> l;
    b[l] = b[l - 1] + l;
  }
  ll mx = 0;
  int x = m;
  for (int j = 0; j <= n; j++) {

    while (b[x] + a[j] > k)
      x--;
    mx = max(mx, j + x);
  }
  cout << mx;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long ll;
#define max(x, y) ((x < y) ? y : x)
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, m, k, l;
  cin >> n >> m >> k;
  ll a[200005] = {0}, b[200005] = {0};
  a[0] = 0, b[0] = 0;
  for (int i = 1; i <= n; i++) {

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

    cin >> l;
    b[i] = b[i - 1] + l;
  }
  ll j = 0, x = m, mx = 0;
  for (j = 0; j <= n; j++) {

    while (b[x] + a[j] > k)
      x--;
    mx = max(mx, j + x);
    // cout<<mx<<endl;
  }
  cout << mx << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define max(a, b) ((a < b) ? b : a)
int main() {
  int n, m, k, l;
  cin >> n >> m >> k;
  int a[200005] = {0}, b[200005] = {0};
  for (int i = 1; i <= n; i++) {

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

    cin >> l;
    b[i] = b[l - 1] + l;
  }
  ll mx = 0;
  int x = m;
  for (int j = 0; j <= n; j++) {

    while (b[x] + a[j] > k)
      x--;
    mx = max(mx, j + x);
  }
  cout << mx << endl;
}