alexjercan/bugnet huggingface dataset:
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;
using ll = long long;

int main() {
  int n, m, k, ans = 0;
  ll a[300000], b[300000];

  cin >> n >> m >> k;
  rep(i, n) cin >> a[i + 1], a[i + 1] += a[i];
  rep(i, m) cin >> b[i + 1], b[i + 1] += b[i];

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

    ans = max(i + j, 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;
using ll = long long;

int main() {
  int n, m, k;
  vector<int> a(300000, 0), b(300000, 0);
  cin >> n >> m >> k;
  rep(i, n) cin >> a.at(i);
  rep(i, m) cin >> b.at(i);

  int i = 0, j = 0, time = 0, ans = 0;
  while (1) {
    if (a.at(i) == 0 && b.at(j) == 0)
      break;

    if (a.at(i) < b.at(j)) {
      if (a.at(i) > 0) {
        time += a.at(i);
        i++;
      } else {
        time += b.at(j);
        j++;
      }
    } else {
      if (b.at(i) > 0) {
        time += b.at(j);
        j++;
      } else {
        time += a.at(i);
        i++;
      }
    }

    if (time > k)
      break;
    else
      ans++;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N + 1), B(M + 1);
  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 bidx = M, ans = 0;
  for (int aidx = 0; aidx <= N; ++aidx) {
    if (A[aidx] > K)
      break;
    while (bidx > 0 && A[aidx] + B[bidx] > K)
      --bidx;
    ans = max(ans, aidx + bidx);
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N), B(M), C;
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  for (int i = 0; i < M; ++i)
    cin >> B[i];

  int aidx = 0, bidx = 0;
  while (aidx < N || bidx < N) {
    if (aidx < N && bidx < N) {
      if (A[aidx] < B[bidx]) {
        C.push_back(A[aidx]);
        aidx++;
      } else {
        C.push_back(B[bidx]);
        bidx++;
      }
      continue;
    }
    if (aidx < N) {
      C.push_back(A[aidx]);
      aidx++;
    } else {
      C.push_back(B[bidx]);
      bidx++;
    }
  }

  int ans = 0;
  for (int i = 0; i < N + M; ++i) {
    if (K < C[i])
      break;
    K -= C[i];
    ans++;
  }

  cout << ans << endl;

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

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

#define ll long long
#define mod 1000000007

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

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> arr1(n);
  vector<ll> arr2(m);

  for (int i = 0; i < n; i++)
    cin >> arr1[i];
  for (int i = 0; i < m; i++)
    cin >> arr2[i];

  vector<ll> pa(n + 1, 0);
  vector<ll> pb(m + 1, 0);

  for (int i = 1; i <= n; i++)
    pa[i] = pa[i - 1] + arr1[i - 1];
  for (int i = 1; i <= m; i++)
    pb[i] = pb[i - 1] + arr2[i - 1];

  int ans = 0;
  int j = m;
  for (int i = 0; i <= n; i++) {
    if (pa[i] > k)
      break;

    while (j >= 0 && k - pa[i] < pb[j])
      j--;

    ans = max(ans, i + j);
  }
  cout << ans << "\\n";
}


Error: 0

Fixed submission:

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

#define ll long long
#define mod 1000000007

unordered_map<string, int> mem;

ll __max(ll a, ll b) { return a > b ? a : b; }

ll max_read(vector<ll> &arr1, vector<ll> &arr2, ll n, ll m, ll l) {
  vector<vector<vector<ll>>> dp(
      n + 1, vector<vector<ll>>(m + 1, vector<ll>(l + 1, 0)));

  dp[0][0][0] = 1;

  for (int i = 1; i <= n; i++)
    if (arr1[i - 1] <= l)
      dp[i][0][arr1[i - 1]] = 1;
  for (int i = 1; i <= m; i++)
    if (arr2[i - 1] <= l)
      dp[0][i][arr2[i - 1]] = 1;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 1; k <= l; k++) {
        int a = (k - arr1[i - 1] >= 0) ? 1 + dp[i - 1][j][k - arr1[i - 1]] : 0;
        int b = (k - arr2[j - 1] >= 0) ? 1 + dp[i][j - 1][k - arr2[j - 1]] : 0;

        dp[i][j][k] = max(a, b);
      }
    }
  }

  int _max = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      _max = __max(dp[i][j][l], _max);
    }
  }
  return _max;
}

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

  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> arr1(n);
  vector<ll> arr2(m);
  mem.clear();
  for (int i = 0; i < n; i++)
    cin >> arr1[i];
  for (int i = 0; i < m; i++)
    cin >> arr2[i];

  cout << max_read(arr1, arr2, n, m, k) << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  int ans = 0;
  int f = n;
  ll o = k - sum;
  if (o >= 0) {
    int pp = lower_bound(b.begin(), b.end(), o) - b.begin();
    if (b[pp] == o) {
      pp++;
    }
    ans = max(ans, pp + f);
  }
  for (int i = n - 1; i >= 0; i--) {
    f--;
    sum -= a[i];
    ll t = k - sum;
    if (t <= 0) {
      continue;
    } else {
      int p = lower_bound(b.begin(), b.end(), t) - b.begin();
      if (b[p] == t) {
        p++;
      }
      ans = max(ans, p + f);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI 3.14159265358979323846264338327950
#define INF 1e18
ll n, m;
ll k;
vector<ll> a;
vector<ll> b;

bool C(ll x) {
  if (n + m < x) {
    return false;
  }
  ll numa = x;
  ll numb = 0;
  if (x > n) {
    numa = n;
    numb = x - n;
  }
  ll sum = 0;
  rep(i, numa) { sum += a[i]; }
  rep(i, numb) { sum += b[i]; }
  ll res = sum;
  for (int i = 1; i <= (m - numb); i++) {
    sum = sum - a[numa - i] + b[numb + i - 1];
    if (res > sum) {
      res = sum;
    }
  }

  return k >= res;
}
int main() {
  cin >> n >> m >> k;
  a.resize(n);
  b.resize(m);
  rep(i, n) { cin >> a[i]; }
  rep(i, m) { cin >> b[i]; }
  if (m > n) {
    vector<ll> k = b;
    b = a;
    a = k;
    swap(n, m);
  }
  ll lb = 0, ub = INF;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (C(mid)) {
      lb = mid;
    } else {
      ub = mid;
    }
  }
  cout << lb << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

vector<ll> a, b;
ll n, m, k, maxi = 0;

void func(int aindex, int bindex, ll ans, ll minu, char c) {
  if (c == 'a') {
    ans++;
    minu += a[aindex];
    aindex++;
  } else {
    ans++;
    minu += b[bindex];
    bindex++;
  }

  if (minu > k) {
    maxi = max(maxi, ans - 1);
    return;
  }

  if (aindex == n && bindex == m) {
    maxi = max(maxi, ans);
    return;
  } else if (aindex == n) {
    func(aindex, bindex, ans, minu, 'b');
  } else if (bindex == m) {
    func(aindex, bindex, ans, minu, 'a');
  } else {
    func(aindex, bindex, ans, minu, 'a');
    func(aindex, bindex, ans, minu, 'b');
  }
}

int main() {

  cin >> n >> m >> k;
  a.resize(n);
  b.resize(m);

  REP(i, n) cin >> a[i];
  REP(i, m) cin >> b[i];

  func(0, 0, 0, 0, 'a');
  func(0, 0, 0, 0, 'b');

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

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

typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 5e5 + 1;
const ll INF = 1e10;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  for (ll &i : a)
    cin >> i;
  for (ll &i : b)
    cin >> i;
  int ans = 0, aptr = 0, bptr = 0;
  ll sum = 0;
  while (aptr < n and sum + a[aptr] <= k)
    sum += a[aptr++];
  ans = aptr;
  while (aptr) {
    while (bptr < m and sum + b[bptr] <= k)
      sum += b[bptr++];
    ans = max(ans, aptr + bptr);
    sum -= a[--aptr];
  }
  bptr = 0, sum = 0;
  while (bptr < m and sum + b[bptr] <= k)
    sum += b[bptr++];
  ans = max(ans, bptr);
  cout << ans;
}


Error: WA

Fixed submission:

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

typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 5e5 + 1;
const ll INF = 1e10;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  for (ll &i : a)
    cin >> i;
  for (ll &i : b)
    cin >> i;
  int ans = 0, aptr = 0, bptr = 0;
  ll sum = 0;
  while (aptr < n and sum + a[aptr] <= k)
    sum += a[aptr++];
  ans = aptr;
  while (aptr) {
    while (bptr < m and sum + b[bptr] <= k)
      sum += b[bptr++];
    ans = max(ans, aptr + bptr);
    sum -= a[--aptr];
  }
  bptr = 0, sum = 0;
  while (sum + b[bptr] <= k)
    sum += b[bptr++];
  ans = max(ans, bptr);
  cout << ans;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

int A[2333333], B[2333333];
ll suma[233333], sumb[233333];

int main() {
  ios::sync_with_stdio(false);
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    suma[i] = suma[i - 1] + A[i];
  }
  for (int i = 1; i <= m; i++)
    cin >> B[i], sumb[i] = sumb[i - 1] + B[i];
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (suma[i] > k)
      break;
    int ls = upper_bound(sumb + 1, sumb + m + 1, k - suma[i]) - sumb;
    ans = max(ans, i + ls - 1);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int A[2333333], B[2333333];

int main() {
  ios::sync_with_stdio(false);
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= m; i++)
    cin >> B[i];
  int l = 0, r = 0;
  int ls = k;
  for (int i = 1; i <= n; i++) {
    if (ls - A[i] >= 0)
      l = i, ls -= A[i];
    else {
      break;
    }
  }
  for (int i = 1; i <= m; i++) {
    if (ls - B[i] >= 0)
      r = i, ls -= B[i];
    else {
      break;
    }
  }
  int ans = l + r;
  while (l >= 1 || r < m) {
    ls += A[l];
    l--;
    while (ls - B[r + 1] > 0 && r < m)
      ls -= B[r + 1], r++;
    ans = max(ans, l + r);
  }

  ls = k;
  l = 0, r = 0;
  for (int i = 1; i <= m; i++) {
    if (ls - B[i] >= 0)
      l = i, ls -= B[i];
    else {
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (ls - A[i] >= 0)
      r = i, ls -= A[i];
    else {
      break;
    }
  }
  ans = max(ans, l + r);
  while (l >= 1 || r < n) {
    ls += B[l];
    l--;
    while (ls - A[r + 1] > 0 && r < n)
      ls -= A[r + 1], r++;
    ans = max(ans, l + r);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long N, M, k;
int main() {
  // freopen("input.txt", "r", stdin);
  cin >> N >> M >> k;
  vector<long long> a(N + 3, 0LL), b(M + 3, 0LL);
  // a[0] = b[0] = 0;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    if (i)
      a[i] = a[i] + a[i - 1];
    // a[i] += a[i-1];
  }
  for (int j = 0; j < M; ++j) {
    cin >> b[j];
    if (j)
      b[j] += b[j - 1];
  }

  long long ans = 0, j = M - 1;

  for (int i = 0; i < N; ++i) {
    if (a[i] > k)
      break;
    while (j >= 0 && b[j] + a[i] > k)
      --j;
    // if(j>=0)
    ans = max(i + j + 2, ans);
  }

  j = N - 1;
  for (int i = 0; i < M; ++i) {
    if (b[i] > k)
      break;
    while (j >= 0 && a[j] + b[i] > k)
      --j;
    // if(j >= 0)
    ans = max(i + j + 2, ans);
  }

  // /for (int i = 1; i )
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long N, M, k;
int main() {
  // freopen("input.txt", "r", stdin);
  cin >> N >> M >> k;
  vector<long long> a(N + 3, 0), b(M + 3, 0);
  for (int i = 1; i <= N; ++i) {
    cin >> a[i];
  }
  for (int j = 1; j <= M; ++j) {
    cin >> b[j];
  }

  long long ans = 0;
  for (int i = 2; i <= N; ++i) {
    a[i] += a[i - 1];
  }

  for (int j = 2; j <= M; ++j) {
    b[j] += b[j - 1];
  }

  long long l = 0, i = 0, h = N;
  while (l <= h) {
    long long m = (l + h) / 2;
    if (a[m] > k)
      h = m - 1;
    else {
      long long remaining = k - a[m];
      ans = max(ans, m);
      i = 1;
      while (i <= M) {
        if (b[i] > remaining)
          break;
        ans = max(ans, m + i);
      }
      if (i <= M && b[i] <= remaining)
        ans = max(ans, m + i);
      l = m + 1;
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  ll ans = 0;
  rep(i, n) {
    cin >> a[i];
    if (i != 0)
      a[i] += a[i - 1];
    if (a[i] <= k)
      ans = max(ans, i + 1);
  }
  rep(i, m) {
    cin >> b[i];
    if (i != 0)
      b[i] += b[i - 1];
    if (b[i] <= k)
      ans = max(ans, i + 1);
  }
  ll cnt = 0;

  rep(i, n) {
    ll tmp = a[i];
    ll left = -1;
    ll right = m;
    if (tmp + b[m - 1] <= k) {
      ans = max(ans, i + m + 1);
      continue;
    }
    if (tmp + b[0] > k) {
      continue;
    }
    while (right - left > 1) {
      ll mid = (right + left) / 2;
      if (tmp + b[mid] > k) {
        right = mid;
        continue;
      } else {
        left = mid;
        continue;
      }
    }

    ans = max(ans, i + right + 1);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  ll ans = 0;
  rep(i, n) {
    cin >> a[i];
    if (i != 0)
      a[i] += a[i - 1];
    if (a[i] <= k)
      ans = max(ans, i + 1);
  }
  rep(i, m) {
    cin >> b[i];
    if (i != 0)
      b[i] += b[i - 1];
    if (b[i] <= k)
      ans = max(ans, i + 1);
  }
  ll cnt = 0;

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

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

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  long long A[N], B[M], a[N + 1], b[M + 1];
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  a[0] = 0;
  b[0] = 0;
  for (int i = 1; i <= N; i++)
    a[i] = a[i - 1] + A[i - 1];

  for (int j = 1; j <= M; j++)
    b[j] = b[j - 1] + B[j - 1];

  int ans = 0;
  int j = M;
  rep(i, N + 1) {
    if (K - a[i] < 0)
      break;

    while (b[j] > K - a[i] && j >= 0) {
      j -= 1;
    }
    ans = max(ans, i + j); // ansよりi+jが大きかったら更新
    if (i == N)
      break;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  long long A[N], B[M], a[N + 1], b[M + 1];
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  a[0] = 0;
  b[0] = 0;
  for (int i = 1; i <= N; i++)
    a[i] = a[i - 1] + A[i - 1];

  for (int j = 1; j <= M; j++)
    b[j] = b[j - 1] + B[j - 1];

  int ans = 0;

  rep(i, N + 1) {
    if (K - a[i] < 0)
      break;
    int j = M;
    while (b[j] > K - a[i] && j >= 0) {
      j -= 1;
    }
    ans = max(ans, i + j); // ansよりi+jが大きかったら更新
    if (i == N)
      break;
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define INF = 1001001001
#define pi = acos(-1)
using namespace std;
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);
  ll ans = 0;

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

  ll d = m;
  for (int l = 0; l <= n; ++l) {
    if (k < a[l])
      break;
    while (b[d] > k - a[l])
      d--;
    ans = max(ans, l + d);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
#define INF = 1001001001
#define pi = acos(-1)
using namespace std;
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);
  ll ans = 0;

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

  for (int l = 0; l <= n; ++l) {
    ll d = m;
    if (k < a[l])
      break;
    while (b[d] > k - a[l])
      d--;
    ans = max(ans, l + d);
  }

  cout << ans << endl;

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

// c++ テンプレ
#include <bits/stdc++.h>
using namespace std;

typedef long long llint;
typedef long double ld;

#define inf 1e18
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

vector<llint> a;
vector<llint> b;
int n, m;
llint k;
void solve() {
  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<llint> asum(n + 1, 0);
  vector<llint> bsum(m + 1, 0);
  for (int i = 1; i <= n; i++)
    asum[i] = asum[i - 1] + a[i - 1];
  for (int i = 1; i <= m; i++)
    bsum[i] = bsum[i - 1] + b[i - 1];
  // 2分探索
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (asum[i] > k)
      break;
    int j = upper_bound(bsum.begin(), bsum.end(), k - asum[i]) - bsum.begin();
    ans = max(ans, i + j - 1);
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

// c++ テンプレ
#include <bits/stdc++.h>
using namespace std;

typedef long long llint;
typedef long double ld;

#define inf 1e18
#define mod 1000000007
priority_queue<llint, vector<llint>, greater<llint>> que;
priority_queue<llint> Que;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

vector<llint> a;
vector<llint> b;
llint n, m, k;

llint calc(llint now, llint v, llint ai, llint bi) {
  if (v > k)
    return now - 1;
  if (v == k)
    return now;
  llint res1 = 0, res2 = 0;
  if (ai < n)
    res1 = calc(now + 1, v + a[ai], ai + 1, bi);
  if (bi < m)
    res2 = calc(now + 1, v + b[bi], ai, bi + 1);
  return max(res1, res2);
}

void solve() {
  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];
  llint x = calc(0, 0, 0, 0);
  cout << x << endl;
}

int main() {
  solve();
  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++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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, M) { B[i + 1] += B[i]; }

  int ans = 0;
  ll sum = 0;
  int j = M;
  rep(i, N + 1) {
    sum += A[i];
    if (sum > K) {
      break;
    }
    while (true) {
      if (sum + B[j] <= K) {
        break;
      }
      if (j == 0) {
        break;
      }
      j--;
    }
    ans = max(ans, i + j);
  }

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

Error: TLE

Fixed submission:

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

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, M) { B[i + 1] += B[i]; }

  int ans = 0;
  ll sum = 0;
  rep(i, N + 1) {
    int j = M;
    sum += A[i];
    if (sum > K) {
      break;
    }
    while (true) {
      if (sum + B[j] <= K) {
        break;
      }
      if (j == 0) {
        break;
      }
      j--;
    }
    ans = max(ans, i + j);
  }

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