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)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[6] = {0, 1, 0, -1, 1, -1};
const int vy[6] = {1, 0, -1, 0, 1, 1};

ll f(ll a) {
  ll rep = 0;
  for (ll i = 1; i <= a / 2; ++i) {
    if (a % i == 0) {
      rep++;
    }
  }
  return rep + 1;
}

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

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll ans = 0;
  ll sum = 0;
  ll i = 0;
  ll j = 0;
  while (sum <= k) {
    if (i == n)
      break;
    i++;
    sum += a[i - 1];
  }
  if (sum == k || sum < k) {
    ans = i;
  } else {
    ans = i - 1;
  }
  while (sum <= k) {
    if (j == m)
      break;
    j++;
    sum += b[j - 1];
  }
  if (sum == k || sum < k) {
    ans += j;
    cout << ans << endl;
    return 0;
  } else {
    ans += j - 1;
    sum -= b[j - 1];
    j--;
  }
  while (i) {
    i--;
    sum -= a[i];
    while (sum <= k) {
      if (j == m)
        break;
      j++;
      sum += b[j - 1];
    }
    ll tmp;
    if (sum == k || sum < k) {
      tmp = j;
    } else {
      tmp = j - 1;
      sum -= b[j - 1];
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;

  cout << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

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

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[6] = {0, 1, 0, -1, 1, -1};
const int vy[6] = {1, 0, -1, 0, 1, 1};

ll f(ll a) {
  ll rep = 0;
  for (ll i = 1; i <= a / 2; ++i) {
    if (a % i == 0) {
      rep++;
    }
  }
  return rep + 1;
}

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

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll ans = 0;
  ll sum = 0;
  ll i = 0;
  ll j = 0;
  while (sum <= k) {
    i++;
    sum += a[i - 1];
    if (i == n)
      break;
  }
  if (sum == k || sum < k) {
    ans = i;
  } else {
    ans = i - 1;
  }
  while (sum <= k) {
    j++;
    sum += b[j - 1];
    if (j == m)
      break;
  }
  if (sum == k || sum < k) {
    ans += j;
    cout << ans << endl;
    return 0;
  } else {
    ans += j - 1;
    sum -= b[j - 1];
    j--;
  }
  while (i) {
    i--;
    sum -= a[i];
    while (sum <= k) {
      j++;
      sum += b[j - 1];
      if (j == m)
        break;
    }
    ll tmp;
    if (sum == k || sum < k) {
      tmp = j;
    } else {
      tmp = j - 1;
      sum -= b[j - 1];
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;

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

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

int main(int argc, char const *argv[]) {
  long n, m, k;
  cin >> n >> m >> k;
  vector<long> A(n + 1);
  vector<long> B(m + 1);

  for (long i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  for (long i = 0; i < m; i++) {
    cin >> B.at(i);
  }

  vector<long> a(n + 1);
  vector<long> b(m + 1);

  long temp_sum = 0;
  for (long i = 0; i <= n; i++) {
    a.at(i) = temp_sum;
    temp_sum += A.at(i);
  }

  temp_sum = 0;
  for (long i = 0; i <= m; i++) {
    b.at(i) = temp_sum;
    temp_sum += B.at(i);
  }

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

  return 0;
}


Error: TLE

Fixed submission:

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

int main(int argc, char const *argv[]) {
  long n, m, k;
  cin >> n >> m >> k;
  vector<long> A(n + 1);
  vector<long> B(m + 1);

  for (long i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  for (long i = 0; i < m; i++) {
    cin >> B.at(i);
  }

  vector<long> a(n + 1);
  vector<long> b(m + 1);

  long temp_sum = 0;
  for (long i = 0; i <= n; i++) {
    a.at(i) = temp_sum;
    temp_sum += A.at(i);
  }

  temp_sum = 0;
  for (long i = 0; i <= m; i++) {
    b.at(i) = temp_sum;
    temp_sum += B.at(i);
  }

  long ans = 0;
  for (long i = 0; i <= n; i++) {
    if (a.at(i) > k) {
      continue;
    }
    for (long j = m; j >= 0; j--) {
      if (a.at(i) + b.at(j) <= k) {
        ans = max(ans, i + j);
        break;
      }
    }
  }
  cout << ans << endl;

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

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

using namespace std;
using ull = unsigned long long;

#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i, s, n) for (int i = s; i < (int)(n); ++i)

int main() {
  ull N, M, K;
  cin >> N >> M >> K;

  vector<ull> As(N + 1);
  vector<ull> Bs(M + 1);

  {
    ull sum = 0;
    Rep(i, N) {
      ull j;
      cin >> j;
      sum += j;
      As[i + 1] = sum;
    }
  }
  {
    ull sum = 0;
    Rep(i, M) {
      ull j;
      cin >> j;
      sum += j;
      Bs[i + 1] = sum;
    }
  }

  int answer = 0;
  Rep(n, N + 1) {
    ull remain = K - As[n];
    auto bound = lower_bound(Bs.begin(), Bs.end(), remain);
    if (*bound > remain)
      bound--;
    if (bound != Bs.end()) {
      int pos = (int)(bound - Bs.begin());
      answer = max(answer, n + pos);
    }
  }

  cout << answer << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;
using ull = unsigned long long;

#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i, s, n) for (int i = s; i < (int)(n); ++i)

int main() {
  ull N, M, K;
  cin >> N >> M >> K;

  vector<ull> As(N + 1);
  vector<ull> Bs(M + 1);

  {
    ull sum = 0;
    Rep(i, N) {
      ull j;
      cin >> j;
      sum += j;
      As[i + 1] = sum;
    }
  }
  {
    ull sum = 0;
    Rep(i, M) {
      ull j;
      cin >> j;
      sum += j;
      Bs[i + 1] = sum;
    }
  }

  int answer = 0;
  Rep(n, N + 1) {
    Rep(m, M + 1) {
      if (As[n] + Bs[m] <= K) {
        answer = max(answer, n + m);
      }
    }
  }

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

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

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

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

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

  int n, m, k;
  cin >> n >> m >> k;
  vi a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  int ac = 0, bc = 0;
  while (ac < n) {
    if (a[ac] > k)
      break;
    k -= a[ac];
    ac++;
  }
  while (bc < m) {
    if (b[bc] > k)
      break;
    k -= b[bc];
    bc++;
  }

  int ret = ac + bc;
  while (ac > 0 && bc < m - 1) {
    k += a[ac - 1];
    ac--;
    while (bc < m) {
      if (b[bc] > k)
        break;
      k -= b[bc];
      bc++;
    }
    ret = max(ret, ac + bc);
  }

  cout << ret << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

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

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

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

  int n, m, k;
  cin >> n >> m >> k;
  vi a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  int ac = 0, bc = 0;
  while (ac < n) {
    if (a[ac] > k)
      break;
    k -= a[ac];
    ac++;
  }
  while (bc < m) {
    if (b[bc] > k)
      break;
    k -= b[bc];
    bc++;
  }

  int ret = ac + bc;
  while (ac > 0 && bc < m - 1) {
    k += a[ac - 1];
    ac--;
    while (bc < m) {
      if (b[bc] > k)
        break;
      k -= b[bc];
      bc++;
    }
    ret = max(ret, ac + bc);
  }

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

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

int main() {
  int64_t N, M, K, ans = 0;
  cin >> N >> M >> K;
  vector<int64_t> A(N + 1);
  vector<int64_t> B(M + 1);
  A.at(0) = 0;
  B.at(0) = 0;
  rep(i, N) {
    int64_t a;
    cin >> a;
    A.at(i + 1) = a;
    A.at(i + 1) += A.at(i);
  }
  rep(i, M) {
    int64_t a;
    cin >> a;
    B.at(i + 1) = a;
    B.at(i + 1) += B.at(i);
  }
  int64_t j = M;
  rep(i, N + 1) {
    if (A.at(i) > K)
      break;
    int64_t temp = K - A.at(i);
    while (B.at(j) > temp) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans;
}


Error: WA

Fixed submission:

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

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

/**
 *    author        : Saurav Paul
 *    created       : August 21, 2020 12:18 PM
 *    Problem Name  : C - Tsundoku
 *    Problem Limit : 2000 ms , 1024 MB
 *    Problem Url   : https://atcoder.jp/contests/abc172/tasks/abc172_c
 *    @genarated by : ai-virtual-assistant
 **/

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

using ll = long long int;

void solve() {

  int N, M, K;
  cin >> N >> M >> K;

  vector<int> deskA(N), deskB(M);

  for (auto &x : deskA)
    cin >> x;
  for (auto &x : deskB)
    cin >> x;

  vector<ll> ca, cb;

  ca.push_back(0);
  cb.push_back(0);

  for (auto x : deskA)
    ca.push_back(x + ca.back());
  for (auto x : deskB)
    cb.push_back(x + cb.back());

  int can_read = 0;

  for (int i = 0, j = M; i <= N; i++) {
    if (ca[i] > K)
      break;

    while (ca[i] + cb[j] > K)
      j--;

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

  cout << can_read << endl;
}

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

  int testcases = 1;
  for (int i = 1; i <= testcases; i++) {
    solve();
  }

  return 0;
}


Error: 0

Fixed submission:

/**
 *    author        : Saurav Paul
 *    created       : August 21, 2020 12:18 PM
 *    Problem Name  : C - Tsundoku
 *    Problem Limit : 2000 ms , 1024 MB
 *    Problem Url   : https://atcoder.jp/contests/abc172/tasks/abc172_c
 *    @genarated by : ai-virtual-assistant
 **/

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

using ll = long long int;

void solve() {

  int N, M, K;
  cin >> N >> M >> K;

  vector<int> deskA(N), deskB(M);

  for (auto &x : deskA)
    cin >> x;
  for (auto &x : deskB)
    cin >> x;

  vector<ll> ca, cb;

  ca.push_back(0);
  cb.push_back(0);

  for (auto x : deskA)
    ca.push_back(x + ca.back());
  for (auto x : deskB)
    cb.push_back(x + cb.back());

  int can_read = 0;

  for (int i = 1, j = 0; i <= N; i++) {
    if (ca[i] > K)
      break;

    while (j + 1 <= M && cb[j + 1] + ca[i] <= K) {
      j++;
    }
    while (j > 0 && cb[j] + ca[i] > K) {
      j--;
    }
    can_read = max(can_read, i + j);
  }

  for (int i = 1, j = 0; i <= M; i++) {
    if (cb[i] > K)
      break;

    while (j + 1 <= N && ca[j + 1] + cb[i] <= K) {
      j++;
    }
    while (j > 0 && ca[j] + ca[i] > K) {
      j--;
    }
    can_read = max(can_read, i + j);
  }
  cout << can_read << endl;
}

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

  int testcases = 1;
  for (int i = 1; i <= testcases; i++) {
    solve();
  }

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

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

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


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  vector<ll> aa(n, 0), bb(m, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    aa[i + 1] = aa[i] + a[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
    bb[i + 1] = bb[i] + b[i];
  }
  ll ans = 0;
  ll j = m;
  for (ll i = 0; i <= n; ++i) {
    if (aa[i] > k)
      break;
    while (bb[j] > k - aa[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  for (int i = 1; i < n; i++)
    a[i] = a[i] + a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] = b[i] + b[i - 1];
  ll ans = 0;
  rep(i, n) {
    ll cnt = 0;
    if (k - a[i] >= 0)
      cnt = i + 1;
    cnt += upper_bound(all(b), k - a[i]) - b.begin();
    chmax(ans, cnt);
  }
  chmax(ans, ll(upper_bound(all(b), k) - b.begin()));
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  for (int i = 1; i < n; i++)
    a[i] = a[i] + a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] = b[i] + b[i - 1];
  ll ans = 0;
  rep(i, n) {
    ll cnt = 0;
    if (k - a[i] >= 0)
      cnt = i + 1;
    cnt += upper_bound(all(b), k - a[i]) - b.begin();
    chmax(ans, cnt);
  }
  chmax(ans, ll(upper_bound(all(b), k) - b.begin()));
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ll long long int
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define vvi vector<vector<ll>>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define pii pair<ll, ll>
#define rep(x, n) for (ll i = x; i < n; ++i)
#define repd(x, n) for (ll i = x - 1; i >= n; --i)
#define repn(x, n) for (ll j = x; j < n; ++j)
#define repnd(x, n) for (ll j = x - 1; j >= n; --j)
#define dbg(x, y) cout << x << " " << y << endl
#define endl '\\n'
#define M 1000000007
const ll INF = 1ll << 60;

using namespace std;
//-----------------------------------------//

void solve() {

  ll n, m, ms;
  cin >> n >> m >> ms;
  ll A[n], B[m];

  for (ll A_i = 0; A_i < n; A_i++) {
    //			scanf("%lld",&A[A_i]);
    cin >> A[A_i];
  }
  for (ll B_i = 0; B_i < m; B_i++) {
    //			scanf("%lld",&B[B_i]);
    cin >> B[B_i];
  }

  long long sum = 0;
  ll x = 0, y = 0;

  while (x < n && sum + A[x] <= ms) {
    sum += A[x++];
  }

  ll ans = x;

  while (y < m && x >= 0) {
    sum += B[y++];
    while (sum > ms && x > 0) {
      sum -= A[--x];
    }

    if (sum <= ms && ans < x + y) {
      ans = x + y;
    }
  }

  cout << ans << endl;
}

int main() { solve(); }


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int A[100002], B[100002];

int main() {

  int n, m, ms;
  scanf("%d%d%d", &n, &m, &ms);

  for (int A_i = 0; A_i < n; A_i++) {
    scanf("%d", &A[A_i]);
  }
  for (int B_i = 0; B_i < m; B_i++) {
    scanf("%d", &B[B_i]);
  }

  long long sum = 0;
  int x = 0, y = 0;

  while (x < n && sum + A[x] <= ms) {
    sum += A[x++];
  }

  int ans = x;

  while (y < m && x >= 0) {
    sum += B[y++];
    while (sum > ms && x > 0) {
      sum -= A[--x];
    }

    if (sum <= ms && ans < x + y) {
      ans = x + y;
    }
  }

  printf("%d\\n", ans);
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int a[n], b[m];

  vector<long long int> c(n + 1, 0);
  vector<long long int> d(m + 1, 0);
  int k;
  cin >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    c[i + 1] = c[i] + a[i];
  }
  for (int i = 0; i < m; i++)
    d[i + 1] = d[i] + b[i];
  long int ans = 0;
  long int l;
  for (long int i = 0; i <= n; i++) {
    long long int e = k - c[i];
    if (e < 0)
      continue;
    else {
      l = lower_bound(d.begin(), d.end(), e) - d.begin();
      if (l > m || d[l] > e)
        l--;
    }
    ans = max(ans, i + l);
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int a[n], b[m];

  vector<long long int> c(n + 1, 0);
  vector<long long int> d(m + 1, 0);
  int k;
  cin >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= n; i++) {
    c[i] = c[i - 1] + a[i - 1];
    d[i] = d[i - 1] + b[i - 1];
  }
  long int ans = 0;
  long int l;
  for (long int i = 1; i <= n; i++) {
    long long int e = k - c[i];
    if (e < 0)
      continue;
    else {
      l = lower_bound(d.begin(), d.end(), e) - d.begin();
      if (l > m || d[l] > e)
        l--;
    }
    ans = max(ans, i + l);
  }
  cout << ans;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
const int mod = 1e+9 + 7;

const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
const ll INF = 1e10;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> asum(n + 1), bsum(m + 1);
  asum[0] = 0;
  bsum[0] = 0;
  rep(i, n) {
    ll A;
    cin >> A;
    asum[i + 1] = asum[i] + A;
  }
  rep(i, m) {
    ll B;
    cin >> B;
    bsum[i + 1] = bsum[i] + B;
  }
  ll ans = 0;
  ll mx = 0;
  for (int i = 0; i <= n; i++) {
    ll K = k - asum[i];
    if (K < 0)
      break;
    for (int j = m; 0 <= j; j--) {
      if (K - bsum[j] < 0)
        continue;
      else {
        m = j;
        mx = j;
        break;
      }
    }
    ans = max(ans, i + mx);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
const int mod = 1e+9 + 7;

const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
const ll INF = 1e10;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll sum = 0, cnt = 0;
  vector<ll> asum(n + 1);
  asum[0] = 0;
  rep(i, n) {
    sum += a[i];
    if (sum > k)
      break;
    else {
      cnt++;
      asum[i + 1] = asum[i] + a[i];
    }
  }
  // cout<<cnt<<endl;
  // cout<<"UNKO"<<endl;
  // return 0;
  ll ans = cnt;
  for (int i = cnt; 0 <= i; i--) {
    ll SUM = asum[i];
    ll CNT = i;
    for (int j = 0; j < m; j++) {
      SUM += b[j];
      if (SUM > k)
        break;
      else {
        CNT++;
      }
    }
    ans = max(ans, CNT);
  }
  cout << ans << endl;
}