alexjercan/bugnet huggingface dataset:
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, r, n) for (int i = (int)(r); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define INF 2147483647
#define INF64 9223372036854775807
#define MOD 1000000007

int main() {

  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0), b(m + 1, 0);
  FOR(i, 1, n) {
    int64_t x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  FOR(i, 1, m) {
    int64_t x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }

  int64_t ans = 0;

  FOR(i, 0, n) {
    if (a[i] > k)
      break;
    int64_t x = m / 2;
    int64_t best = x;
    while (x > 1) {
      x /= 2;
      if (a[i] + b[best] > k)
        best -= x;
      if (a[i] + b[best] == k)
        break;
      if (a[i] + b[best] < k)
        best += x;
    }
    if (a[i] + b[best] > k) {
      while (a[i] + b[best] > k)
        best--;
    }
    if (a[i] + b[best] < k) {
      while (a[i] + b[best] <= k) {
        best++;
        if (best > m)
          break;
      }
      best--;
    }
    if (i + best > ans)
      ans = i + best;
  }

  cout << ans << 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, r, n) for (int i = (int)(r); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define INF 2147483647
#define INF64 9223372036854775807
#define MOD 1000000007

int main() {

  int64_t n, m, k;
  cin >> n >> m >> k;
  vector<int64_t> a(n + 1, 0), b(m + 1, 0);
  FOR(i, 1, n) {
    int64_t x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  FOR(i, 1, m) {
    int64_t x;
    cin >> x;
    b[i] = b[i - 1] + x;
  }

  int64_t ans = 0;

  FOR(i, 0, n) {
    if (a[i] > k)
      break;
    int64_t x = n / 2;
    int64_t best = x;
    while (x > 1) {
      x /= 2;
      if (a[i] + b[best] > k)
        best -= x;
      if (a[i] + b[best] == k)
        break;
      if (a[i] + b[best] < k)
        best += x;
    }
    if (a[i] + b[best] > k) {
      while (a[i] + b[best] > k)
        best--;
    }
    if (a[i] + b[best] < k) {
      while (a[i] + b[best] <= k) {
        best++;
        if (best > m)
          break;
      }
      best--;
    }
    if (i + best > ans)
      ans = i + best;
  }

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

#include <bits/stdc++.h>
using namespace std;
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define SIZE 100005
#define INF 1000000000000000LL
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

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

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

  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define SIZE 100005
#define INF 1000000000000000LL
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  ll n, m, k;
  cin >> n >> m >> k;
  vll a(n, 1e9 + 5);
  vll b(m, 1e9 + 5);

  rep(i, n) { cin >> a[i]; }
  rep(i, m) { cin >> b[i]; }

  int num = max(n, m);
  ll ans = 0, time = 0, cnt = 0, j = 0, l = 0;
  for (ll i = 0; i < num; i++) {
    time = min(a[j], b[l]);
    if (time == b[j]) {
      j++;
    } else {
      l++;
    }
    if ((ans += time) > k) {
      cnt = i - 1;
      break;
    }
    ans += time;
  }

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

#include <bits/stdc++.h>
using namespace std;
// #define cerr if(false)cerr
#define watch(x) cerr << "\\n" << (#x) << " is " << (x) << endl;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> ii;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vii;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<vector<ld>> vvld;
typedef vector<bool> vbl;
typedef vector<vbl> mbl;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(s) (s).begin(), (s).end()
const ll MOD = 1e9 + 7;

inline void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vll a(n), b(m);
  for (auto &it : a)
    cin >> it;
  for (auto &it : b)
    cin >> it;
  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i < m; i++)
    b[i] += b[i - 1];
  ll ans = (ll)(upper_bound(all(b), k) - b.begin());
  for (int i = 0; i < n; i++) {
    if (k >= a[i]) {
      ans = max(ans, (ll)(upper_bound(all(b), k - a[i]) - b.begin() + i + 1));
    }
  }
  cout << ans << endl;
}

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

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define cerr if(false)cerr
#define watch(x) cerr << "\\n" << (#x) << " is " << (x) << endl;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> ii;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vii;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<vector<ld>> vvld;
typedef vector<bool> vbl;
typedef vector<vbl> mbl;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(s) (s).begin(), (s).end()
const ll MOD = 1e9 + 7;

inline void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vll a(n), b(m);
  for (auto &it : a)
    cin >> it;
  for (auto &it : b)
    cin >> it;
  for (int i = 1; i < n; i++)
    a[i] += a[i - 1], b[i] += b[i - 1];
  ll ans = (ll)(lower_bound(all(b), k) - b.begin());
  for (int i = 0; i < n; i++) {
    if (k >= a[i]) {
      ans = max(ans, (ll)(lower_bound(all(b), k - a[i]) - b.begin() + i + 1));
    }
  }
  cout << ans << endl;
}

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

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;

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

  int n, m, k, tmp;
  cin >> n >> m >> k;
  queue<int> a, b;
  unsigned long long ts;
  vector<unsigned long long> sa(n), sb(m);
  ts = 0;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    ts += tmp;
    sa[i] = ts;
    a.push(tmp);
  }
  ts = 0;
  for (int i = 0; i < m; i++) {
    cin >> tmp;
    ts += tmp;
    sb[i] = ts;
    b.push(tmp);
  }

  int ans = 0, tans;

  for (int i = 0; i <= n; i++) {
    tans = 0;
    if (!i || sa[i - 1] <= k) {
      tans += i; // first partition
      int left =
          k - (i ? sa[i - 1] : 0); // max amount we can get from the other deck

      if (sb[0] > left) {
        ans = max(tans, ans);
        continue;
      }

      if (sb[m - 1] <= left) {
        ans = max(tans + m, ans);
        continue;
      }

      int lo = 0, hi = m - 1, mid, res;
      while (true) {
        mid = (lo + hi) / 2;
        if (sb[mid] <= left && sb[mid + 1] > left) {
          res = mid;
          break;
        }
        if (sb[mid] <= left) {
          lo = mid;
        } else {
          hi = mid;
        }
      }
      ans = max(tans + res + 1, ans);
    }
  }

  cout << ans;
}

/*
3 4 100000
60 90 120
80 150 80 150



 10 10 100
 20 20 20 20 20
 25 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


 */

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;

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

  int n, m, k, tmp;
  cin >> n >> m >> k;
  queue<int> a, b;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a.push(tmp);
  }
  for (int i = 0; i < m; i++) {
    cin >> tmp;
    b.push(tmp);
  }
  int cnt = 0;
  while ((!a.empty() && !b.empty() && min(a.front(), b.front()) <= k) ||
         (a.empty() && !b.empty() && b.front() <= k) ||
         (!a.empty() && b.empty() && a.front() <= k)) {
    if (!a.empty() && a.front() < b.front()) {
      k -= a.front();
      a.pop();
    } else {
      k -= b.front();
      b.pop();
    }
    cnt++;
  }
  cout << cnt;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long

const int mxN = 2e5 + 10;
int n, m;
ll k, a[mxN], b[mxN];

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

  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 = 0; i <= n; ++i) {
    if (a[i] > k)
      break;
    // int j = m;
    // while (a[i] + b[j] > k)
    //   j--;
    int j = upper_bound(b, b + m + 1, k - a[i]) - b;
    ans = max(i + j - 1, ans);
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

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

#define ll long long

const int mxN = 2e5 + 10;
int n, m;
ll k, a[mxN], b[mxN];

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

  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 = 0; i <= n; ++i) {
    if (a[i] > k)
      break;
    int j = m;
    while (a[i] + b[j] > k)
      j--;
    ans = max(i + j, ans);
  }
  cout << ans << "\\n";
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>

#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#define int64 long long
#define uint64 unsigned long long
using namespace std;

int main() {
  int64 nn, mm, kk, tt, ans = 0;
  cin >> nn >> mm >> kk;
  vector<int64> aa(nn + 1, 0), bb(mm + 1, 0);
  for (int64 ii = 1; ii <= nn; ii++) {
    cin >> aa[ii];
    aa[ii] += aa[ii - 1];
  }
  for (int64 ii = 1; ii <= mm; ii++) {
    cin >> bb[ii];
    bb[ii] += bb[ii - 1];
  }
  for (int64 ii = 0; ii <= nn; ii++) {
    tt = aa[ii];
    if (tt > kk) {
      break;
    }
    int64 jj, uu = mm + 1, dd = -1, oo;
    while (uu - dd > 1) {
      oo = (uu + dd) / 2;
      if (tt + bb[oo] <= kk) {
        dd = oo;
      } else {
        uu = oo;
      }
    }
    ans = max(ans, ii + dd);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>

#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#define int64 long long
#define uint64 unsigned long long
using namespace std;

int main() {
  int64 nn, mm, kk, tmp, ans = -1;
  cin >> nn >> mm >> kk;
  tmp = mm;
  vector<int64> aa(nn + 1), bb(mm + 1);
  aa[0] = 0;
  bb[0] = 0;
  for (int64 ii = 1; ii <= nn; ii++) {
    cin >> aa[ii];
    aa[ii] += aa[ii - 1];
  }
  for (int64 ii = 1; ii <= mm; ii++) {
    cin >> bb[ii];
    bb[ii] += bb[ii - 1];
  }
  if (aa[nn] + bb[mm] <= kk) {
    cout << (nn + mm) << endl;
    return 0;
  }
  for (int64 ii = 0; ii <= nn; ii++) {
    if (aa[ii] > kk) {
      if (ans < ii) {
        ans = ii - 1;
        break;
      }
    }
    for (int64 jj = tmp; jj >= 0; jj--) {
      if (aa[ii] + bb[jj] <= kk && ans < ii + jj) {
        ans = ii + jj;
        tmp = jj;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;
int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N + 1);
  vector<long long> B(M + 1);
  for (int n = 0; n < N; ++n) {
    int m = 0;
    cin >> m;
    A[n + 1] = A[n] + m;
  }
  for (int n = 0; n < M; ++n) {
    int m = 0;
    cin >> m;
    B[n + 1] = B[n] + m;
  }
  int ans = 0;
  for (int n = 0; n <= N; ++n) {
    int z = K - A[n];
    if (z < 0) {
      break;
    }
    auto itr = upper_bound(B.begin(), B.end(), z);
    int num = itr - B.begin() - 1;
    // cout << z << " " << *itr << " " << num << endl;
    ans = max(ans, n + num);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N + 1);
  vector<long long> B(M + 1);
  for (int n = 0; n < N; ++n) {
    int m = 0;
    cin >> m;
    A[n + 1] = A[n] + m;
    // cout << A[n+1] << endl;
  }
  for (int n = 0; n < M; ++n) {
    int m = 0;
    cin >> m;
    B[n + 1] = B[n] + m;
    // cout << B[n+1] << endl;
  }
  int ans = 0;
  for (int n = 0; n <= N; ++n) {
    for (int m = 0; m <= M; ++m) {
      if (A[n] + B[m] <= K) {
        ans = max(ans, n + m);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  long long int k;
  cin >> n >> m >> k;
  vector<int> A(n + 1);
  vector<int> B(m + 1);
  vector<long long int> a(n + 1);
  vector<long long int> b(m + 1);
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  for (int j = 1; j <= m; j++)
    cin >> B[j];
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; i++)
    a[i] = a[i - 1] + A[i];
  for (int j = 1; j <= m; j++)
    b[j] = b[j - 1] + B[j];

  int best_j = m;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (a[i] > k)
      break;
    for (int j = best_j; j >= 0; j--) {
      if (b[j] <= k - a[i]) {
        best_j = j;
        ans = max(ans, i + j);
        break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int n, m;
long long int k;
vector<int> a;
vector<int> b;
int max_num = -1;
// minが残り分数
void dfs(int min, int i, int j, int num) {
  if (min < 0) {
    if (max_num < num - 1)
      max_num = num - 1;
    return;
  }
  // Aの一番上を選ぶ
  dfs(min - a[i + 1], i + 1, j, num + 1);
  // Bの一番上を選ぶ
  dfs(min - b[j + 1], i, j + 1, num + 1);
}

int main() {
  cin >> n >> m >> k;
  int num1, num2;
  for (int i = 0; i < n; i++) {
    cin >> num1;
    a.push_back(num1);
  }
  for (int i = 0; i < m; i++) {
    cin >> num2;
    b.push_back(num2);
  }

  dfs(k, -1, -1, 0);

  cout << max_num << endl;

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

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

int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  long long int N, M, K, S;
  cin >> N >> M >> K;
  long long int A[N + 1], B[M + 1];
  A[0] = 0;
  B[0] = 0;
  long long int C = N;

  for (long long int i = 1; i < N + 1; i++) {
    cin >> A[i];
    A[i] = A[i] + A[i - 1];
    if (C == N && A[i] > K) {
      C = i - 1;
    }
  }

  for (long long int i = 1; i < M + 1; i++) {
    cin >> B[i];
    B[i] = B[i] + B[i - 1];
  }

  long long int D = 0;

  while (A[C] + B[D + 1] <= K && D < M) {
    D++;
  }
  // cout << C << ":" << D << endl;

  long long int Max = C + D;
  for (long long int i = 1; i < C + 1; i++) {
    while (A[C - i] + B[D + 1] <= K) {
      if (D == M) {
        break;
      }
      D++;
    }
    S = C - i + D;
    // cout << C-i << ":" << D << endl;
    if (S > Max) {
      Max = S;
    }
  }
  cout << Max << endl;
  return 0;
}

int Exp(long long int exp_n, long long int exp_e, long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}


Error: 0

Fixed submission:

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

int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  long long int N, M, K, S;
  cin >> N >> M >> K;
  long long int A[N + 1], B[N + 1];
  A[0] = 0;
  B[0] = 0;
  long long int C = N;

  for (long long int i = 1; i < N + 1; i++) {
    cin >> A[i];
    A[i] = A[i] + A[i - 1];
    if (C == N && A[i] > K) {
      C = i - 1;
    }
  }

  for (long long int i = 1; i < M + 1; i++) {
    cin >> B[i];
    B[i] = B[i] + B[i - 1];
  }

  long long int D = 0;

  while (A[N] + B[D] < K && D < M) {
    D++;
  }

  S = C + D;
  long long int Max = C + D;
  for (long long int i = 1; i < C + 1; i++) {
    while (A[N - i] + B[D] < K) {
      if (D == M) {
        break;
      }
      D++;
    }
    S = C - i + D;
    if (S > Max) {
      Max = S;
    }
  }
  cout << Max << endl;
  return 0;
}

int Exp(long long int exp_n, long long int exp_e, long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
static const ll INF = 1ll << 60;
static const int MOD = 1e9 + 7;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  vector<ll> asum(N + 1, 0), bsum(M, 0);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++)
    cin >> B[i];
  bsum[0] = B[0];

  for (int i = 1; i <= N; i++)
    asum[i] = A[i - 1] + asum[i - 1];
  for (int i = 1; i < M; i++)
    bsum[i] = B[i] + bsum[i - 1];

  ll ans = 0;

  for (ll a = 0; a <= N; a++) {
    ll cur = 0;
    cur = asum[a];
    if (cur > K)
      break;
    ll lim = K - asum[a];
    auto itr = upper_bound(bsum.begin(), bsum.end(), lim);
    ll b = itr - bsum.begin();

    ans = max(ans, b + a);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
static const ll INF = 1ll << 60;
static const int MOD = 1e9 + 7;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  vector<ll> asum(N + 1, 0);
  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++)
    asum[i] = A[i - 1] + asum[i - 1];

  ll ans = 0;

  for (ll a = 0; a <= N; a++) {
    ll cur = 0;
    ll bcnt = 0;
    cur += asum[a];
    if (asum[a] > K)
      continue;
    for (ll b = 0; b < M; b++) {
      if (cur + B[b] > K) {
        bcnt = b;
        break;
      }
      cur += B[b];
      if (b == M - 1)
        bcnt = M;
    }

    ans = max(ans, a + bcnt);
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>

#define llint long long int

using namespace std;

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

  cin >> N >> M >> K;

  vector<llint> A(N), B(M);

  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];
  }
  auto it = upper_bound(B.begin(), B.end(), K);

  int ans = (it - B.begin());

  for (int i = 0; i < N; i++) {
    if (A[i] > K) {
      break;
    }
    it = upper_bound(B.begin(), B.end(), K - A[i]);

    int temp = (i + 1) + (it - B.begin());

    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define llint long long int

using namespace std;

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

  cin >> N >> M >> K;

  vector<llint> A(N), B(M);

  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];
  }
  auto it = upper_bound(B.begin(), B.end(), K);

  int ans = (it - B.begin());

  for (int i = 0; A[i] < K; i++) {
    auto it = upper_bound(B.begin(), B.end(), K - A[i]);

    int temp = (i + 1) + (it - B.begin());

    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}