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

/**
 *     author: nayakashutosh9
 *     college: National Institute of Technology, Rourkela
 *	   "Winners Never Quit and Quitters Never Win".
 **/
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\\n"
#define mod 1000000007
#define inf 1e18
const int N = 300005;

/*******************************************************************/

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // int t = 1, z = 1;
  // cin >> t;
  // cin.ignore();
  // while (t--) {

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

Error: -11

Fixed submission:

/**
 *     author: nayakashutosh9
 *     college: National Institute of Technology, Rourkela
 *	   "Winners Never Quit and Quitters Never Win".
 **/
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\\n"
#define mod 1000000007
#define inf 1e18
const int N = 300005;

/*******************************************************************/

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // int t = 1, z = 1;
  // cin >> t;
  // cin.ignore();
  // while (t--) {

  // }
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n + 1, 0), b(m + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  int ans = 0, j = m;
  for (int i = 0; i <= n; i++) {
    int g = a[i];
    while ((g + b[j]) > k)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const int mod = 1E9 + 7;
const int inf = 1 << 29;

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const int mod = 1E9 + 7;
const int inf = 1 << 29;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> sum_a(n + 1), sum_b(n + 1);
  sum_a[0] = 0;
  sum_b[0] = 0;
  int a, b;
  rep(i, n) {
    cin >> a;
    sum_a[i + 1] = sum_a[i] + a;
  };
  rep(i, m) {
    cin >> b;
    sum_b[i + 1] = sum_b[i] + b;
  };
  int ans = 0;
  rep(i, n + 1) {
    int tmp_t = sum_a[i];
    if (tmp_t > k)
      break;
    // cout << "table A: " << i << ", " << tmp_t << endl;
    rep(j, m + 1) {
      if (k - tmp_t < sum_b[j])
        break;
      // cout << "table B: " << j << ", " << tmp_t + sum_b[j] << endl;
      ans = max(ans, i + j);
    }
    // cout << "best: " << ans << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a) for (int(i) = 0; (i) < (a); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define m0(x) memset(x, 0, sizeof(x))
#define chmax(a, b)                                                            \\
  if (b > a)                                                                   \\
  a = b
#define chmin(a, b)                                                            \\
  if (b < a)                                                                   \\
  a = b
typedef long long ll;
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), ruia(n + 1, 0), ruib(m + 1, 0);
  rep(i, n) {
    cin >> a[i];
    ruia[i + 1] = ruia[i] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    ruib[i + 1] = ruib[i] + b[i];
  }

  rep(i, n + 1) {
    auto key = k - ruia[i];
    int l = 0, r = m, mid = (l + r) / 2;
    while (l < r - 1) {
      mid = (l + r) / 2;
      if (ruib[mid] > key) {
        r = mid;
      } else {
        l = mid;
      }
    }
    if (ruib[r] <= key) {
      chmax(ans, r + i);
    } else if (ruib[l] <= key)
      chmax(ans, l + i);
  }
  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int(i) = 0; (i) < (a); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define m0(x) memset(x, 0, sizeof(x))
#define chmax(a, b)                                                            \\
  if (b > a)                                                                   \\
  a = b
#define chmin(a, b)                                                            \\
  if (b < a)                                                                   \\
  a = b
typedef long long ll;
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), ruia(n), ruib(m);
  rep(i, n) {
    cin >> a[i];
    if (i > 0)
      ruia[i] = ruia[i - 1] + a[i];
    else
      ruia[i] = a[i];
  }
  rep(i, m) {
    cin >> b[i];
    if (i > 0)
      ruib[i] = ruib[i - 1] + b[i];
    else
      ruib[i] = b[i];
  }
  ll x = m - 1;
  rep(i, n) {

    while (1) {
      if (ruia[i] + ruib[x] <= k) {
        chmax(ans, i + 1 + x + 1);
        break;
      }
      x--;
      if (x == 0)
        break;
    }
  }
  cout << ans << endl;

  return 0;
}
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;
using P = pair<int, int>;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N);
  vector<ll> B(M);
  vector<ll> a;
  vector<ll> b;
  a.push_back(0);
  b.push_back(0);
  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }
  rep(i, N) { a.push_back(a[i] + A[i]); }
  rep(i, M) { b.push_back(b[i] + B[i]); }

  ll j = M;
  ll ans = 0;
  rep(i, N + 1) {
    if (a[i] > K)
      break;
    while (b[j] > K - a[i])
      j -= 1;
    ans = max(ans, i + j);
  }
  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;
using P = pair<int, int>;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N);
  vector<ll> B(M);
  vector<ll> C;
  ll all_time = 0;
  ll book_count = 0;

  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }
  while (all_time < K) {
    if ((A.size() == 0) && (B.size() == 0))
      break;
    if (A[0] > B[0]) {
      all_time += A[0];
      A.erase(A.begin());
      book_count++;
    } else if (A[0] < B[0]) {
      all_time += B[0];
      B.erase(B.begin());
      book_count++;
    } else if (A[0] == B[0]) {
      if (A[1] > B[1]) {
        all_time += A[0];
        A.erase(A.begin());
        book_count++;
      } else if (A[1] < B[1]) {
        all_time += B[0];
        B.erase(B.begin());
        book_count++;
      }
    }
  }
  cout << book_count << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000009;
const long long INF = 1LL << 60;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  vector<ll> A_S(N + 1, 0);
  for (int i = 0; i < N; ++i)
    A_S[i + 1] = A_S[i] + A[i];
  vector<ll> B(M);
  for (int i = 0; i < M; ++i)
    cin >> B[i];
  vector<ll> B_S(M + 1, 0);
  for (int i = 0; i < M; ++i)
    B_S[i + 1] = B_S[i] + B[i];
  ll res = 0;
  ll j = M;
  for (int i = 0; i < N + 1; ++i) {
    if (A_S[i] > K)
      break;
    while (B_S[j] > K - A_S[i])
      j -= 1;
    res = max(res, i + j);
  }
  cout << res << endl;
  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000009;
const long long INF = 1LL << 60;

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

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

long long A[200000], B[200000];

int main() {

  long long n, m, ms;
  cin >> n >> m >> ms;

  for (int A_i = 0; A_i < n; A_i++) {
    cin >> A[A_i];
  }
  for (int B_i = 0; B_i < m; B_i++) {
    cin >> B[B_i];
  }

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

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

  long long 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;

  return 0;
}

Error: 0

Fixed submission:

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

long long A[100002], B[100002];

int main() {

  long long n, m, ms;
  cin >> n >> m >> ms;

  for (int A_i = 0; A_i < n; A_i++) {
    cin >> A[A_i];
  }
  for (int B_i = 0; B_i < m; B_i++) {
    cin >> B[B_i];
  }

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

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

  long long 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;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);

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

  ll timeA = 0;
  int ansA = 0;
  int finalAns = 0;
  for (int i = 0; i <= n; i++) {
    if (timeA + a[i] <= k) {
      timeA += a[i];
      if (a[i] != 0)
        ansA++;
    } else
      break;
    ll time = timeA;
    int ans = ansA;
    int ok = 0;
    int ng = s.size();
    while (1 < abs(ok - ng)) {
      int c = (ng + ok) / 2;
      if (s[c] + time <= k)
        ok = c;
      else
        ng = c;
    }
    ans += ok;
    finalAns = max(finalAns, ans);
  }
  cout << finalAns << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  reverse(a.begin(), a.end());
  for (int i = 0; i < m; i++)
    cin >> b[i];
  reverse(b.begin(), b.end());
  vector<ll> s(m + 1);
  if (1 < b.size()) {
    for (int i = 1; i <= m; i++)
      s[i] = s[i - 1] + b[i - 1];
  }

  ll timeA = 0;
  int ansA = 0;
  int finalAns = 0;
  for (int i = 0; i < n; i++) {
    if (timeA + a[i] <= k) {
      timeA += a[i];
      ansA++;
    } else {
      finalAns = max(finalAns, ansA);
      break;
    }
    ll time = timeA;
    int ans = ansA;
    int l = -1;
    int r = n + 1;
    while (1 < r - l) {
      int m = (r + l) / 2;
      if (s[m] + time < k)
        l = m;
      else
        r = m;
    }
    ans += r;
    finalAns = max(finalAns, ans);
  }
  cout << finalAns << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define lg long long int
#define loop(i, s, e) for (lg i = s; i <= e; i++)
#define iloop(i, s, e) for (lg i = e; i >= s; i--)
#define pb push_back
#define mp make_pair
#define mod 1000000007

using namespace std;

lg min(lg a, lg b) { return a < b ? a : b; }
lg max(lg a, lg b) { return a > b ? a : b; }
lg lcm(lg a, lg b) { return (a * b) / __gcd(a, b); }
lg n, m, k, ans;
lg bin_search(vector<lg> &a, lg start, lg end, lg num) {
  if (a[end] <= num)
    return end;
  else if (a[start] > num)
    return start - 1;
  else if (a[start] == num)
    return start;
  else if (start + 1 >= end)
    return start;
  else {
    lg mid = (start + end) / 2;
    if (a[mid] == num)
      return mid;
    else if (a[mid] > num)
      return bin_search(a, start, mid - 1, num);
    else
      return bin_search(a, mid + 1, end, num);
  }
}

int main() {
  cin >> n >> m >> k;
  vector<lg> a(n, 0);
  vector<lg> b(m, 0);
  ans = 0;
  lg h = 0;
  for (lg i = 0; i < n; i++) {
    cin >> a[i];
    a[i] += h;
    h = a[i];
    if (h <= k)
      ans = i + 1;
  }
  for (lg i = 0; i < m; i++)
    cin >> b[i];

  lg time = 0;
  for (lg i = 0; i < m; i++) {
    time += b[i];
    if (time > k)
      break;
    lg ind = bin_search(a, 0, n - 1, k - time) + 1;
    ans = max(ans, i + ind + 1);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lg long long int
#define loop(i, s, e) for (lg i = s; i <= e; i++)
#define iloop(i, s, e) for (lg i = e; i >= s; i--)
#define pb push_back
#define mp make_pair
#define mod 1000000007

using namespace std;

lg min(lg a, lg b) { return a < b ? a : b; }
lg max(lg a, lg b) { return a > b ? a : b; }
lg lcm(lg a, lg b) { return (a * b) / __gcd(a, b); }
lg n, m, k, ans;
void sol(vector<lg> &a, vector<lg> &b, lg ai, lg bi, lg now, lg books) {
  if (now <= k) {
    ans = max(books, ans);
    if (ai < n && bi < m) {
      sol(a, b, ai + 1, bi, now + a[ai], books + 1);
      sol(a, b, ai, bi + 1, now + b[bi], books + 1);
    } else if (ai < n) {
      sol(a, b, ai + 1, bi, now + a[ai], books + 1);
    } else if (bi < m) {
      sol(a, b, ai, bi + 1, now + b[bi], books + 1);
    }
  }
}

int main() {
  cin >> n >> m >> k;
  vector<lg> a(n, 0);
  vector<lg> b(m, 0);
  ans = 0;
  for (lg i = 0; i < n; i++)
    cin >> a[i];
  for (lg i = 0; i < m; i++)
    cin >> b[i];
  sol(a, b, 0, 0, 0, 0);
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"

using namespace std;
using ll = long long;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);

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

  for (int i = 1; i <= n; ++i) {
    a[i] += a[i - 1];
    // cout << i << " " << a[i] << endl;
  }
  // cout << " --- " << endl;
  for (int i = 1; i <= m; ++i) {
    b[i] += b[i - 1];
    // cout << i << " " << b[i] << endl;
  }

  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (k - a[i] < 0)
      continue;
    auto b_iter = upper_bound(b.begin(), b.end(), k - a[i]);
    int b_ind = distance(b.begin(), b_iter);
    // cout << i << " " << b_ind << endl;
    ans = max(ans, i + b_ind - 1);
  }
  cout << ans;
}

int main() {
  fastio;
  solve();

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"

using namespace std;
using ll = long long;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);

  // dp[a][b]
  vector<vector<ll>> dp(n, vector<ll>(m, 0));

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

  for (int i = 1; i <= n; ++i) {
    a[i] += a[i - 1];
    // cout << i << " " << a[i] << endl;
  }
  // cout << " --- " << endl;
  for (int i = 1; i <= m; ++i) {
    b[i] += b[i - 1];
    // cout << i << " " << b[i] << endl;
  }

  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (k - a[i] < 0)
      continue;
    auto b_iter = upper_bound(b.begin(), b.end(), k - a[i]);
    int b_ind = distance(b.begin(), b_iter);
    // cout << i << " " << b_ind << endl;
    ans = max(ans, i + b_ind - 1);
  }
  cout << ans;
}

int main() {
  fastio;
  solve();

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

#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;
using Grid = vector<vector<char>>;
using vin = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vbl = vector<bool>;
using vch = vector<char>;
using pinin = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rell(i, n) for (ll i = 0; i < (ll)(n); i++)
#define sort(x) sort(x.begin(), x.end());
#define rese(x) reverse(x.begin(), x.end());
#define vnn(x, y, name) vector<vector<int>> name(x, vector<int>(y));

string yes = "Yes", no = "No";

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vll a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  vll bsum(m + 1, 0), asum(n + 1, 0);
  rep(i, m) bsum[i + 1] = bsum[i] + b[i];
  rep(i, n) asum[i + 1] = asum[i] + a[i];
  ll ans = 0;
  rep(i, n + 1) {
    if (asum[i] > k)
      break;
    ll ok = -1, ng = m + 1;
    while (ng - ok > 1) {
      int mid = (ok + ng) / 2;
      if (asum[i] + bsum[mid] > k)
        ng = mid;
      else
        ok = mid;
    }
    ans = max(ans, i + ok);
  }
  cout << ans << endl;
}


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> suma(n + 1, 0);
  vector<long long> sumb(m + 1, 0);

  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a.at(i);
    suma.at(i + 1) = suma.at(i) + a.at(i);
  }
  vector<long long> b(m);
  for (long long i = 0; i < m; i++) {
    cin >> b.at(i);
    sumb.at(i + 1) = sumb.at(i) + b.at(i);
  }
  long long ans = 0;
  long long i = 0;
  while (i < n + 1) {
    long long x = k - suma.at(i);
    if (x < 0) {
      break;
    }
    if (ans >= i + m) {
      i = ans + 1 - m;
    }

    for (long long j = ans - i + 1; j < m + 1; j++) {

      if (sumb.at(j) <= x) {
        ans = j + i;
      }

      if (j == m) {
        i++;
      }
    }
  }

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

#include <iostream>
typedef long long ll;
using namespace std;

// ll sumArray(ll a[], int n)
// {
// 	ll sum = 0;
// 	for (int i = 0; i < n; i++)
// 	{
// 		sum += a[i];
// 	}
// 	return sum;
// }

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

  int ans = 0;
  int ans_tmp;

  cin >> N >> M >> K;

  ll A[N];
  ll B[M];
  ll A_acc[N + 1];
  ll B_acc[M + 1];
  A_acc[0] = 0;
  B_acc[0] = 0;

  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int j = 0; j < M; j++)
    cin >> B[j];

  for (int i = 1; i < N + 1; i++) {
    A_acc[i] = A_acc[i - 1] + A[i - 1];
    // cout << A_acc[i] << endl;
  }
  for (int i = 1; i < M + 1; i++) {
    B_acc[i] = B_acc[i - 1] + B[i - 1];
    // cout << B_acc[i] << endl;
  }

  int j = M;
  for (int i = 0; i < N + 1; i++) {
    if (A_acc[i] > K)
      break;
    // cout << B_acc[j] << endl;
    while (B_acc[j] > K - A_acc[i]) {
      j--;
      // cout << j << endl;
    }
    ans_tmp = i + j;
    // cout << "i: " << i << endl;
    // cout << "j: " << j << endl;
    // cout << "ans: " << ans_tmp << endl;
    if (ans_tmp > ans)
      ans = ans_tmp;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
typedef long long ll;
using namespace std;

ll sumArray(ll a[], int n) {
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  return sum;
}

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

  int ans = 0;
  int ans_tmp;

  cin >> N >> M >> K;

  ll A[N];
  ll B[M];
  ll A_acc[N + 1];
  ll B_acc[M + 1];

  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int j = 0; j < M; j++)
    cin >> B[j];

  for (int i = 0; i < N + 1; i++) {
    A_acc[i] = sumArray(A, i);
    // cout << A_acc[i] << endl;
  }
  for (int i = 0; i < M + 1; i++) {
    B_acc[i] = sumArray(B, i);
    // cout << B_acc[i] << endl;
  }

  int j = M;
  for (int i = 0; i < N + 1; i++) {
    if (A_acc[i] > K)
      break;
    // cout << B_acc[j] << endl;
    while (B_acc[j] > K - A_acc[i]) {
      j--;
      // cout << j << endl;
    }
    ans_tmp = i + j;
    // cout << "i: " << i << endl;
    // cout << "j: " << j << endl;
    // cout << "ans: " << ans_tmp << endl;
    if (ans_tmp > ans)
      ans = ans_tmp;
  }

  cout << ans << endl;
}