alexjercan/bugnet huggingface dataset:
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_s(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

#define MOD 1000000007

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

  vi a, b;
  rep(i, n) {
    ll _a;
    cin >> _a;
    a.push_back(_a);
  }

  rep(i, m) {
    ll _b;
    cin >> _b;
    b.push_back(_b);
  }

  int ans = 0;

  ll a_k = 0;
  int a_index = -1;

  ll b_k = 0;
  int b_index = -1;

  // b全部読む
  rep(i, m) { b_k += b[i]; }
  b_index = m - 1;

  rep(i, n + 1) {
    if (i > 0) {
      a_index++;
      a_k += a[a_index];
    }

    // bを減らす
    while (a_k + b_k > k && b_index >= 0) {
      b_k -= b[b_index];
      b_index--;
    }

    if (a_k + b_k <= k) {
      ans = max(ans, a_index + b_index + 2);
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_s(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

#define MOD 1000000007

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

  vi a, b;
  rep(i, n) {
    ll _a;
    cin >> _a;
    a.push_back(_a);
  }

  rep(i, m) {
    ll _b;
    cin >> _b;
    b.push_back(_b);
  }

  int ans = 0;

  int a_k = 0;
  int a_count = 0;
  int a_index = -1;
  while (a_k <= k) {
    int read_count = a_count;
    // bを読む
    int b_k = 0;
    for (int b_index = 0; a_k + b_k + b[b_index] <= k && b_index < b.size();
         b_index++) {
      b_k += b[b_index];
      read_count++;
    }
    ans = max(ans, read_count);

    // 次のaを読む
    if (a_index + 1 < a.size()) {
      a_index++;
      a_k += a[a_index];
      a_count++;
    } else {
      break;
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int main() {
  cout << fixed << setprecision(10);
  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];
  vector<ll> suma(n, 0), sumb(m, 0);
  suma[0] = a[0];
  sumb[0] = b[0];
  for (int i = 1; i < n; i++)
    suma[i] += suma[i - 1] + a[i];
  for (int i = 1; i < m; i++)
    sumb[i] += sumb[i - 1] + b[i];
  ll ans = 0;
  rep(i, n) {
    ll now = i + 1;
    if (suma[i] > k)
      break;
    ll num = upper_bound(sumb.begin(), sumb.end(), k - suma[i]) - sumb.begin();
    ans = max(ans, num + now);
  }
  rep(i, m) {
    ll now = i + 1;
    if (sumb[i] > k)
      break;
    ll num = upper_bound(suma.begin(), suma.end(), k - sumb[i]) - suma.begin();
    ans = max(ans, num + now);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int main() {
  cout << fixed << setprecision(10);
  ll n, m, k;
  cin >> n >> m >> k;
  ll now = 0;
  int cnt = 0;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  int itra = 0, itrb = 0;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  while ((itra < n || itrb < m) && now < k) {
    if (itra < n && itrb < m) {
      if (a[itra] < b[itrb]) {
        if (now + a[itra] <= k) {
          now += a[itra];
          itra++;
          cnt++;
        } else
          break;
      } else {
        if (now + b[itrb] <= k) {
          now += b[itrb];
          itrb++;
          cnt++;
        } else
          break;
      }
    } else if (itra < n) {
      if (now + a[itra] <= k) {
        now += a[itra];
        itra++;
        cnt++;
      } else
        break;
    } else {
      if (now + b[itrb] <= k) {
        now += a[itrb];
        itrb++;
        cnt++;
      } else
        break;
    }
  }
  cout << cnt << 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);
  A.at(0) = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> A.at(i);
  }

  vector<long long> cumsum_B(M + 1);
  cumsum_B.at(0) = 0;
  long long b;
  for (int j = 1; j < M + 1; j++) {
    cin >> b;
    cumsum_B.at(j) = cumsum_B.at(j - 1) + b;
  }

  int max_num = 0;
  int b_num = M;
  long long a_time;
  for (int i = 0; i < N + 1; i++) {
    a_time += A.at(i);
    if (a_time > K) {
      break;
    }
    while (cumsum_B.at(b_num) + a_time > K) {
      b_num--;
    }
    max_num = max(max_num, i + b_num);
  }
  cout << max_num << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N + 1);
  vector<int> B(M + 1);
  A.at(0) = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> A.at(i);
  }
  B.at(0) = 0;
  for (int j = 1; j < M + 1; j++) {
    cin >> B.at(j);
  }

  int max_num = 0;
  int a_time;
  int total_time;
  for (int i = 0; i < N + 1; i++) {
    a_time += A.at(i);
    if (a_time > K) {
      max_num = max(max_num, i - 1);
      break;
    }
    total_time = a_time;
    for (int j = 0; j < M + 1; j++) {
      total_time += B.at(j);
      if (total_time > K) {
        max_num = max(max_num, i + j - 1);
        break;
      } else if (j == M) {
        max_num = max(max_num, i + M);
      }
    }
  }
  cout << max_num << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  int ans = 0, j = m;
  ll tot = 0;
  for (int i = 0; i < m; i++)
    tot += b[i];

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

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

Error: 0

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];

  int ans = 0, j = m;
  ll tot = 0;
  for (int i = 0; i < m; i++)
    tot += b[i];

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

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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mod 1000000007

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll arr[n], brr[m];

  for (ll i = 0; i < n; i++)
    cin >> arr[i];

  for (ll i = 0; i < m; i++)
    cin >> brr[i];

  for (ll i = 1; i < n; i++)
    arr[i] = arr[i] + arr[i - 1];

  for (ll i = 1; i < m; i++)
    brr[i] = brr[i] + brr[i - 1];

  if (arr[n - 1] + brr[m - 1] <= k) {
    cout << m + n;
    return 0;
  }
  if (arr[0] > k && brr[0] > k) {
    cout << 0;
    return 0;
  }

  ll minn = -1;

  for (ll i = 0; i < n; i++) {
    if (arr[i] <= k) {
      minn = i;
    }
  }

  ll pp2 = upper_bound(arr, arr + n, k) - arr;
  ll pp = upper_bound(brr, brr + m, k) - brr;

  if (minn == -1) {
    ll ans = upper_bound(brr, brr + m, k) - brr - 1;
    cout << ans + 1;
    return 0;
  }

  ll ans = minn + 1;

  while (minn != -1) {
    ll d = arr[minn];
    ll temp = k - d;

    if (temp < brr[0]) {
      minn--;
      continue;
    }
    if (temp >= brr[m - 1]) {
      ans = max(ans, minn + m + 1);
      minn--;
      continue;
    }
    ll p = upper_bound(brr, brr + m, temp) - brr - 1;

    ans = max(ans, minn + p + 2);
    minn--;
  }

  cout << max(ans, max(pp, pp2));

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mod 1000000007

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll arr[n], brr[m];

  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  for (ll i = 0; i < m; i++)
    cin >> brr[i];

  for (ll i = 1; i < n; i++)
    arr[i] = arr[i] + arr[i - 1];
  for (ll i = 1; i < m; i++)
    brr[i] = brr[i] + brr[i - 1];

  if (arr[n - 1] + brr[m - 1] <= k) {
    cout << m + n;
    return 0;
  }
  if (arr[0] > k && brr[0] > k) {
    cout << 0;
    return 0;
  }

  ll minn = -1;

  for (ll i = 0; i < n; i++)
    if (arr[i] <= k) {
      minn = i;
    }

  if (minn == -1)
    minn = 0;

  if (minn == 0) {
    ll ans = upper_bound(brr, brr + m, k) - brr - 1;
    cout << ans + 1;
    return 0;
  }

  ll ans = minn;

  while (minn != -1) {
    ll d = arr[minn];
    ll temp = k - d;

    if (temp < brr[0]) {
      minn--;
      continue;
    }
    if (temp > brr[m - 1]) {
      minn--;
      ans = max(ans, minn + m + 2);
    }
    ll p = upper_bound(brr, brr + n, temp) - brr - 1;

    if (p >= 0)
      ans = max(ans, minn + p + 2);
    minn--;
  }

  cout << ans;

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


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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int find(int b[], int m, int x) {
  int start = 0, end = m - 1, res = -1;

  while (start <= end) {
    int mid = start + ((end - start) / 2);
    if (b[mid] == x) {
      return mid;
    } else if (b[mid] < x) {
      res = mid;
      start = mid + 1;
    } else {
      end = mid - 1;
    }
  }

  return res;
}
int32_t main() {
  //  c_p_c();
  int n, m, k;
  cin >> n >> m >> k;

  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];
  }

  int prea[n + 1], preb[m + 1];
  prea[0] = 0, preb[0] = 0;
  for (int i = 1; i <= n; i++) {
    prea[i] = prea[i - 1] + a[i - 1];
  }

  for (int i = 1; i <= m; i++) {
    preb[i] = preb[i - 1] + b[i - 1];
  }

  int ans = 0;
  for (int i = 0; i < n + 1; i++) {
    int rem = k - prea[i];
    if (rem < 0) {
      break;
    }
    int num = find(preb, m + 1, rem);
    ans = max(ans, (num + i));
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:


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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
const int MAXN = 2e5 + 2;
int a[MAXN], b[MAXN], n, m, k;

int solve(int idx1, int idx2, int rem) {
  if (rem < 0) {
    return -1;
  }
  if (rem == 0) {
    return 0;
  }
  if (idx1 == n && idx2 == m) {
    return 0;
  }
  if (idx1 == n) {
    return 1 + solve(idx1, idx2 + 1, rem - b[idx2]);
  }
  if (idx2 == m) {
    return 1 + solve(idx1 + 1, idx2, rem - a[idx1]);
  }
  return max(1 + solve(idx1 + 1, idx2, rem - a[idx1]),
             1 + solve(idx1, idx2 + 1, rem - b[idx2]));
}
int32_t main() {
  // c_p_c();

  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }

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

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

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

  ll ans = 0;
  ll b_tmp = m;
  for (ll i = 0; i < n + 1; i++) {
    if (A[i] > k)
      break;
    while (A[i] + B[b_tmp] > k)
      b_tmp--;
    ans = max(ans, i + b_tmp);
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  ll n_end = n, m_end = m;
  ll sum_a = 0, sum_b = 0;
  int flag = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum_a += a[i];
    if (sum_a > k && flag == 0) {
      n_end = i;
      flag = 1;
    }
  }
  flag = 0;
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
    sum_b += b[i];
    if (sum_b > k && flag == 0) {
      m_end = i;
      flag = 1;
    }
  }
  ll end = max(n_end, m_end);
  if (end == 0) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = end;
  cout << ans << endl;
  sum_a = 0, sum_b = 0;
  ll dif = 0;
  for (ll i = 0; i < end; i++) {
    if (n_end > m_end) {
      sum_a += a[n_end - i - 1];
      sum_b += b[i + dif];
      while (sum_a > sum_b + b[i + dif + 1]) {
        sum_b += b[i + dif + 1];
        dif++;
        ans++;
      }
    } else {
      sum_a += a[i + dif];
      sum_b += b[m_end - i - 1];
      while (sum_b > sum_a + a[i + dif + 1]) {
        sum_a += a[i + dif + 1];
        dif++;
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>
using namespace std;
#define INF 1000000010

int main(void) {
  long N, M, K;
  cin >> N >> M >> K;
  long *A = (long *)malloc(sizeof(long) * (N + 1));
  long *B = (long *)malloc(sizeof(long) * (M + 1));
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  A[N] = INF;
  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }
  B[M] = INF;
  long a = 0;
  long t = 0;
  for (long i = 0; t + A[i] <= K; i++) {
    t += A[i];
    a++;
  }
  long b = 0;
  long retval = a + b;
  while (a >= 0 and b >= 0) {
    if (t + B[b] <= K) {
      t += B[b];
      b++;
    } else {
      a--;
      t -= A[a];
    }
    if (a >= 0) {
      retval = max(retval, a + b);
    }
  }

  cout << retval << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  long N, M, K;
  cin >> N >> M >> K;
  long *A = (long *)malloc(sizeof(long) * N);
  long *B = (long *)malloc(sizeof(long) * M);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }
  long retval = 0;
  for (long a = 0; a <= N; a++) {
    long t = 0;
    for (int i = 0; i < a; i++) {
      t += A[i];
    }
    if (t > K) {
      break;
    }
    long b = 0;
    for (; t + B[b] <= K and b < M; b++) {
      t += B[b];
    }
    retval = max(retval, a + b);
  }
  cout << retval << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, m;
  long long k;

  cin >> n >> m >> k;

  vector<long long> a(n);

  vector<long long> b(m);

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < m; i++)
    cin >> b[i];

  // aとbの先頭を比較して、小さいほうを引いていく。
  // Kから引いて行って、残っている最小よりもKが小さくなったらカウント終了
  // 上の考え方はよくなかった
  // abの組み合わせを求める問題と読み換えるのが正解

  int count = 0;

  vector<long long> suma(n + 1);
  vector<long long> sumb(m + 1);

  suma[0] = 0;
  sumb[0] = 0;

  for (int i = 1; i <= n; i++)
    suma[i] = a[i - 1] + suma[i - 1];
  for (int i = 1; i <= m; i++)
    sumb[i] = b[i - 1] + sumb[i - 1];

  int i = 0;
  int begin = m;
  int count1 = 0;
  while (i <= n) {

    for (int j = begin; j >= 0; j--) {
      if (k >= suma[i] + sumb[j]) {
        count1 = i + j;
        begin = j;
        break;
      }
    }

    if (count < count1)
      count = count1;
    i++;
  }

  /*
  for (int i = 0; i < n + 1; i++) {

      int count1 = 0;

      for (int j = m; j >= 0;j--) {
          if (k>=suma[i]+sumb[j]) {
              count1 = i + j;
              break;
          }
      }
      if (count<count1) count = count1;

  }
  */

  std::cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, m;
  long long k;

  cin >> n >> m >> k;

  vector<long long> a(n);

  vector<long long> b(m);

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < m; i++)
    cin >> b[i];

  // aとbの先頭を比較して、小さいほうを引いていく。
  // Kから引いて行って、残っている最小よりもKが小さくなったらカウント終了
  // 上の考え方はよくなかった
  // abの組み合わせを求める問題と読み換えるのが正解

  int count = 0;

  vector<long long> suma(n + 1);
  vector<long long> sumb(m + 1);

  suma[0] = 0;
  sumb[0] = 0;

  for (int i = 1; i <= n; i++)
    suma[i] = a[i - 1] + suma[i - 1];
  for (int i = 1; i <= m; i++)
    sumb[i] = b[i - 1] + sumb[i - 1];

  for (int i = 0; i < n + 1; i++) {

    int count1 = 0;
    for (int j = m; j >= 0; j--) {
      if (k >= suma[i] + sumb[j]) {
        count1 = i + j;
        break;
      }
    }
    if (count < count1) {
      count = count1;
    }
  }

  std::cout << count << endl;

  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 < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n, m, k;
  cin >> n >> m >> k;
  vl sa(n + 1), sb(m + 1);
  rep(i, n) {
    int a;
    cin >> a;
    sa[i + 1] = sa[i] + a;
  }
  rep(i, m) {
    int b;
    cin >> b;
    sb[i + 1] = sb[i] + b;
  }

  int idx = m, ans = 0;

  rep(i, n + 1) {
    if (sa[i] > k)
      break;
    while (idx > 0 && sa[i] + sb[idx] > k)
      idx--;
    cmax(ans, i + idx);
  }

  cout << ans << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repa(i, a, n) for (int i = (a); i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepa(i, a, n) for (int i = n - 1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define vc vector

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

int main() {

  int n, m, k;
  cin >> n >> m >> k;
  vl sa(n + 1), sb(n + 1);
  rep(i, n) {
    int a;
    cin >> a;
    sa[i + 1] = sa[i] + a;
  }
  rep(i, m) {
    int b;
    cin >> b;
    sb[i + 1] = sb[i] + b;
  }

  int idx = m, ans = 0;

  rep(i, n + 1) {
    if (sa[i] > k)
      break;
    while (idx > 0 && sa[i] + sb[idx] > k)
      idx--;
    cmax(ans, i + idx);
  }

  cout << ans << "\\n";

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

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

int main() {
  int64_t N, M, K, a, b;
  cin >> N >> M >> K;
  vector<int64_t> A(N + 1);
  vector<int64_t> B(M + 1);
  for (int64_t i = 1; i <= N; i++) {
    cin >> a;
    A.at(i) = a + A.at(i - 1);
  }
  for (int64_t i = 1; i <= M; i++) {
    cin >> b;
    B.at(i) = b + B.at(i - 1);
  }

  int64_t C = 0, j = M;
  for (int i = 0; i <= N; i++) {
    if (A.at(i) > K) {
      break;
    }
    while (B.at(j) > K - A.at(i)) {
      // cout << i << " " << j << " " << C << endl;
      j--;
    }
    C = max(C, i + j);
  }
  cout << C << endl;
}

Error: TLE

Fixed submission:

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

int64_t sum(vector<int64_t> V, int64_t x) {
  if (x == 0) {
    return 0;
  }
  int64_t S = 0;
  for (int64_t i = 0; i < x; i++) {
    S += V.at(i);
  }
  return S;
}

int main() {
  int64_t N, M, K;
  cin >> N >> M >> K;
  int64_t C = 0, b = M;
  vector<int64_t> A(N);
  vector<int64_t> B(M);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int64_t i = 0; i < M; i++) {
    cin >> B.at(i);
  }

  int64_t T;
  int64_t SA = 0;
  int64_t SB = 0;
  for (int64_t i = 0; i <= N; i++) {
    SB = 0;
    if (i >= 1) {
      SA += A.at(i - 1);
    }
    T = K;
    T -= SA;
    if (T < 0) {
      break;
    }

    for (int64_t j = 0; j <= b; j++) {
      if (j >= 1) {
        SB += B.at(j - 1);
      }
      // cout << i << " " << j << " " << b << " " << SB << endl;
      if (T < SB) {
        b = j;
        C = max(C, i + j - 1);
        break;
      }

      else if (j == b) {
        C = max(C, i + j);
        break;
      }
    }
  }
  cout << C << endl;
}