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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int N = 3e5 + 1;
int n, m, k;
long long int A[N], B[N], cs[N], sum;
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> B[i];
    cs[i + 1] = cs[i] + B[i];
  }
  int answer = upper_bound(cs, cs + m + 1, k) - cs - 1;
  for (int i = 0; i < n; i++) {
    sum += A[i];
    if (sum > k)
      break;
    int value = upper_bound(cs, cs + m + 1, k - sum) - cs - 1;
    answer = max(answer, (i + 1) + value);
  }
  cout << answer << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int main() {
  int n;
  cin >> n;
  ll k;
  cin >> k;
  vector<int> a(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i + 1];

  if (k > n) {
    vector<int> visit(n + 1, -1);
    int cur = 1;
    visit[1] = 0;
    int cycle = n;
    for (int i = 1; i <= n + 1; i++) {
      cur = a[cur];
      if (visit[cur] != -1) {
        cycle = i - visit[cur];
        break;
      }
      visit[cur] = i;
    }
    k = (k - n) % cycle + n;
  }
  int cur = 1;
  while (k--) {
    cur = a[cur];
  }
  cout << cur;

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

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

#define all(x) (x).begin(), (x).end()
#define inf (sizeof(int) == 4 ? (int)1E9 : (int)1E18)

template <class T> istream &operator>>(istream &I, vector<T> &v) {
  for (T &e : v)
    I >> e;
  return I;
}
template <class T> ostream &operator<<(ostream &O, vector<T> &v) {
  for (T &e : v)
    O << e << ' ';
  return O;
}

void _main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  cin >> a >> b;
  int ans = 0;
  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] += b[i - 1];

  for (int i = -1; i < n; i++) {
    int take = i == -1 ? 0 : a[i];
    if (take > k)
      break;
    int l = k - take;
    int j = upper_bound(all(b), l) - b.begin();
    ans = max(ans, i + j + 1);
  }

  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int _t = 1;
  //        cin >> _t;
  while (_t--)
    _main();
  return 0;
}


Error: WA

Fixed submission:

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

#define all(x) (x).begin(), (x).end()
#define inf (sizeof(int) == 4 ? (int)1E9 : (int)1E18)

template <class T> istream &operator>>(istream &I, vector<T> &v) {
  for (T &e : v)
    I >> e;
  return I;
}
template <class T> ostream &operator<<(ostream &O, vector<T> &v) {
  for (T &e : v)
    O << e << ' ';
  return O;
}

void _main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  cin >> a >> b;
  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i < n; i++)
    b[i] += b[i - 1];

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > k)
      break;
    int l = k - a[i];
    int j = upper_bound(all(b), l) - b.begin();
    ans = max(ans, i + j + 1);
  }

  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int _t = 1;
  //        cin >> _t;
  while (_t--)
    _main();
  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 REP(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
using namespace std;

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

Error: 0

Fixed submission:

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  queue<ll> a;
  queue<ll> b;
  rep(i, n) {
    int c;
    cin >> c;
    a.push(c);
  }
  rep(i, m) {
    int c;
    cin >> c;
    b.push(c);
  }
  ll count = 0;
  ll ans = 0;
  while (1) {
    if (count > k || ans > n + m)
      break;
    if (a.front() <= b.front() || b.empty() == 1) {
      count += a.front();
      a.pop();
      ans++;
    } else if (b.front() < a.front() || a.empty() == 1) {
      count += b.front();
      b.pop();
      ans++;
    }
  }
  cout << ans - 1 << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main(int argc, char *argv[]) {
  int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<long> a(n + 1), b(m + 1);
  a.at(0) = 0;
  b.at(0) = 0;
  for (int i = 1; i <= n; ++i) {
    std::cin >> a.at(i);
    a.at(i) += a.at(i - 1);
  }
  for (int i = 1; i <= m; ++i) {
    std::cin >> b.at(i);
    b.at(i) += b.at(i - 1);
  }

  int ans = 0;
  int j = m;
  for (int i = 0; i <= n; ++i) {
    if (k < a.at(i))
      break;
    while (b.at(j) > k - a.at(i))
      --j;
    ans = std::max(ans, i + j);
  }

  std::cout << ans << std::endl;
}


Error: 0

Fixed submission:

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

int main(int argc, char *argv[]) {
  int n, m, k;
  std::vector<int> a, b;
  a.push_back(0);
  b.push_back(0);
  std::cin >> n >> m >> k;
  for (int i = 0; i < n; ++i) {
    int ai;
    std::cin >> ai;
    a.push_back(ai + a.back());
  }
  for (int i = 0; i < m; ++i) {
    int bi;
    std::cin >> bi;
    b.push_back(bi + b.back());
  }

  int ans = 0;
  int j = m;
  for (int i = 0; i <= n; ++i) {
    if (k < a.at(i))
      continue;
    while (b.at(j) > k - a.at(i))
      --j;
    ans = std::max(ans, i + j);
  }

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

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

#pragma warning(disable : 4996)

#define si(n) scanf("%d", &n)
#define sll(n) scanf("%lld", &n)
#define ss(s) scanf("%s", &s)
#define pi(n) printf("%d\\n", n)
#define pll(n) printf("%lld\\n", n)
#define ps(s) printf("%s\\n", s)
#define FOR(i, a, b) for (int i = a; i <= b; i++)

typedef long long ll;

void solve() {
  // input
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  for (auto &i : a)
    sll(i);
  vector<ll> b(m);
  for (auto &i : b)
    sll(i);

  // pre_sum O(n)
  vector<ll> a_sum(n + 1);
  a_sum[0] = 0;
  a_sum[1] = a[0];
  FOR(i, 2, n) { a_sum[i] = (ll)a_sum[i - 1] + a[i - 1]; }
  vector<ll> b_sum(m + 1);
  b_sum[0] = 0;
  b_sum[1] = b[0];
  FOR(i, 2, m) { b_sum[i] = (ll)b_sum[i - 1] + b[i - 1]; }
  // dp O(nlogm)
  int max_book = 0;
  FOR(i, 0, n) {
    if (a_sum[i] > k) {
      break;
    }
    // two pointers
    int l = 0, r = m, mid;
    while (l < r) {
      mid = (l + r) / 2;
      ll sum1 = b_sum[mid] + a_sum[i];
      ll sum2 = b_sum[mid + 1] + a_sum[i];
      if (sum1 <= k && sum2 > k) {
        break;
      } else if (sum1 > k) {
        r = mid - 1;
      } else if (sum1 < k) {
        l = mid + 1;
      }
    }
    mid = (l + r) / 2;
    max_book = max(max_book, i + mid);
  }
  cout << max_book << endl;
}

int main() { solve(); }

Error: 0

Fixed submission:

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

#pragma warning(disable : 4996)

#define si(n) scanf("%d", &n)
#define sll(n) scanf("%lld", &n)
#define ss(s) scanf("%s", &s)
#define pi(n) printf("%d\\n", n)
#define pll(n) printf("%lld\\n", n)
#define ps(s) printf("%s\\n", s)
#define FOR(i, a, b) for (int i = a; i <= b; i++)

typedef long long ll;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n);
  for (auto &i : a)
    si(i);
  vector<int> b(m);
  for (auto &i : b)
    si(i);

  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1));
  int max_book = 0;
  FOR(i, 0, n) {
    FOR(j, 0, m) {
      if (i == 0 && j == 0) {
        dp[i][j] = 0;
      } else if (j == 0) {
        dp[i][j] = dp[i - 1][j] + a[i - 1];
      } else {
        dp[i][j] = dp[i][j - 1] + b[j - 1];
      }
      if (dp[i][j] > k) {
        break;
      } else {
        max_book = max(max_book, i + j);
      }
    }
  }
  cout << max_book << endl;
}

int main() { solve(); }
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define DBG(x)                                                                 \\
  (void)(cout << "L" << __LINE__ << ": " << #x << " = " << (x) << '\\n')
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
typedef long long ll;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const long double PI = (acos(-1));
const long double EPS = 0.0000000001;
const int INF = 0x3f3f3f3f;
const int maxn = 1100000;
ll a[maxn];
ll b[maxn];
void run_case() {
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i + 1];
    b[i + 1] += b[i];
  }
  int ans = 0;
  // for (int i = 1; i <= m; i++)
  // {
  //     DBG(b[i]);
  // }
  // DBG(b[m]);
  for (int i = 0, j = m; i <= n; i++) {
    //  DBG(ans);
    if (a[i] > k)
      break;
    while (a[i] + b[j] > k) {
      j--;
      // DBG(j);
    }
    ans = max(i + j, ans);
    // DBG(ans);
  }
  cout << ans << "\\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setiosflags(ios::fixed) << setprecision(12);
  run_case();
  cout.flush();
  return 0;
}

Error: TLE

Fixed submission:

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

int main()

{
  int a[400005], b, c = 0, i, j, k, m, n, p, u = 0;
  cin >> n >> m >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = n; i < n + m; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n + m; i++) {
    for (j = 0; j < n + m - 1; j++) {
      if (a[j] > a[j + 1]) {
        b = a[j];
        a[j] = a[j + 1];
        a[j + 1] = b;
      }
    }
  }
  for (i = 0; i < n + m; i++) {

    if (c <= k) {
      c = c + a[i];
      u++;
      if (c > k) {
        c = c - a[i];
        u--;
        break;
      }
    }
  }
  cout << u << endl;

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

#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long int deskA, deskB, totalTime;
  cin >> deskA >> deskB >> totalTime;
  vector<long long int> timeA(deskA);
  vector<long long int> timeB(deskB);
  vector<long long int> prefixSumA(deskA + 1);
  vector<long long int> prefixSumB(deskB + 1);
  prefixSumA.push_back(0);
  prefixSumB.push_back(0);
  for (long long int i = 0; i < deskA; i++) {
    cin >> timeA[i];
    prefixSumA[i + 1] = prefixSumA[i] + timeA[i];
  }
  for (long long int j = 0; j < deskB; j++) {
    cin >> timeB[j];
    prefixSumB[j + 1] = prefixSumB[j] + timeB[j];
  }
  long long int maxBooks;
  ;
  vector<long long int> result;
  long long int m = timeB.size();
  for (long long int i = 0; i < timeA.size() + 1; i++) {
    long long int remainingTime = totalTime - prefixSumA[i];
    for (long long int j = m; j >= 0; j--) {
      if (prefixSumA[i] + prefixSumB[j] <= totalTime) {
        m = j;
        result.push_back(i + j);
        break;
      }
    }
  }
  maxBooks = *max_element(result.begin(), result.end());
  cout << maxBooks;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int max_book(vector<long long int> vec1, vector<long long int> vec2, int max,
             long long int limit, int n, bool &flag) {
  int counter = 0;
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    if (sum + vec1[i] <= limit) {
      sum += vec1[i];
      counter++;
    } else {
      flag = false;
      break;
    }
  }
  for (int i = 0; i < vec2.size(); i++) {
    if (sum + vec2[i] <= limit) {
      sum += vec2[i];
      counter++;
    } else {
      break;
    }
  }
  return (counter > max) ? (counter) : (max);
}

int main() {
  int n, m, counter = 0, x, y, max = 0;
  long long int k, sum = 0;
  cin >> n >> m >> k;
  vector<long long int> A(n);
  vector<long long int> B(m);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  for (int i = 0; i < m; i++)
    cin >> B[i];
  bool flag = true;
  for (int i = 1; i <= A.size() && flag; i++)
    max = max_book(A, B, max, k, i, flag);
  flag = true;
  for (int i = 1; i <= B.size() && flag; i++)
    max = max_book(B, A, max, k, i, flag);
  cout << max << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m, k;
  cin >> n >> m >> k;
  long long a[n + 1], b[m + 1], mx = 0;
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  long long i = 0;
  for (int j = n; j >= 0; j--) {
    long long x = k - a[j];
    if (x < 0)
      continue;
    for (; i <= m; i++) {
      if (b[i] > x) {
        i--;
        break;
      }
    }
    if (i > m)
      i--;
    mx = max(mx, j + i);
  }
  cout << mx << '\\n';
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long int ll;

using namespace std;

long long int fun(ll i, ll j, ll sum, ll limit, vector<ll> a, vector<ll> b) {
  ll books1 = 0, books2 = 0;
  ll sum1 = sum + a[i];
  if (i < a.size()) {
    i++;
    if (sum1 <= limit)
      books1 = fun(i, j, sum1, limit, a, b);
    // cout << "Books 1 " << books1 << endl;
  }
  ll sum2 = sum + b[j];
  if (j < b.size()) {
    j++;
    if (sum2 <= limit)
      books2 = fun(i, j, sum2, limit, a, b);
    // cout << "Books 2 " << books2 << endl;
  }
  return (max(books1, books2) + 1);
}

int main() {
  long long int n, m, counter = 0, x, y;
  long long int k, sum = 0;
  cin >> n >> m >> k;
  vector<long long int> a(n);
  vector<long long int> b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  cout << fun(0, 0, 0, k, a, b) - 1 << endl;

  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, x;
  vector<ll> v1, v2;
  cin >> n >> m >> k;
  for (ll i = 0; i < n; i++) {
    cin >> x;
    if (i == 0)
      v1.push_back(x);
    else {
      v1.push_back(v1[i - 1] + x);
    }
  }
  ll ans = 0;
  for (ll i = 0; i < m; i++) {
    cin >> x;
    if (i == 0)
      v2.push_back(x);
    else {
      v2.push_back(v2[i - 1] + x);
    }
    x = upper_bound(v1.begin(), v1.end(), k - v2[i]) - v1.begin() - 1;
    if (v2[i] <= k) {
      ans = max(ans, x + 1 + i + 1);
    } else
      break;
  }
  for (ll i = 0; i < n; i++) {
    x = upper_bound(v2.begin(), v2.end(), k - v1[i]) - v2.begin() - 1;
    if (v1[i] <= k) {
      ans = max(ans, x + 1 + i + 1);
    } else
      break;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int y, x, m, sum = 1;
  cin >> x >> y >> m;
  stack<int> st;
  stack<int> st1;
  for (int i = 0; i < x; i++) {
    int a;
    cin >> a;
    st.push(a);
  }
  for (int i = 0; i < x; i++) {
    int a;
    cin >> a;
    st1.push(a);
  }
  for (int i = 0; i <= x + y; i++) {

    if (st.top() > st1.top() && m - st.top() > 0) {
      m -= st.top();
      st.pop();
      sum++;
    } else if (st.top() < st1.top() && m - st1.top() > 0) {
      m -= st1.top();
      st1.pop();
      sum++;
    }
  }
  cout << sum;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

// problem C
// from https://atcoder.jp/contests/abc172/tasks/abc172_c
#include <algorithm>
#include <iostream>

using namespace std;

const int MAXN = 200 * 1000 + 10;
int a[MAXN], b[MAXN];
long sa[MAXN], sb[MAXN];

int main() {
  ios::sync_with_stdio(false);

  int n, m, k;
  cin >> n >> m >> k;
  fill(sa, sa + n, 0L);
  fill(sb, sb + m, 0L);

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

  int max_books = 0;

  int last_optimal_y = 0;
  for (int x = n; x >= 0; x--) {
    for (int y = last_optimal_y; y <= m; y++) {
      if (sa[x] + sb[y] <= k) {
        max_books = max(max_books, x + y);
        last_optimal_y = y;
      } else {
        break;
      }
    }
  }
  cout << max_books << endl;
  return 0;
}


Error: TLE

Fixed submission:

// problem C
// from https://atcoder.jp/contests/abc172/tasks/abc172_c
#include <algorithm>
#include <iostream>

using namespace std;

const int MAXN = 200 * 1000 + 10;
int a[MAXN], b[MAXN];
long sa[MAXN], sb[MAXN];

int main() {
  ios::sync_with_stdio(false);

  int n, m, k;
  cin >> n >> m >> k;
  fill(sa, sa + n, 0L);
  fill(sb, sb + m, 0L);

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

  int max_books = 0;

  for (int x = n; x >= 0; x--) {
    int last_optimal_y = 0;
    for (int y = last_optimal_y; y <= m; y++) {
      if (sa[x] + sb[y] <= k) {
        max_books = max(max_books, x + y);
      }
    }
  }
  cout << max_books << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define endl '\\n'
#define mod 1000000007
#define inf 1000000000000000000
#define pii pair<int, pair<int, int>>
const double pi = acos(-1);
const int sz = 200002;
int n, m, q, k;
vector<int> a, b;
int main() {
  io cin >> n >> m >> k;
  a.resize(n);
  b.resize(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<ll> prefixa(n), prefixb(m);
  for (int i = 0; i < n; i++) {
    prefixa[i] = a[i] + (i == 0 ? 0 : prefixa[i - 1]);
  }
  for (int i = 0; i < m; i++) {
    prefixb[i] = b[i] + (i == 0 ? 0 : prefixb[i - 1]);
  }
  int ans = INT_MIN, curAns = 0;
  int pos = lower_bound(prefixb.begin(), prefixb.end(), k) - prefixb.begin();
  if (pos == m) {
    curAns += m;
  } else {
    if (prefixb[pos] > k) {
      pos--;
    }
    if (pos >= 0)
      curAns += pos + 1;
  }
  ans = max(ans, curAns);
  for (int i = 0; i < n; i++) {
    int kc = k;
    curAns = 0;
    if (prefixa[i] <= kc) {
      kc -= prefixa[i];
      curAns = i + 1;
    } else
      break;
    pos = lower_bound(prefixb.begin(), prefixb.end(), kc) - prefixb.begin();
    if (pos == m) {
      curAns += m;
    } else {
      if (prefixb[pos] > kc) {
        pos--;
      }
      if (pos >= 0)
        curAns += pos + 1;
    }
    ans = max(ans, curAns);
  }
  cout << (ans == INT_MIN ? 0 : ans) << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define endl '\\n'
#define mod 1000000007
#define inf 1000000000000000000
#define pii pair<int, pair<int, int>>
const double pi = acos(-1);
const int sz = 200002;
int n, m, q, k;
int main() {
  io 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];
  }
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push({a[0], {0, 0}});
  pq.push({b[0], {0, 1}});
  int ans = 0;
  while (k > 0) {
    int cur = pq.top().first;
    int idx = pq.top().second.first;
    int which = pq.top().second.second;
    pq.pop();
    if (idx + 1 < n) {
      pii ax = {a[idx + 1], {idx + 1, 0}};
      pii bx = {b[idx + 1], {idx + 1, 1}};
      pq.push((which == 0 ? ax : bx));
    }
    if (k < cur)
      break;
    k -= cur;
    ans++;
  }
  cout << ans;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long n, m, k, i, j, c, d, mx, s, x, y, a[200001], b[200001], a1[200001],
    b1[200001];
int main() {
  cin >> n >> m >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  for (i = 1; i <= m; i++)
    cin >> b[i];

  for (i = 1; i <= n; i++) {
    s += a[i];
    a1[i] = s;
  }
  s = 0;
  for (i = 1; i <= m; i++) {
    s += b[i];
    b1[i] = s;
  }

  a1[0] = 0;
  b1[0] = 0;
  for (i = 0; i <= n; i++) {
    if (k >= a1[i])
      x = k - a1[i];
    else
      break;
    d = 1;
    c = m;

    while (d <= m && c >= 0 && c > d) {

      j = (c + d) / 2;
      if (b1[j] == x) {
        c = j;
        break;
      }
      if (b1[j] > x)
        c = j - 1;
      else
        d = j + 1;
    }
    if (b1[c] > x)
      j = c - 1;
    else
      j = c;

    if (b1[1] > x)
      j = 0;
    if (i + j > mx)
      mx = i + j;
  }
  cout << mx;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long n, m, k, i, j, mx, s, a[200001], b[200001], a1[200001], b1[200001];
int main() {
  cin >> n >> m >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  for (i = 1; i <= m; i++)
    cin >> b[i];

  for (i = 1; i <= n; i++) {
    s = 0;
    for (j = 1; j <= i; j++)
      s += a[j];
    a1[i] = s;
  }
  for (i = 1; i <= m; i++) {
    s = 0;
    for (j = 1; j <= i; j++)
      s += b[j];
    b1[i] = s;
  }
  a1[0] = 0;
  b1[0] = 0;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++)
      if (k - a1[i] - b1[j] >= 0 && i + j > mx)
        mx = i + j;
  }
  cout << mx;
}