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

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define mrep(i, n) for (int i = (int)(n); i > 0; i--)
#define be(v) (v).begin(), (v).end()
#define dcout cout << fixed << setprecision(20)
#define fi first
#define se second
ll INF = 1LL << 60;
ll mod = 1e9 + 7;
ll modn = 998244353;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  ll a[n], b[m];
  ll A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;
  rep(i, n) {
    cin >> a[i];
    A[i + 1] = A[i] + a[i];
  }
  rep(i, m) {
    cin >> b[i];
    B[i + 1] = B[i] + b[i];
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    ll tim = A[i];
    if (tim > k)
      continue;
    ll limit = k - tim;
    if (limit >= B[m]) {
      ans = max(ans, i + m);
      continue;
    }
    int ok = 0;
    int ng = m;
    rep(i, 40) {
      int c = (ok + ng) / 2;
      if (limit >= B[c])
        ok = c;
      else
        ng = c;
    }
    ans = max(ans, i + ok);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rrep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++)
#define mrep(i, n) for (ll i = (ll)(n); i > 0; i--)
#define be(v) (v).begin(), (v).end()
#define dcout cout << fixed << setprecision(20)
#define fi first
#define se second
ll INF = 1LL << 60;
ll mod = 1e9 + 7;
ll modn = 998244353;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vint a(n);
  vint b(n);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  int ans = 0;
  int A = 0;
  int B = 0;
  rep(i, n + m) {
    if (k < min(a[A], b[B]))
      break;
    if (a[A] <= b[B]) {
      k -= a[A];
      A++;
      ans++;
    } else {
      k -= b[B];
      B++;
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\\n'
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define unique_sort(x)                                                         \\
  (sort(x.begin(), x.end()),                                                   \\
   x.resize(distance(x.begin(), unique(x.begin(), x.end()))))
#define fill(a) memset(a, 0, sizeof(a))
#define ff first
#define ss second
#define pb push_back
typedef long double ld;
typedef vector<ll> vll;
typedef vector<pair<ll, ll>> vpl;
const ll INF = 1e18;
const ld PI = 4 * atan((ld)1);
const ll mod = 1e9 + 7;

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll tt = 1;
  // cin >> tt;
  while ((tt)--) {
    ll n, m, k;
    cin >> n >> m >> k;
    vll a(n + 1), b(m + 1);
    repA(i, 1, n) {
      cin >> a[i];
      a[i] += a[i - 1];
    }
    repA(i, 1, m) {
      cin >> b[i];
      b[i] += b[i - 1];
    }

    ll ans = 0;
    ll i = 0;
    ll j = m;
    while (i <= n) {
      if (a[i] > k)
        break;

      while ((a[i] + b[j]) > k)
        j--;
      ans = max(ans, i + j);
      i++;
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\\n'
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define unique_sort(x)                                                         \\
  (sort(x.begin(), x.end()),                                                   \\
   x.resize(distance(x.begin(), unique(x.begin(), x.end()))))
#define fill(a) memset(a, 0, sizeof(a))
#define ff first
#define ss second
#define pb push_back
typedef long double ld;
typedef vector<ll> vll;
typedef vector<pair<ll, ll>> vpl;
const ll INF = 1e18;
const ld PI = 4 * atan((ld)1);
const ll mod = 1e9 + 7;

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll tt = 1;
  // cin >> tt;
  while ((tt)--) {
    ll n, m, k;
    cin >> n >> m >> k;
    vll a(n + 1), b(m + 1);
    repA(i, 1, n) {
      cin >> a[i];
      a[i] += a[i - 1];
    }
    repA(i, 1, m) {
      cin >> b[i];
      b[i] += b[i - 1];
    }

    ll ans = 0;
    ll i = 0;
    while (i <= n) {
      ll j = m;
      if (a[i] > k)
        break;

      while ((a[i] + b[j]) > k)
        j--;
      ans = max(ans, i + j);
      i++;
    }
    cout << ans << endl;
  }
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define pii pair<int, int>
#define ff first
#define ss second
#define N 2005

int INF = 1000000;
const int mod = 1e9 + 7;
using namespace std;
int dp[N][N];

void need_for_speed() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

int32_t main() {
  need_for_speed();
  int n, m, k;
  cin >> n >> m >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int b[m];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  bool fl1 = false, fl2 = false;
  int op1 = 0, op2 = 0, ans = 0;

  for (int i = 1; i < n; i++) {
    a[i] += a[i - 1];
  }

  for (int i = 1; i < m; i++) {
    b[i] += b[i - 1];
  }
  ans = lower_bound(b, b + m, k) - b;
  if (ans < m and b[ans] == k) {
    ans++;
  }

  int i = 0;
  while (i < n and a[i] <= k) {
    int rem = k - a[i];
    if (rem == 0) {
      ans = max(ans, i + 1);
      break;
    }
    if (rem < 0) {
      ans = max(ans, i);
      break;
    }
    int p = lower_bound(b, b + m, rem) - b;

    if (p < m and b[p] == rem) {
      p++;
    }

    ans = max(i + 1 + p, ans);
    i++;
  }
  cout << ans << endl;
  ;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define pii pair<int, int>
#define ff first
#define ss second
#define N 1005

int INF = 1000000;
const int mod = 1e9 + 7;
using namespace std;
int dp[N][N];
int n, r;
vector<int> v;

void need_for_speed() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

int32_t main() {
  need_for_speed();
  int n, m, k;
  cin >> n >> m >> k;
  stack<int> s1, s2;
  int a[n + 1], b[m + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = n; i >= 1; i--) {
    s1.push(a[i]);
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
  }
  for (int i = m; i >= 1; i--) {
    s2.push(b[i]);
  }
  int ans = 0;
  int cnt = 0;
  int flag = 0;
  while (!s1.empty() and !s2.empty()) {
    int x = s1.top();
    int y = s2.top();
    if (x > y) {
      // cout<<s2.top()<<" ";
      cnt += s2.top();
      s2.pop();
      if (cnt <= k) {
        ans++;
      } else {
        flag = 1;
        break;
      }
      s2.pop();
    } else {
      // cout<<s1.top()<<" ";
      cnt += s1.top();
      s1.pop();
      if (cnt <= k) {
        ans++;
      } else {
        flag = 1;
        break;
      }
      s1.pop();
    }
  }
  if (!flag) {
    while (!s1.empty()) {
      cnt += s1.top();
      s1.pop();
      if (cnt <= k) {
        ans++;
      } else {
        cout << ans << endl;
        return 0;
      }
    }
    while (!s2.empty()) {
      cnt += s2.top();
      s2.pop();
      if (cnt <= k) {
        ans++;
      } else {
        cout << ans << endl;
        return 0;
      }
    }

  } else
    cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
int n, m, k;

int main() {
  scanf("%d %d %d", &n, &m, &k);
  vector<int> a(n + 1, 0), b(m + 1, 0);
  vector<long long> sumA(n + 1, 0), sumB(m + 1, 0);

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

  int max_ans = 0;
  int j = m;
  for (int i = 0; i <= n; ++i) {
    if (sumA[i] > k) {
      break;
    }
    while (sumB[j] > k - sumA[i]) {
      j--;
    }
    max_ans = max(max_ans, i + j);
  }

  printf("%d\\n", max_ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
int n, m, k;
queue<int> a, b;
long long max_ans = 0;

void backtrack(long long time, long long cnt) {
  int cur_time;
  if (!a.empty()) {
    cur_time = a.front();
    if (time + cur_time <= k) {
      a.pop();
      backtrack(time + cur_time, cnt + 1);
      a.push(cur_time);
    }
  }

  if (!b.empty()) {
    cur_time = b.front();
    if (time + cur_time <= k) {
      b.pop();
      backtrack(time + cur_time, cnt + 1);
      b.push(cur_time);
    }
  }
  max_ans = max(max_ans, cnt);
}

int main() {
  scanf("%d %d %d", &n, &m, &k);
  int foo;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &foo);
    a.push(foo);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", &foo);
    b.push(foo);
  }
  backtrack(0, 0);
  printf("%lld\\n", max_ans);
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N, M, K, j = 1;
  long long cnt = 0, buf, sum = 0;
  unsigned long long max_a = 0, max_b = 0;
  bool f_a = true, f_b = true;
  cin >> N >> M >> K;
  vector<long long> A(N + 1);
  vector<long long> B(M + 1);
  for (long long i = 1; i <= N; i++) {
    cin >> buf;
    sum += buf;
    A.at(i) = sum;
  }
  sum = 0;
  for (long long i = 1; i <= M; i++) {
    cin >> buf;
    sum += buf;
    B.at(i) = sum;
  }
  max_b = B.size() - 1;
  for (int i = 0; i < A.size(); i++) {
    for (int j = max_b; j >= 0; j--) {
      if (A.at(i) + B.at(j) <= K) {
        max_b = j;
        if (i + j >= cnt) {
          cnt = i + j;
        }
        break;
      }
    }
    if (A.at(i) > K) {
      break;
    }
  }
  cout << cnt << endl;
}

Error: 0

Fixed submission:

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

int main() {
  long long N, M, K, j = 1;
  long long cnt = 0, time = 0;
  unsigned long long pos_a = 0, pos_b = 0;
  cin >> N >> M >> K;
  vector<long long> A(N);
  vector<long long> 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);
  }
  if (A.at(0) > K && B.at(0) > K) {
    cout << 0 << endl;
    return 0;
  }
  for (long long i = 0; i < N + M; i++) {
    if (pos_a < A.size() && A.at(pos_a) < B.at(pos_b)) {
      time += A.at(pos_a);
      pos_a++;
    } else if (pos_b < B.size() && A.at(pos_a) > B.at(pos_b)) {
      time += B.at(pos_b);
      pos_b++;
    } else if (A.at(pos_a) == B.at(pos_b)) {
      while (1) {
        if (pos_a < A.size() && pos_b < B.size() &&
            A.at(pos_a + j) < B.at(pos_b + j)) {
          time += A.at(pos_a);
          pos_a++;
          break;
        } else if (pos_a < A.size() && pos_b < B.size() &&
                   A.at(pos_a + 1) > B.at(pos_b + 1)) {
          time += B.at(pos_b);
          pos_b++;
          break;
        }
        j++;
      }
      j = 1;
    }
    cnt++;
    if (time == K) {
      cout << cnt << endl;
      return 0;
    } else if (time > K) {
      cout << cnt - 1 << endl;
      return 0;
    }
  }
  cout << cnt << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\\n"
#define MOD 1000000007

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;

  vector<int> a(n);
  vector<int> b(m);

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

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

  ll L = 0;
  ll R = 0;

  ll count = 0;

  while (count + a[L] <= k) {
    count += a[L];
    L++;
    if (L == n) {
      break;
    }
  }

  if (L == n || count < k) {
    while (count + b[R] <= k) {
      count += b[R];
      R++;
      if (R == m) {
        break;
      }
    }
  }

  if (R == m && L == n) {
    cout << n + m << "\\n";
    return;
  }

  ll ans = L + R;

  // debug(R);
  // debug(L);

  L--;

  while (L >= 0 && R < m) {
    count -= a[L];
    L--;

    while (count + b[R] <= k) {
      count += b[R];
      R++;
      if (R == m) {
        break;
      }
    }

    // debug(ans);
    ans = max(ans, L + R + 1);
  }

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

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

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}


Error: 0

Fixed submission:

/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\\n"
#define MOD 1000000007

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;

  vector<int> a(n);
  vector<int> b(m);

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

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

  ll L = 0;
  ll R = 0;

  ll count = 0;

  while (count + a[L] <= k) {
    count += a[L];
    L++;
    if (L == n - 1) {
      break;
    }
  }

  if (L == n - 1 && count < k) {
    while (count + b[R] <= k) {
      count += b[R];
      R++;
      if (R == m - 1) {
        break;
      }
    }
  }

  if (R == m - 1 && L == n - 1) {
    cout << n + m << "\\n";
    return;
  }

  ll ans = L + R;

  L--;

  while (L >= 0 && R < m) {
    count -= a[L];
    L--;

    while (count + b[R] <= k) {
      count += b[R];
      R++;
      if (R == m - 1) {
        break;
      }
    }

    ans = max(ans, L + R + 1);
  }

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

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

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <iomanip> : 表示桁数, ex) cout << fixed << setprecision(15) << ...

#define pi 3.14159265358979323846
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using ld = long double;
using Byte = unsigned char;
using namespace std;

// string ans = "Yes";

int SetAns() {
  int N, M, K;
  cin >> N >> M >> K;

  vector<int> A(N);
  vector<int> B(M);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  // 本体
  int ans = 0;
  ll sumA = 0;
  rep(j, N) sumA += A[j]; // against TLE

  ll nowSumA = sumA;
  ll nowSumB = 0;
  int bChecker = 0;

  rep(i, N + 1) {
    int nowAns = N - i + bChecker;
    ll time = nowSumA + nowSumB;
    if (i != N)
      nowSumA -= A[N - 1 - i];
    if (time > K)
      continue;

    for (; bChecker < M; bChecker++) {
      if (time + B[bChecker] > K)
        break;
      nowAns++;
      nowSumB += B[bChecker];
      time += B[bChecker];
    }

    ans = max(ans, nowAns);
  }

  return ans;
}

int main() {

  cout << SetAns() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <iomanip> : 表示桁数, ex) cout << fixed << setprecision(15) << ...

#define pi 3.14159265358979323846
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using ld = long double;
using Byte = unsigned char;
using namespace std;

// string ans = "Yes";

int SetAns() {
  int N, M, K;
  cin >> N >> M >> K;

  vector<int> A(N);
  vector<int> B(M);
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];

  // 本体
  int ans = 0;
  ll sumA = 0;
  rep(j, N) sumA += A[j]; // against TLE

  ll nowSumA = sumA;

  rep(i, N + 1) {
    int nowAns = N - i;
    ll time = nowSumA;
    if (i != N)
      nowSumA -= A[N - 1 - i];
    if (time > K)
      continue;

    rep(j, M) {
      if (time + B[j] > K)
        break;
      nowAns++;
      time += B[j];
    }

    ans = max(ans, nowAns);
  }

  return ans;
}

int main() {

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

#include <bits/stdc++.h>
#include <stdio.h>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define _USE_MATH_DEFINES
#define f first
// #define s second
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define INF 1000000000000000000
#define MAXN (int)(1e7) + 1
#define MOD 1000000007

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pwi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<pair<ll, ll>>> wGraph;
typedef vector<vector<ll>> graph;
// typedef vector<vector<ll> > matrix;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << std::fixed;
  cout << std::setprecision(2);

  int n, m;
  cin >> n >> m;
  ll k;
  cin >> k;

  vll a(n), b(m);

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

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

      ll l = 0;
      ll r = b.size() - 1;

      while (l <= r) {
        ll mid = (l + r) / 2;
        if (a[i] <= k - b[mid]) {
          // cout<<"VALS: "<<a[i]<<" + "<<b[mid]<<" with mid: "<<mid<<endl;
          res = max(res, i + mid + 2);
          l = mid + 1;
        } else {
          r = mid - 1;
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <stdio.h>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define _USE_MATH_DEFINES
#define f first
// #define s second
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define INF 1000000000000000000
#define MAXN (int)(1e7) + 1
#define MOD 1000000007

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pwi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<pair<ll, ll>>> wGraph;
typedef vector<vector<ll>> graph;
// typedef vector<vector<ll> > matrix;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << std::fixed;
  cout << std::setprecision(2);

  int n, m;
  cin >> n >> m;
  ll k;
  cin >> k;

  vll a(n), b(m);

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

  ll res = 0;
  for (ll i = 0; i < n; ++i) {
    if (a[i] <= k) {
      for (ll j = 0; j < m; ++j) {
        // cout<<a[i]<<" -- "<<b[j]<<endl;
        if (a[i] <= k - b[j]) {
          res = max(res, i + 2 + j);
        } else
          break;
      }
    }
  }

  cout << res << endl;

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

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

#define ll long long int

vector<ll> arr1, arr2;

ll bs(ll lower, ll upper, ll no) {

  ll pos = -1;
  while (lower <= upper) {
    ll mid = (lower + upper) / 2;
    if (no - arr2[mid] >= 0) {
      pos = max(pos, mid);
    }
    if (arr2[mid] < no)
      lower = mid + 1;
    else
      upper = mid - 1;
  }
  return pos;
}

int main() {
  ll n, m, k, i, temp, prefix;
  prefix = 0;
  cin >> n >> m >> k;
  for (i = 0; i < n; i++) {
    cin >> temp;
    arr1.push_back(prefix + temp);
    prefix = arr1[i];
  }
  prefix = 0;
  for (i = 0; i < m; i++) {
    cin >> temp;
    arr2.push_back(prefix + temp);
    prefix = arr2[i];
  }
  ll ans = 0;
  i = 0;
  while (i < n) {
    if (k - arr1[i] >= 0)
      ans = max(ans, i + 1);
    i++;
  }
  i = 0;
  while (i < m) {
    if (k - arr2[i] >= 0)
      ans = max(ans, i + 1);
    i++;
  }
  i = 0;
  while (i < n) {
    ll j = bs(0, m - 1, k - arr1[i]);
    if (j >= 0)
      ans = max(ans, i + j + 2);
    i++;
  }
  cout << ans;
}


Error: TLE

Fixed submission:

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

vector<ll> arr1, arr2;

int main() {
  ll n, m, k, i, temp, prefix;
  prefix = 0;
  cin >> n >> m >> k;
  for (i = 0; i < n; i++) {
    cin >> temp;
    arr1.push_back(prefix + temp);
    prefix = arr1[i];
  }
  prefix = 0;
  for (i = 0; i < m; i++) {
    cin >> temp;
    arr2.push_back(prefix + temp);
    prefix = arr2[i];
  }
  ll ans = 0;
  i = 0;
  while (i < n) {
    if (k - arr1[i] >= 0)
      ans = max(ans, i + 1);
    i++;
  }
  i = 0;
  while (i < m) {
    if (k - arr2[i] >= 0)
      ans = max(ans, i + 1);
    i++;
  }
  i = 0;
  while (i < n) {
    ll j = 0;
    while (j < m) {
      if (k - arr1[i] - arr2[j] >= 0) {
        ans = max(ans, i + j + 2);
      }
      j++;
    }
    i++;
  }
  cout << ans;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), timea(n + 1, 0), timeb(m + 1, 0);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    timea[i + 1] = timea[i] + a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
    timeb[i + 1] = timeb[i] + b[i];
  }
  ll ans = 0;
  for (ll i = 0; i < n + 1; i++) {
    ll tmp = 0;
    if (k < timea[i])
      break;
    auto itr = upper_bound(timeb.begin(), timeb.end(), k - timea[i]);
    if (itr - timeb.begin() == timeb.end() - timeb.begin())
      tmp = itr - timeb.begin() - 1;
    else
      tmp = itr - timeb.begin() - 1;
    tmp += i;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m), timea(n, 0), timeb(m, 0);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    timea[i + 1] = timea[i] + a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
    timeb[i + 1] = timeb[i] + b[i];
  }
  ll ans = 0;
  for (ll i = 0; i < n + 1; i++) {
    ll tmp = 0;
    if (k < timea[i])
      break;
    auto itr = upper_bound(timeb.begin(), timeb.end(), k - timea[i]);
    tmp = itr - timeb.begin();
    tmp--;
    if (tmp == m - 1)
      tmp++;
    tmp += i;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}