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

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

using namespace std;
#define ll long long
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  ll time = 0, num = 0;
  if (k < a[0]) {
    for (int i = 0; i < n; i++) {
      if (time + b[i] > k) {
        break;
      } else {
        num++;
        time += b[i];
      }
    }
    cout << num << endl;
    return 0;
  }
  ///////////////////////////
  ll a_ind = 0;
  for (ll i = 0; i < n; i++) {
    if (time + a[i] > k) {
      break;
    } else {
      num++;
      a_ind++;
      time += a[i];
    }
  }
  ll b_ind = 0;
  for (ll i = 0; i < m; i++) {
    if (time + b[i] > k) {
      break;
    } else {
      num++;
      b_ind++;
      time += b[i];
    }
  }
  ll max_num = num;
  while (a_ind != 0) {
    time -= a[a_ind - 1];
    a_ind--;
    num--;
    while (true) {
      if (b_ind == b.size() || time + b[b_ind] > k) {
        break;
      } else {
        time += b[b_ind];
        num++;
        b_ind++;
      }
    }
    if (max_num < num) {
      max_num = num;
    }
  }
  cout << max_num << endl;
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;
#define ll long long
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  ll time = 0, num = 0;
  if (k < a[0]) {
    for (int i = 0; i < n; i++) {
      if (time + b[i] > k) {
        break;
      } else {
        num++;
        time += b[i];
      }
    }
    cout << num << endl;
    return 0;
  }
  ///////////////////////////
  ll a_ind = 0;
  for (int i = 0; i < n; i++) {
    if (time + a[i] > k) {
      break;
    } else {
      num++;
      a_ind++;
      time += a[i];
    }
  }
  ll b_ind = 0;
  for (int i = 0; i < m; i++) {
    if (time + b[i] > k) {
      break;
    } else {
      num++;
      b_ind++;
      time += b[i];
    }
  }
  ll max_num = num;
  while (a_ind != 0) {
    time -= a[a_ind - 1];
    a_ind--;
    num--;
    while (true) {
      if (time + b[b_ind] > k) {
        break;
      } else {
        time += b[b_ind];
        num++;
        b_ind++;
      }
    }
    if (max_num < num) {
      max_num = num;
    }
  }
  cout << max_num << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int a[N];
int b[N];
char s[N];
char t[N];
ll pre[N];
int cmp1(int a, int b) { return b < a; }
multiset<int> ss;
map<int, int> mp;
int main() {

  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];
  pre[0] = 0;
  a[0] = 0;
  for (int i = 1; i <= m; i++)
    pre[i] = pre[i - 1] + b[i];
  ll sum = 0;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    sum += a[i];
    if (sum > k)
      break;
    ll s = k - sum;
    int pos = upper_bound(pre + 1, pre + 1 + m, s) - pre - 1;
    // cout<<pos<<endl;
    ans = max(ans, i + pos);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll a[N];
ll b[N];
char s[N];
char t[N];
ll pre[N];
int cmp1(int a, int b) { return b < a; }
multiset<int> ss;
map<int, int> mp;
int main() {

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

  for (int i = 1; i < m; i++)
    pre[i] = pre[i - 1] + b[i];
  // for(int i=0;i<m;i++) cout<<pre[i]<<" ";
  ll sum = 0;
  int ans = 0;
  int num = m - 1;
  for (int i = 0; i < n; i++) {
    sum += a[i];
    while (sum + pre[num] > k) {
      num--;
    }
    ans = max(ans, i + 1 + num + 1);
  }

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

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, M, K, i, j;
  cin >> N >> M >> K;
  vector<int> A(N + 1), B(M + 1);
  for (i = 0; i < N; i++)
    cin >> A[i];
  for (j = 0; j < M; j++)
    cin >> B[j];
  int k = K;
  for (i = 0; i < N && A[i] <= k; i++) {
    k -= A[i];
  }
  for (j = 0; j < M && B[j] <= k; j++) {
    k -= B[j];
  }
  int I = i, ANS = i + j, ans = i + j;
  for (i = I - 1; i >= 0; i--) {
    ans--;
    k += A[i];
    for (; j < M && B[j] <= k; j++) {
      ans++;
      k -= B[j];
    }
    ANS = max(ANS, ans);
  }
  cout << ANS << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, M, K, i, j, tmp;
  cin >> N >> M >> K;
  vector<int> A(N + 1), B(M + 1);
  A[0] = 0;
  B[0] = 0;
  for (i = 1; i <= N; i++) {
    cin >> tmp;
    A[i] = A[i - 1] + tmp;
  }
  for (j = 1; j <= M; j++) {
    cin >> tmp;
    B[j] = B[j - 1] + tmp;
  }
  int ans = 0;
  for (i = 0; i <= N; i++) {
    if (K < A[i])
      break;
    for (j = 0; j <= M; j++) {
      if (K - A[i] < B[j])
        break;
      ans = max(ans, i + j);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define re register
#define int long long
using namespace std;

inline int read() {
  re int ans = 0;
  re bool f = 1;
  re char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ans = (ans << 3) + (ans << 1) + (ch ^ 48);
    ch = getchar();
  }
  return f ? ans : ~(ans - 1);
}
int a[204800], b[204800];

signed main(void) {
  int n, m, k, ans = 0;
  n = read(), m = read(), k = read();
  for (re int i = 1; i <= n; ++i)
    a[i] = a[i - 1] + read();
  for (re int i = 1; i <= m; ++i)
    b[i] = b[i - 1] + read();
  int j = m;
  for (re int i = 0; i <= n && a[i] <= k; ++i) {
    while (j >= 0 && a[i] + b[j] > k)
      j--;
    ans = max(ans, i + j);
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define re register
#define int long long
using namespace std;

inline int read() {
  re int ans = 0;
  re bool f = 1;
  re char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ans = (ans << 3) + (ans << 1) + (ch ^ 48);
    ch = getchar();
  }
  return f ? ans : ~(ans - 1);
}
int a[204800], b[204800];

signed main(void) {
  int n, m, k, ans = 0;
  n = read(), m = read(), k = read();
  for (re int i = 1; i <= n; ++i)
    a[i] = a[i - 1] + read();
  for (re int i = 1; i <= m; ++i)
    b[i] = b[i - 1] + read();
  for (re int i = 1; i <= n; ++i) {
    if (a[i] > k)
      n = i - 1;
  }
  for (re int i = 1; i <= m; ++i) {
    if (b[i] > k)
      m = i - 1;
  }
  for (re int i = 0; i <= n; ++i) {
    if (a[i] > k)
      break;
    for (re int j = 0; j <= m; ++j) {
      if (b[j] > k)
        break;
      if (i + j <= ans)
        continue;
      if (a[i] + b[j] <= k)
        ans = max(ans, i + j);
      else
        break;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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


Error: WA

Fixed submission:

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

ll n, m, k, ans;

int main() {
  cin >> n >> m >> k;
  ll a[n + 1], b[n + 1];
  a[0] = b[0] = 0;

  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (ll i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  for (ll i = 0; i < n; i++)
    if (k >= a[i])
      ans = max(ans, upper_bound(b + 1, b + 1 + m, k - a[i]) - b - 1 + i);

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

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

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> a(N), b(M);
  rep(i, N) cin >> a[i];
  rep(i, M) cin >> b[i];
  vector<ll> suma(N + 1, 0), sumb(M + 1, 0);
  for (int i = 1; i < N + 1; i++)
    suma[i] = suma[i - 1] + a[i - 1];
  for (int i = 1; i < M + 1; i++)
    sumb[i] = sumb[i - 1] + b[i - 1];
  int ans = 0;
  // a基準
  for (int i = 0; i < N + 1; i++) {
    int mx = 0;
    if (suma[i] <= K)
      mx = i;
    else
      break;
    int key = K - suma[i];
    mx += (upper_bound(sumb.begin(), sumb.end(), key) - sumb.begin() - 1);
    ans = max(ans, mx);
  }
  // b基準
  for (int i = 0; i < M + 1; i++) {
    int mx = 0;
    if (sumb[i] <= K)
      mx = i;
    else
      break;
    int key = K - sumb[i];
    mx += (upper_bound(suma.begin(), suma.end(), key) - suma.begin() - 1);
    ans = max(ans, mx);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  queue<int> a, b;
  rep(i, N) {
    int A;
    cin >> A;
    a.push(A);
  }
  rep(i, M) {
    int B;
    cin >> B;
    b.push(B);
  }
  int cnt = 0;
  ll sum = 0;
  rep(i, N + M) {
    if (a.front() > b.front() || a.empty()) {
      sum += b.front();
      b.pop();
    } else if (a.front() <= b.front() || b.empty()) {
      sum += a.front();
      a.pop();
    }
    cnt++;
    if (sum > K) {
      cnt--;
      break;
    }
  }
  cout << cnt << endl;
  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 ll = long long;
using namespace std;
int main() {
  ll n, m, k;
  ll c, d = 0;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) {
    cin >> c;
    d += c;
    a[i] = d;
  }
  d = 0;
  rep(i, m) {
    cin >> c;
    d += c;
    b[i] = d;
  }
  int ans = upper_bound(b.begin(), b.end(), k) - b.begin();
  rep(i, n) {
    if (a[i] <= k) {
      int ho = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin() + i + 1;
      ans = max(ans, ho);
    } else {
      break;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
  int time = 0;
  int N, M, K, max = 0;
  int A[200002], B[200002], T = 0;
  scanf("%d%d%d", &N, &M, &K);
  A[0] = 0;
  A[N + 1] = K + 1;
  for (int i = 1; i <= N; i++) {
    scanf("%d", &time);
    A[i] = A[i - 1] + time;
  }
  B[0] = 0;
  B[M + 1] = K + 1;
  for (int i = 1; i <= M; i++) {
    scanf("%d", &B[i]);
  }
  int i = 0, j = 0;
  while ((time = A[i]) <= K) {
    while ((time += B[j]) <= K) {
      N = i + j;
      if (N > max) {
        max = N;
      }
      j++;
    }
    i++;
    j = 0;
  }
  printf("%d", max);
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long N, M, K;
  cin >> N >> M >> K;
  vector<long> A(N + 1, 0);
  vector<long> B(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    long a;
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i <= M; i++) {
    long b;
    cin >> b;
    B[i] = B[i - 1] + b;
  }
  long jbest = M;
  while (B[jbest] > K)
    jbest--;
  long res = jbest;
  for (int i = 1; i <= N; i++) {
    if (A[i] > K)
      break;
    while (A[i] + B[jbest] > K)
      jbest--;
    res = max(res, i + jbest);
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long N, M, K;
  cin >> N >> M >> K;
  vector<long> A(N + 1, 0);
  vector<long> B(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    long a;
    cin >> a;
    A[i] = A[i - 1] + a;
  }
  for (int i = 1; i <= M; i++) {
    long b;
    cin >> b;
    B[i] = B[i - 1] + b;
  }
  long res = 0;
  for (int i = 0; i <= N; i++) {
    if (A[i] > K)
      break;
    long j = M;
    while (A[i] + B[j] > K)
      j--;
    res = max(res, i + j);
  }
  cout << res << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <bits/stdint-intn.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {
  int64_t n, m, k;
  std::cin >> n >> m >> k;
  std::vector<int64_t> a(n), b(m);
  rep(i, n) { std::cin >> a[i]; }
  rep(i, m) { std::cin >> b[i]; }

  int64_t t = 0;
  rep(i, m) { t += b[i]; }

  int j = m, ans = 0;

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

  std::cout << ans << std::endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <bits/stdint-intn.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {
  int64_t N, M, K;
  std::stack<int64_t> A, B;
  std::cin >> N >> M >> K;
  rep(i, N) {
    int64_t x;
    std::cin >> x;
    A.push(x);
  }
  rep(j, M) {
    int64_t x;
    std::cin >> x;
    B.push(x);
  }

  int64_t total = 0, ans = 0;

  if (min(A.top(), B.top()) <= K) {
    while (total <= K) {
      if (A.empty() && B.empty()) {
        break;
      } else if (A.empty() || B.top() < A.top()) {
        total += B.top();
        B.pop();
      } else {
        total += A.top();
        A.pop();
      }
      ans++;
    }
  }

  std::cout << ans << std::endl;

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

#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " " << x << "\\n";
#define lli long long int
int32_t main() {
  lli n, m, i, k, j, c = 0, v1 = 0, v2 = 0;
  cin >> n >> m >> k;
  lli a[n + 5];
  lli b[m + 5];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (i != 1)
      a[i] += a[i - 1];
    if (k >= a[i])
      v1 = i;
  }
  for (i = 1; i <= m; i++) {
    cin >> b[i];
    if (i != 1)
      b[i] += b[i - 1];
    if (k >= b[i])
      v2 = i;
  }
  i = 1;
  j = m;
  c = max(v1, v2);
  while (i <= n && j >= 1) {
    if (a[i] + b[j] <= k) {
      c = max(c, i + j);
      i++;
    } else
      j--;
  }
  cout << c << "\\n";
  return 0;
} // main


Error: WA

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int x = n + m;
  int arr[x];
  for (int i = 0; i < x; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + x);

  int sum = 0;
  int Count = 0;

  for (int j = 0; j < x; j++) {
    if (sum <= k) {
      sum += arr[k];
      Count++;
    } else
      break;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i < (n); i++)
#define fore(i, a) for (auto &i : a)
#define debug(a, b) cout << "debug : " << a << " , " << b << endl
using ll = long long;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
const ll INF = 1LL << 60;
const ll mod = 1E9 + 7;

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];
  vector<ll> Sa(n + 1, 0), Sb(m + 1, 0);
  rep(i, n) Sa[i + 1] = Sa[i] + a[i];
  rep(i, m) Sb[i + 1] = Sb[i] + b[i];
  ll b_max = 0;
  int c = 0;
  while (1) {
    if (b_max + 1 > m)
      break;
    if (Sb[b_max + 1] > k)
      break;
    b_max++;
  }
  // cout << b_max;
  ll ans = b_max, sum = Sb[b_max], cnt = b_max, j = 0;
  for (int i = b_max; i >= 0; i--) {
    while (1) {
      if (sum + a[j] > k || j == n)
        break;
      sum += a[j];
      j++;
      cnt++;
    }
    // debug(sum,cnt);
    ans = max(ans, cnt);
    sum -= b[i - 1];
    cnt--;
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i < (n); i++)
#define fore(i, a) for (auto &i : a)
#define debug(a, b) cout << "debug : " << a << " , " << b << endl
using ll = long long;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
const ll INF = 1LL << 60;
const ll mod = 1E9 + 7;

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];
  vector<ll> Sa(n + 1, 0), Sb(m + 1, 0);
  rep(i, n) Sa[i + 1] = Sa[i] + a[i];
  rep(i, m) Sb[i + 1] = Sb[i] + b[i];
  ll b_max;
  ll i = 0, sum = 0;
  while (1) {
    sum = Sb[i + 1];
    if (sum > k)
      break;
    i++;
  }
  b_max = i;
  ll ans = 0;
  for (i = b_max; i >= 0; i--) {
    ll count = i, S = Sb[i], j = 0;
    while (S + a[j] <= k) {
      S += a[j];
      count++;
      j++;
    }
    ans = max(ans, count);
  }
  cout << ans;

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

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;
typedef long long LL;
const int N = 200010;
LL a[N], s1[N], b[N], s2[N];
int n, m, k;

int find(int x) {
  int res = 0;
  int l = 0, r = m;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (s2[mid] <= x)
      l = mid;
    else
      r = mid - 1;
  }
  return l;
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), s1[i] = a[i] + s1[i - 1];
  for (int i = 1; i <= m; i++)
    scanf("%lld", &b[i]), s2[i] = b[i] + s2[i - 1];

  int res = 0;
  for (int i = 0; i <= n && s1[i] <= k; i++)
    res = max(res, i + find(k - s1[i]));
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;
typedef long long LL;
const int N = 200010;
LL a[N], b[N];
int n, m, k;

int find(int x) {
  int res = 0;
  int l = 0, r = n;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (a[mid] <= x)
      l = mid;
    else
      r = mid - 1;
  }
  res += l;
  int y = k - x + (x - a[l]);
  l = 0, r = m;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (b[mid] <= y)
      l = mid;
    else
      r = mid - 1;
  }
  res += l;
  return res;
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), a[i] += a[i - 1];
  for (int i = 1; i <= m; i++)
    scanf("%lld", &b[i]), b[i] += b[i - 1];

  int res = 0;
  for (int i = 0; i <= k; i++)
    res = max(res, find(i));
  cout << res << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  // input
  int64_t N, M, K;
  cin >> N >> M >> K;

  vector<int64_t> A(N), B(M);
  for (int64_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int64_t i = 0; i < M; i++) {
    cin >> B[i];
  }

  // calculation
  vector<int64_t> sum_A(N + 1), sum_B(M + 1);
  int64_t temp_sum = 0;
  sum_A[0] = 0;
  for (int64_t i = 0; i < N; i++) {
    temp_sum += A[i];
    sum_A[i + 1] = temp_sum;
  }
  temp_sum = 0;
  sum_B[0] = 0;
  for (int64_t i = 0; i < M; i++) {
    temp_sum += B[i];
    sum_B[i + 1] = temp_sum;
  }

  int64_t output = 0;
  for (int64_t i = 0; i <= N; i++) {
    int64_t start_j = output - i;
    if (start_j < 0) {
      start_j = 0;
    }
    for (int64_t j = start_j; j <= M; j++) {
      if (sum_A[i] + sum_B[j] > K) {
        break;
      }
      if (output < i + j) {
        output = i + j;
      }
    }
  }

  // output
  cout << output << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  // input
  int64_t N, M, K;
  cin >> N >> M >> K;

  vector<int64_t> A(N), B(M);
  for (int64_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int64_t i = 0; i < M; i++) {
    cin >> B[i];
  }

  // calculation
  vector<int64_t> sum_A(N + 1), sum_B(M + 1);
  int64_t temp_sum = 0;
  sum_A[0] = 0;
  for (int64_t i = 0; i < N; i++) {
    temp_sum += A[i];
    sum_A[i + 1] = temp_sum;
  }
  temp_sum = 0;
  sum_B[0] = 0;
  for (int64_t i = 0; i < M; i++) {
    temp_sum += B[i];
    sum_B[i + 1] = temp_sum;
  }

  int64_t output = 0;
  for (int64_t i = 0; i <= N; i++) {
    for (int64_t j = 0; j <= M; j++) {
      if (sum_A[i] + sum_B[j] > K) {
        break;
      }
      if (output < i + j) {
        output = i + j;
      }
    }
  }

  // output
  cout << output << endl;

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

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

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

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

  vector<ll> a(n);
  vector<ll> b(m);
  for (long long i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (long long i = 0; i < m; i++) {
    cin >> b.at(i);
  }

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


Error: 0

Fixed submission:

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

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

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

  vector<ll> a(n);
  vector<ll> b(m);
  for (long long i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (long long i = 0; i < m; i++) {
    cin >> b.at(i);
  }

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