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

#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(c) c.begin(), c.end()
#define rall(c) c.end(), c.begin()
#define INF (int)1e9
#define MOD 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pi;
typedef map<int, int> MPII;
typedef set<int> SETI;

const int mxN = 2e5;
int a[mxN];
int n, m, k, x;

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

  cin >> n >> m >> k;

  vll a(n);
  vll b(m);

  for (auto &x : a)
    cin >> x;
  for (auto &x : b)
    cin >> x;

  vll sA(n + 1);
  vll sB(m + 1);
  sA[0] = 0, sB[0] = 0;

  for (int i = 0; i < n; ++i)
    sA[i + 1] = sA[i] + a[i];
  for (int i = 0; i < m; ++i)
    sB[i + 1] = sB[i] + b[i];

  ll ans = 0, j = m;
  for (int i = 0; i < n + 1; ++i) {
    if (sA[i] > k)
      break;
    while (sB[j] > k - sA[i])
      j -= 1;
    ans = max(ans, i + j);
  }
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(c) c.begin(), c.end()
#define rall(c) c.end(), c.begin()
#define INF (int)1e9
#define MOD 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef map<int, int> MPII;
typedef set<int> SETI;

const int mxN = 2e5;
int a[mxN];
int n, m, k;

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

  cin >> n >> m >> k;

  vector<ll> a(n);
  vector<ll> b(m);

  for (auto &x : a)
    cin >> x;
  for (auto &x : b)
    cin >> x;

  int c = 0;
  while (k > 0) {
    if (a[0] < b[0] && !a.empty() && !b.empty()) {
      if (k - a[0] >= 0) {
        k -= a[0];
        a.erase(a.begin());
      } else {
        break;
      }
    } else {
      if (k - b[0] >= 0) {
        k -= b[0];
        b.erase(b.begin());
      } else {
        break;
      }
    }
    c += 1;
  }

  cout << c;

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

#include <bits/stdc++.h>

#define ll long long
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n];
  ll B[m];
  ll preA[n + 1];
  ll preB[m + 1];
  preA[0] = 0;
  preB[0] = 0;
  for (ll i = 0; i < n; ++i) {
    cin >> A[i];
    preA[i + 1] = preA[i] + A[i];
  }
  for (ll i = 0; i < m; ++i) {
    cin >> B[i];
    preB[i + 1] = preB[i] + B[i];
  }
  ll max = 0;
  ll index = m;
  for (ll i = 0; i <= n; ++i) {
    ll totalLeft = k - preA[i];
    if (totalLeft < 0) {
      break;
    }
    while (preB[index] > totalLeft) {
      index--;
    }
    if (index + i > max) {
      max = index + i;
    }
  }
  cout << max;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll A[n];
  ll B[n];
  ll preA[n + 1];
  ll preB[m + 1];
  preA[0] = 0;
  preB[0] = 0;
  for (ll i = 0; i < n; ++i) {
    cin >> A[i];
    preA[i + 1] = preA[i] + A[i];
  }
  for (ll i = 0; i < m; ++i) {
    cin >> B[i];
    preB[i + 1] = preB[i] + B[i];
  }
  ll max = 0;
  ll index = m;
  for (ll i = 0; i <= n; ++i) {
    ll totalLeft = k - preA[i];
    if (totalLeft < 0) {
      break;
    }
    while (preB[index] > totalLeft) {
      index--;
    }
    if (index + i > max) {
      max = index + i;
    }
  }
  cout << max;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define ll long long
#define pii pair<int, int>

void test_case() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < m; i++)
    cin >> b[i];
  ll currsum = 0, i;

  for (i = 0; i < n; i++) {
    if (currsum + a[i] <= k)
      currsum += a[i];
    else
      break;
  }
  ll j = 0, maxlen = i;

  while (i >= 1 || j < m) {
    if (j < m && currsum + b[j] <= k) {
      currsum += b[j];
      j++;
    } else if (i >= 1) {
      currsum -= a[i - 1];
      i--;
    } else
      break;
    maxlen = max(maxlen, i + j);
  }
  cout << maxlen;
}

int main() {
  fastio
  //    int t;
  //    cin>>t;
  //    while(t--)
  test_case();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define forn(i, n) for (int i = 0; i < (int)n; i++)
ll power(ll x, ll n);
bool isprime(ll n);
ll mod = 1e9 + 7;
ll dp[1001][1001];
ll solve(ll a[], ll b[], ll n, ll m, ll i, ll j, ll k) {

  if (i < 1001 && j < 1001 && dp[i][j] != -1) {
    return dp[i][j];
  }

  ll c1 = 0, c2 = 0;
  if (i < n && a[i] <= k) {
    c1 = 1 + solve(a, b, n, m, i + 1, j, k - a[i]);
  }
  if (j < m && b[j] <= k) {
    c2 = 1 + solve(a, b, n, m, i, j + 1, k - b[j]);
  }
  if (c1 == 0 && c2 == 0) {
    return 0;
  }
  if (i < 1001 && j < 1001)
    return dp[i][j] = max(c1, c2);
  else
    return max(c1, c2);
}

void test_case() {
  memset(dp, -1, sizeof(dp));
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < m; i++)
    cin >> b[i];
  cout << solve(a, b, n, m, 0, 0, k);
}

int main() {
  fastio
  //    int t;
  //    cin>>t;
  //    while(t--)
  test_case();
}
ll power(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1)
      ans *= x;
    n >>= 1;
    x *= x;
  }
  return ans;
}

bool isprime(ll n) {
  if (n == 2 || n == 3)
    return true;
  if (n <= 1 || n % 2 == 0 || (n % 6 != 1 && n % 6 != 5))
    return false;
  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;

template <class _T> static void getval(_T &a) { std::cin >> a; }
template <class _T> static void getval(_T &a, _T &b) { std::cin >> a >> b; }
template <class _T> static void getval(_T &a, _T &b, _T &c) {
  std::cin >> a >> b >> c;
}
template <class _T> static _T tp_abs(_T a) {
  if (a < (_T)0) {
    a *= (_T)-1;
  }
  return a;
}
template <class _T> static void tp_swap(_T &right, _T &left) {
  //	usage:  tp_swap<type name>(a, b);
  _T tmp = right;
  right = left;
  left = tmp;
}

static void C();

int main() {
  C();
  fflush(stdout);
  return 0;
}

static void C() {
  int N, M;
  getval(N, M);

  LLONG K;
  getval(K);

  std::vector<LLONG> sumAi(N + 1, 0), sumBi(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    LLONG ai;
    getval(ai);
    sumAi[i] = sumAi[i - 1] + ai;
  }
  for (int i = 1; i <= M; i++) {
    LLONG bi;
    getval(bi);
    sumBi[i] = sumBi[i - 1] + bi;
  }

  int bookMax = 0, bIdx = M;
  for (int a = 0; a <= N; a++) {
    if (sumAi[a] <= K) {
      bookMax = std::max(bookMax, a);
    }
    while (bIdx) {
      if (sumAi[a] + sumBi[bIdx] <= K) {
        bookMax = std::max(bookMax, bIdx + a);
        break;
      } else {
        bIdx--;
      }
    }
  }
  printf("%d\\n", bookMax);
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;

template <class _T> static void getval(_T &a) { std::cin >> a; }
template <class _T> static void getval(_T &a, _T &b) { std::cin >> a >> b; }
template <class _T> static void getval(_T &a, _T &b, _T &c) {
  std::cin >> a >> b >> c;
}
template <class _T> static _T tp_abs(_T a) {
  if (a < (_T)0) {
    a *= (_T)-1;
  }
  return a;
}
template <class _T> static void tp_swap(_T &right, _T &left) {
  //	usage:  tp_swap<type name>(a, b);
  _T tmp = right;
  right = left;
  left = tmp;
}

static void C();

int main() {
  C();
  fflush(stdout);
  return 0;
}

static void C() {
  int N, M;
  getval(N, M);

  LLONG K;
  getval(K);

  std::vector<LLONG> sumAi(N + 1, 0), sumBi(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    LLONG ai;
    getval(ai);
    sumAi[i] = sumAi[i - 1] + ai;
  }
  for (int i = 1; i <= M; i++) {
    LLONG bi;
    getval(bi);
    sumBi[i] = sumBi[i - 1] + bi;
  }

  int bookMax = 0, cnt = 0;
  for (int b = M; b >= 0; b--) {
    for (int a = 0; a <= N; a++) {
      if (sumAi[a] + sumBi[b] <= K) {
        bookMax = std::max(bookMax, b + a);
      }
    }
  }
  printf("%d\\n", bookMax);
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

signed main(void) {
  int n, m, k, ans;
  ll sum;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0), b(m + 1, 0);
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; i++) {
    int A;
    cin >> A;
    a[i] = A;
  }
  for (int i = 1; i <= m; i++) {
    int B;
    cin >> B;
    b[i] = B;
  }
  rep(i, n) a[i + 1] += a[i];
  rep(i, m) b[i + 1] += b[i];
  ans = 0;
  rep(i, n + 1) {
    sum = a[i];
    if (sum > k)
      continue;
    int lo, hi;
    lo = 0;
    hi = m + 1;
    while (hi - lo > 1) {
      int mid = (hi + lo) / 2;
      if ((ll)sum + b[mid] > k)
        hi = mid;
      else
        lo = mid;
    }
    ans = max(lo + i, ans);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

signed main(void) {
  ll n, m, k;
  ll sum, cnt, p, q;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  sum = cnt = 0;
  p = a[0];
  q = b[0];
  while (cnt < n + m) {
    int i = 0;
    int j = 0;
    bool ok = false;
    while (p == q && i < n && j < m) {
      p = a[i];
      q = b[j];
      i++;
      j++;
    }
    if ((i < n || j < m) && p == q) {
      if (i >= n)
        ok = true;
    }
    if (p < q)
      ok = true;
    if (ok) {
      if ((ll)sum + p > k)
        break;
      sum += p;
      p = 0;
      if (a.size() > 1) {
        a.erase(a.begin() + i);
        p = a[0];
      }
    } else {
      if ((ll)sum + q > k)
        break;
      sum += q;
      q = 0;
      if (b.size() > 1) {
        b.erase(b.begin() + j);
        q = b[0];
      }
    }
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  std::ios_base::sync_with_stdio(false);
  int n, m;
  long long k;
  std::cin >> n >> m >> k;
  std::vector<long long> a(n + 1), b(m + 1);
  for (int i = 1; i <= n; i++) {
    std::cin >> a[i];
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    std::cin >> b[i];
    b[i] += b[i - 1];
  }
  int i = 0, j = 0;
  while (i <= n && a[i] <= k)
    i++;
  i--;
  while (j <= m && a[i] + b[j] <= k)
    j++;
  j--;
  int c = i + j;
  while (i > 0) {
    i--;
    while (j <= m && a[i] + b[j] <= k)
      j++;
    j--;
    c = std::max(c, i + j);
  }
  std::cout << c << '\\n';
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  std::ios_base::sync_with_stdio(false);
  int n, m;
  long long k;
  std::cin >> n >> m >> k;
  std::vector<long long> a(n + 1), b(m + 1);
  for (int i = 1; i <= n; i++) {
    std::cin >> a[i];
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    std::cin >> b[i];
    b[i] += b[i - 1];
  }
  int c = 0;
  for (int i = 0; i <= n && a[i] <= k; i++) {
    int j = std::lower_bound(b.begin(), b.end(), k - a[i]) - b.begin();
    if (j > m)
      j = m;
    if (b[j] > k - a[j])
      j--;
    if (j < 0)
      j = 0;
    c = std::max(c, i + j);
  }
  std::cout << c << '\\n';
  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 < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
struct Edge {
  ll to;
  ll cost;
};
int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N);
  vector<ll> B(M);
  cin >> A[0];
  REP(i, N - 1) cin >> A[i + 1], A[i + 1] += A[i];
  cin >> B[0];
  REP(i, M - 1) cin >> B[i + 1], B[i + 1] += B[i];
  ll mx = 0;
  REP(i, N) {
    ll tmp = K - A[i];
    if (tmp < 0)
      break;
    auto itr = upper_bound(B.begin(), B.end(), tmp);
    ll tmp2 = (ll)distance(B.begin(), itr);

    mx = max(mx, tmp2 + i + 1);
  }
  REP(i, M) {
    if (B[i] <= K)
      mx = max(mx, (ll)i + 1);
  }
  cout << mx << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
struct Edge {
  ll to;
  ll cost;
};
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N + 1), B(M + 1);
  REP(i, N) cin >> A[i];
  REP(i, M) cin >> B[i];
  A[N - 1] = 1e15;
  A[M - 1] = 1e15;
  ll ans = 0;
  int i = 0, j = 0;
  while (K > 0) {
    if (A[i] < B[j]) {
      if (K < A[i])
        break;
      K -= A[i];
      ans++;
      i++;
    } else {
      if (K < B[j])
        break;
      K -= B[j];
      ans++;
      j++;
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll N, M, K, k = 0, s = -1, t = -1, ans = 0, x = 0, y = 0;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  rep(i, N) {
    cin >> A.at(i);
    if (x == 0 && k + A.at(i) <= K) {
      k += A.at(i);
      s = i;
    } else {
      x = 1;
    }
  }
  rep(j, M) {
    cin >> B.at(j);
    if (y == 0 && k + B.at(j) <= K) {
      k += B.at(j);
      t = j;
    } else {
      y = 1;
    }
  }
  ans = s + 1 + t + 1;
  while (s >= 0) {
    ll a;
    k -= A.at(s);
    s--;
    while (t < M - 1 && k + B.at(t + 1) <= K) {
      k += B.at(t + 1);
      t++;
    }
    a = s + 1 + t + 1;
    ans = max(ans, a);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  ll N, M, K, k = 0, s = -1, t = -1, ans = 0;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  rep(i, N) {
    cin >> A.at(i);
    if (k + A.at(i) <= K) {
      k += A.at(i);
      s = i;
    }
  }
  rep(j, M) {
    cin >> B.at(j);
    if (k + B.at(j) <= K) {
      k += B.at(j);
      t = j;
    }
  }
  ans = s + 1 + t + 1;
  while (s >= 0) {
    ll a;
    k -= A.at(s);
    s--;
    while (t < N - 1 && k + B.at(t + 1) <= K) {
      k += B.at(t + 1);
      t++;
    }
    a = s + 1 + t + 1;
    ans = max(ans, a);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<long long> vl;

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vl a(N + 2), b(M + 2);
  a[0] = 0;
  b[0] = 0;
  a[N + 1] = 1e12;
  b[M + 1] = 1e12;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= M; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= N; i++) {
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= M; i++) {
    b[i] += b[i - 1];
  }
  long long ans = 0;
  for (int i = 0; i <= M; i++) {
    if (b[i] > K)
      break;
    long long index = upper_bound(a.begin(), a.end(), K - b[i]) - a.begin();
    // cout << i << "  " << index-1 << endl;
    if (index != 0)
      index--;
    ans = max(ans, i + index);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<long long> vl;

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vl a(N + 2), b(M + 2);
  a[0] = 0;
  b[0] = 0;
  a[N + 1] = 1e12;
  b[N + 1] = 1e12;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= M; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= N; i++) {
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= M; i++) {
    b[i] += b[i - 1];
  }
  long long ans = 0;
  for (int i = 0; i <= M; i++) {
    if (b[i] > K)
      break;
    long long index = upper_bound(a.begin(), a.end(), K - b[i]) - a.begin();
    // cout << i << "  " << index-1 << endl;
    if (index != 0)
      index--;
    ans = max(ans, i + index);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, m, K, ans;
long long a[200004], b[200004];
int main() {
  cin >> n >> m >> K;
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i] = a[i - 1] + a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i], b[i] = b[i - 1] + b[i];
  for (int i = 0; i <= n; i++)
    if (K >= a[i]) {
      int res = upper_bound(b + 1, b + 1 + m, K - a[i]) - b - 1;
      ans = max(ans, res + i);
    }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, K, ans;
long long a[100004], b[100004];
int main() {
  cin >> n >> m >> K;
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i] = a[i - 1] + a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i], b[i] = b[i - 1] + b[i];
  for (int i = 0; i <= n; i++)
    if (K >= a[i]) {
      int res = upper_bound(b + 1, b + 1 + m, K - a[i]) - b - 1;
      ans = max(ans, res + i);
    }
  cout << ans;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  ll k;
  cin >> k;

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

  int ans = 0;
  int j = m;
  ll now = 0;
  int flag = 0;
  for (int i = 0; i <= n; i++) {
    now = sum_a[i];
    flag = 0;
    while (j >= 0) {
      if (now + sum_b[j] <= k) {
        flag = 1;
        break;
      }
      j--;
    }

    if (ans < i + j && flag == 1) {
      ans = i + j;
    }
  }

  cout << ans << endl;
}

Error: -11

Fixed submission:

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

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

  ll k;
  cin >> k;

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

  int ans = 0;
  int j = m;
  ll now = 0;
  for (int i = 0; i <= n; i++) {
    now = sum_a[i];
    while (1) {
      if (now + sum_b[j] <= k) {
        break;
      }
      j--;
    }

    if (ans < i + j) {
      ans = i + j;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll n, m, k, l, o, p;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  a[0] = 0;
  b[0] = 0;
  for (ll i = 1; i <= n; i++) {
    ll x, y;
    cin >> x;
    a[i] = x + a[i - 1];
  }
  for (ll i = 1; i <= m; i++) {
    ll x, y;
    cin >> x;
    b[i] = x + b[i - 1];
  }
  ll ans = 0;
  o = m;
  for (ll i = 0; i <= n; i++) {
    for (ll j = o; j >= 0; j--) {
      if (a[i] + b[j] <= k) {
        o = j;
        if (i + j > ans) {
          ans = i + j;
        }
        break;
      }
    }
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll n, m, k, l, o, p;
  cin >> n >> m >> k;
  vector<ll> a, b;
  a.push_back(0);
  b.push_back(0);
  for (ll i = 1; i <= n; i++) {
    ll x, y;
    cin >> x;
    y = x + a[i - 1];
    a.push_back(y);
  }
  for (ll i = 1; i <= m; i++) {
    ll x, y;
    cin >> x;
    y = x + b[i - 1];
    b.push_back(y);
  }
  ll flag = 1;
  ll ans = 0;
  for (ll i = n; i >= 0; i--) {
    for (ll j = m; j >= 0; j--) {
      if (a[i] + b[j] <= k) {
        if (i + j > ans) {
          ans = i + j;
        }
        break;
      }
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

int N, M, K;
int A[200005], B[200005];
int ans;

int main() {
  scanf("%d%d%d", &N, &M, &K);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  for (int i = 0; i < M; i++)
    scanf("%d", &B[i]);
  int ai = 0, bi = 0, curk = 0, curans = 0;
  while (curk + A[ai] <= K && ai < N) {
    curans++;
    curk += A[ai++];
    ans = max(ans, curans);
  }
  while (curk + B[bi] <= K && bi < M) {
    curans++;
    curk += B[bi++];
    ans = max(ans, curans);
  }
  for (int i = ai - 1; i >= 0; i--) {
    curans--;
    curk -= A[i];
    while (curk + B[bi] <= K && bi < M) {
      curans++;
      curk += B[bi++];
      ans = max(ans, curans);
    }
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, m, k;
int a[200005], b[200005];
queue<int> qa, qb;
int ans;

int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    qa.push(a[i]);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &b[i]);
    qb.push(b[i]);
  }
  while (!qa.empty() || !qb.empty()) {
    if (qa.empty() && qb.front() <= k) {
      k -= qb.front();
      ans++;
      qb.pop();
      continue;
    }
    if (qb.empty() && qa.front() <= k) {
      k -= qa.front();
      ans++;
      qa.pop();
      continue;
    }
    if (qa.front() < qb.front() && qa.front() <= k) {
      k -= qa.front();
      ans++;
      qa.pop();
      continue;
    } else if (qb.front() <= k) {
      k -= qb.front();
      ans++;
      qb.pop();
      continue;
    }
    break;
  }
  printf("%d\\n", ans);
  return 0;
}