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

#include <bits/stdc++.h>

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

ll solve(vector<ll> b_sum, ll x) {
  ll ok = -1;
  ll ng = b_sum.size();
  while (abs(ng - ok) > 1) {
    ll mid = (ok + ng) / 2;

    if (x >= b_sum[mid])
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

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

  // rep(i, b_sum.size()) cout << b_sum[i] << " ";
  // cout << endl;
  ll j = m;
  rep(i, n + 1) {
    while (j > 0 && current_sum > k) {
      j--;
      current_sum -= b[j];
    }

    if (current_sum > k)
      break;
    ans = max(ans, i + j);
    if (i == n)
      break;
    current_sum += a[i];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

ll solve(vector<ll> b_sum, ll x) {
  ll ok = -1;
  ll ng = b_sum.size();
  while (ng - ok > 1) {
    ll mid = (ok + ng) / 2;

    if (x >= b_sum[mid])
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1), b(m + 1), b_sum(m + 1, 0);
  a[0] = 0;
  rep(i, n) cin >> a[i + 1];
  rep(i, m) {
    cin >> b[i];
    b_sum[i + 1] = b_sum[i] + b[i];
  }
  ll sum = 0, ans = 0;
  // rep(i, b_sum.size()) cout << b_sum[i] << " ";
  // cout << endl;

  rep(i, n + 1) {
    sum += a[i];
    if (sum > k)
      break;
    // cout << "x = " << k - sum << endl;
    // auto itr = upper_bound(b_sum.begin(), b_sum.end(), k - sum);
    // cout << "value = " << *itr << endl;
    // ll num = itr - b_sum.begin() - 1;
    ll num = solve(b_sum, k - sum);
    // cout << "i = " << i + 1 << " num = " << num << endl;
    ans = max(ans, num + i);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>

#include <algorithm>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

int main() {
  int n, m, k, a, cont1 = 0, cont2 = 0, r, i;
  cin >> n >> m >> k;
  int s1, s2;
  vector<long long> x(200005);
  vector<long long> y(200005);

  for (i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> y[i];
  }
  s1 = s2 = 0;
  for (i = 1; i <= n; i++) {
    if (s1 + x[i] > k)
      break;
    cont1++;
    s1 += x[i];
  }
  for (i = 1; i <= m; i++) {
    if (s1 + s2 + y[i] > k)
      break;
    cont2++;
    s2 += y[i];
  }

  r = cont1 + cont2;

  while (cont1 > 0) {
    s1 -= x[cont1--];
    while (s1 + s2 + y[cont2 + 1] <= k && cont2 < m) {
      cont2++;
      s2 += y[cont2];
    }
    r = max(r, cont1 + cont2);
  }

  cout << r << endl;

  return 0;
}


Error: -6
free(): invalid size


Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>

#include <algorithm>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

int main() {
  int n, m, k, a, cont1 = 0, cont2 = 0, r, i;
  cin >> n >> m >> k;
  int s1, s2;
  vector<long long> x(n);
  vector<long long> y(m);

  for (i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> y[i];
  }
  s1 = s2 = 0;
  for (i = 1; i <= n; i++) {
    if (s1 + x[i] > k)
      break;
    cont1++;
    s1 += x[i];
  }
  for (i = 1; i <= m; i++) {
    if (s1 + s2 + y[i] > k)
      break;
    cont2++;
    s2 += y[i];
  }

  r = cont1 + cont2;

  while (cont1 > 0) {
    s1 -= x[cont1--];
    while (s1 + s2 + y[cont2 + 1] <= k && cont2 < m) {
      cont2++;
      s2 += y[cont2];
    }
    r = max(r, cont1 + cont2);
  }

  cout << r << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long> a(n), b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<long long> A = {0}, B = {0};
  for (int i = 0; i < n; i++) {
    A.push_back(a[i] + A[i]);
  }
  for (int i = 0; i < m; i++) {
    B.push_back(b[i] + B[i]);
  }
  int ans = 0, j = m;
  for (int i = 0; i < n + 1; i++) {
    if (A[i] > k) {
      break;
    }
    while (B[j] > k - A[i]) {
      j--;
    }
    ans = max(ans, i + j);
  }
  cout << ans << "\\n";
  return 0;
}

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), b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<long long> A = {0}, B = {0};
  for (int i = 0; i < n; i++) {
    A.push_back(a[i] + A[i]);
  }
  for (int i = 0; i < m; i++) {
    B.push_back(b[i] + B[i]);
  }
  int ans = 0;
  for (int i = 0; i < n + 1; i++) {
    if (A[i] > k) {
      break;
    }
    int x = -1;
    long long rest = k - A[i];
    for (int j = 0; j < m + 1; j++) {
      if (rest >= B[j]) {
        x = j;
      }
    }
    ans = max(ans, i + x);
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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

  long long A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;

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

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

  int mx = -1;
  for (int i = 0; i <= n; i++) {
    if (A[i] > k)
      break;

    int left = 0, right = m;
    while (left <= right) {
      int mid = (left + right) / 2;
      if (A[i] + B[mid] <= k) {
        mx = max(mx, mid + i);
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }
  }

  cout << mx << endl;
}


Error: TLE

Fixed submission:

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

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

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

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

  long long A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;

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

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

  int mx = -1;
  for (int i = 0; i <= n; i++) {
    if (A[i] > k)
      break;

    int left = 0, right = m;
    while (A[i] + B[left + 1] <= k) {
      left++;
      if (left == m)
        break;
    }

    mx = max(mx, i + left);
  }

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

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

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.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }
  vector<int64_t> A_sum(N + 1), B_sum(M + 1);
  for (int i = 0; i < N + 1; i++) {
    if (i == 0) {
      A_sum.at(i) = 0;
    } else {
      A_sum.at(i) = A.at(i - 1) + A_sum.at(i - 1);
    }
  }
  for (int i = 0; i < M + 1; i++) {
    if (i == 0) {
      B_sum.at(i) = 0;
    } else {
      B_sum.at(i) = B.at(i - 1) + B_sum.at(i - 1);
    }
  }

  int ans = 0;
  int j_max = M;
  for (int i = 0; i < N + 1; i++) {
    if (A_sum.at(i) > K) {
      break;
    }
    while (B_sum.at(j_max) > K - A_sum.at(i)) {
      j_max--;
    }
    ans = max(ans, i + j_max);
  }
  cout << ans << endl;
  return 0;
}

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), B(M);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }
  vector<int64_t> A_sum(N + 1), B_sum(M + 1);
  for (int i = 0; i < N + 1; i++) {
    if (i == 0) {
      A_sum.at(i) = 0;
    } else {
      A_sum.at(i) = A.at(i - 1) + A_sum.at(i - 1);
    }
  }
  for (int i = 0; i < M + 1; i++) {
    if (i == 0) {
      B_sum.at(i) = 0;
    } else {
      B_sum.at(i) = B.at(i - 1) + B_sum.at(i - 1);
    }
  }

  int max = 0;
  int j_max = B_sum.size() - 1;
  for (int i = 0; i < N + 1; i++) {
    if (A_sum.at(i) > K) {
      break;
    }
    for (int j = 0; j < M + 1; j++) {
      if (j > j_max) {
        break;
      }
      if (A_sum.at(i) + B_sum.at(j) <= K && i + j > max) {
        max = i + j;
      }
      if (A_sum.at(i) + B_sum.at(j) > K && j < j_max) {
        j_max = j;
      }
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  int n, m;
  ll 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];
  long long pre_b[m], pre_a[n];
  pre_b[0] = b[0];
  pre_a[0] = a[0];
  for (int i = 1; i < m; ++i) {
    pre_b[i] = (ll)pre_b[i - 1] + (ll)b[i];
  }
  for (int i = 1; i < n; ++i) {
    pre_a[i] = (ll)pre_a[i - 1] + (ll)a[i];
  }
  int answer = 0;
  for (int count = 0; count <= n; ++count) {
    long long check = (count == 0 ? 0 : pre_a[count - 1]);
    if (k >= check) {
      long long left = k - check;
      // find max number of books u can read with k == left;
      int pos = 0;
      if (pre_b[0] > left) {
      } else if (pre_b[m - 1] <= left) {
        pos = m;
      } else {
        int L = 0;
        int R = m - 1;
        while (L <= R) {
          int mid = (L + R) / 2;
          if (pre_b[mid] <= left) {
            if (mid < (m - 1) && pre_b[mid + 1] > left) {
              pos = mid + 1;
            }
            L = mid + 1;
          } else {
            if (mid > 0 && pre_b[mid - 1] <= left) {
              pos = mid;
            }
            R = mid - 1;
          }
        }
      }
      pos += count;
      answer = max(answer, pos);
    }
  }
  cout << answer << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
map<pair<pair<int, int>, int>, int> dp;

int f(int a[], int b[], int n, int m, int i, int j, ll k) {
  if (dp.count({{i, j}, k})) {
    return dp[{{i, j}, k}];
  }
  if (i == n && j == m) {
    dp[{{i, j}, k}] = 0;
    return 0;
  }
  int answer = 0;
  if (i < n && j < m && a[i] <= k && b[j] <= k) {
    answer = 1 + max(f(a, b, n, m, i + 1, j, k - a[i]),
                     f(a, b, n, m, i, j + 1, k - b[j]));
  } else if (i < n && a[i] <= k) {
    answer = 1 + f(a, b, n, m, i + 1, j, k - a[i]);
  } else if (j < m && b[j] <= k) {
    answer = 1 + f(a, b, n, m, i, j + 1, k - b[j]);
  }
  dp[{{i, j}, k}] = answer;
  return answer;
}
int main() {
  int n, m;
  ll 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];
  cout << f(a, b, n, m, 0, 0, k) << '\\n';
  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 < int(N); ++i)
using i64 = int64_t;
using ll = long long;
int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N + 1);
  vector<ll> B(M + 1);
  ll a = 0, b = 0;
  rep(i, N + 1) {
    if (i == 0) {
      A.at(i) = 0;
      continue;
    }
    ll p;
    cin >> p;
    a += p;
    A.at(i) = a;
  }
  rep(i, M + 1) {
    if (i == 0) {
      B.at(i) = 0;
      continue;
    }
    ll q;
    cin >> q;
    b += q;
    B.at(i) = b;
  }
  ll ans = 0;
  ll best = M;
  rep(i, N + 1) {
    ll lim = K - A.at(i);
    if (lim < 0) {
      break;
    }
    for (ll j = best; j >= 0; j--) {
      if (B.at(j) > lim) {
        continue;
      }
      best = j;
      ans = max(ans, i + j);
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using i64 = int64_t;
using ll = long long;
int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  queue<ll> A;
  queue<ll> B;
  rep(i, N) {
    ll a;
    cin >> a;
    A.push(a);
  }
  rep(i, M) {
    ll b;
    cin >> b;
    B.push(b);
  }
  ll m = 0;
  ll ans = 0;
  while (m <= K && (A.size() != 0 || B.size() != 0)) {
    if (A.front() >= B.front() || (A.size() == 0 && B.size() != 0)) {
      m += B.front();
      B.pop();
      ans++;
      continue;
    }
    if (A.front() < B.front() || (A.size() != 0 && B.size() == 0)) {
      m += A.front();
      A.pop();
      ans++;
    }
  }
  if (m > K) {
    cout << ans - 1 << endl;
  } else {
    cout << ans << 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 n, m, k;

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

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

  printf("%lld\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 10;

int n, m, k;

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

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

  printf("%lld\\n", ans);

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

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

#define each(i, c) for (auto &i : c)
#define mkp(a, b) make_pair(a, b)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> Pll;
const ll MOD = 1e9 + 7;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << ": " << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  each(i, v) os << i << ", ";
  os << ")";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> m) {
  os << "{";
  each(i, m) os << i << ", ";
  os << "}";
  return os;
}

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

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

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

  vector<ll> suma(n + 1);
  for (ll i = 1; i <= n; ++i) {
    suma[i] = suma[i - 1] + a[i - 1];
  }

  vector<ll> sumb(m + 1);
  for (ll i = 1; i <= m; ++i) {
    sumb[i] = sumb[i - 1] + b[i - 1];
  }

  ll ans = 0;
  for (ll i = 0; i <= n; ++i) {
    ll x = k;
    if (suma[i] <= x) {
      ans = max(ans, i);
      x -= suma[i];
    }

    ll lb = 0;
    ll ub = m;
    while (ub - lb > 1) {
      ll mid = (lb + ub) / 2;
      if (x < sumb[mid]) {
        ub = mid;
      } else {
        lb = mid;
      }
    }
    if (suma[i] + sumb[lb] <= k) {
      ans = max(ans, i + lb);
    }
    if (suma[i] + sumb[ub] <= k) {
      ans = max(ans, i + ub);
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define each(i, c) for (auto &i : c)
#define mkp(a, b) make_pair(a, b)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> Pll;
const ll MOD = 1e9 + 7;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << ": " << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  each(i, v) os << i << ", ";
  os << ")";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> m) {
  os << "{";
  each(i, m) os << i << ", ";
  os << "}";
  return os;
}

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

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

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

  vector<ll> suma(n + 1);
  for (ll i = 1; i <= n; ++i) {
    suma[i] = suma[i - 1] + a[i - 1];
  }

  vector<ll> sumb(m + 1);
  for (ll i = 1; i <= m; ++i) {
    sumb[i] = sumb[i - 1] + b[i - 1];
  }

  ll ans = 0;
  for (ll i = 0; i <= n; ++i) {
    for (ll j = m; j >= 0; --j) {
      if (suma[i] + sumb[j] <= k) {
        ans = max(ans, i + j);
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  int N, M;
  ll K;
  cin >> N >> M >> K;
  vector<ll> vA(N + 1, 0);
  vector<ll> vB(M + 1, 0);
  for (int i = 0; i < N; i++) {
    ll A;
    cin >> A;
    vA.at(i + 1) = vA.at(i) + A;
  }
  for (int i = 0; i < M; i++) {
    ll B;
    cin >> B;
    vB.at(i + 1) = vB.at(i) + B;
  }
  int ans = 0;
  int k = M;
  for (int i = 0; i < (N + 1); i++) {
    for (int j = k; j >= 0; j--) {
      if ((vA.at(i) + vB.at(j)) <= K) {
        if ((i + j) > ans)
          ans = i + j;
        k = j;
        break;
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll pow(ll x, ll y) {
  ll ans = 1;
  if (y > 0) {
    for (ll i = 0; i < y; i++)
      ans *= x;
  }
  return ans;
}
ll vsum(vector<ll> v) {
  ll ans = 0;
  for (int i = 0; i < v.size(); i++)
    ans += v.at(i);
  return ans;
}
int main() {
  int N, M;
  ll K;
  cin >> N >> M >> K;
  vector<ll> vA(N);
  vector<ll> vB(M);
  for (int i = 0; i < N; i++)
    cin >> vA.at(i);
  for (int i = 0; i < M; i++)
    cin >> vB.at(i);
  if (K >= vsum(vA) + vsum(vB)) {
    cout << N + M << endl;
  } else {
    ll time = 0;
    ll ans = 0;
    if (N > M)
      for (int i = 0; i < N - M; i++)
        vB.push_back(pow(10, 9));
    else if (M > N)
      for (int i = 0; i < M - N; i++)
        vA.push_back(pow(10, 9));
    int j = 0;
    for (int i = 0; i < max(M, N); i++) {
      if (K < time + min(vA.at(i), vB.at(j)))
        break;
      else {
        if (vA.at(i) > vB.at(j)) {
          time += vB.at(j);
          ans++;
          i--;
          j++;
        } else if (vB.at(j) > vA.at(i)) {
          time += vA.at(i);
          ans++;
        } else {
          bool finished = true;
          int t = 0;
          for (int k = 0; k < max(N, M) - max(i, j); k++) {
            if (vA.at(i + k) != vB.at(j + k)) {
              t = k;
              finished = false;
              break;
            }
          }
          if (vA.at(i + t) > vB.at(j + t)) {
            time += vB.at(j);
            ans++;
            i--;
            j++;
          } else {
            time += vA.at(i);
            ans++;
          }
          if (finished) {
            if (i > j) {
              time += vA.at(i);
              ans++;
            } else {
              time += vB.at(j);
              ans++;
              i--;
              j++;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, M;
  //  int s, t;
  int64_t K;
  cin >> N >> M >> K;
  vector<int64_t> A(N + 1);
  A.at(0) = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> A.at(i);
    A.at(i) += A.at(i - 1);
  }
  vector<int64_t> B(M + 1);
  B.at(0) = 0;
  for (int i = 1; i < M + 1; i++) {
    cin >> B.at(i);
    B.at(i) += B.at(i - 1);
  }
  int max = 0;
  int best = M;
  bool f = false;
  for (int i = 0; i < N + 1; i++) {
    //    cout << "i=" << i << endl;
    for (int j = best; j >= 0; j--) {
      //          cout << "j=" << j << endl;
      if (A.at(i) + B.at(j) <= K) {
        //        cout << A.at(i) << " " << i << " " << B.at(j) << " " << j << "
        //        " << K << endl;
        best = j;
        f = true;
        break;
      }
    }
    if (f && i + best > max) {
      max = i + best;
      //      s=i;
      //      t=best;
      //      cout << s << " " << t << endl;
    }
    f = false;
  }
  cout << max << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  int64_t K;
  cin >> N >> M >> K;
  vector<int64_t> A(N + 1);
  A.at(0) = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> A.at(i);
  }
  vector<int64_t> B(M + 1);
  B.at(0) = 0;
  for (int i = 1; i < M + 1; i++) {
    cin >> B.at(i);
  }
  int max = 0;
  int asum = 0;
  int sum = 0;
  for (int i = 0; i < N + 1; i++) {
    asum += A.at(i);
    if (asum > K)
      break;
    sum = asum;
    for (int j = 0; j < M + 1; j++) {
      sum += B.at(j);
      if (sum > K) {
        sum -= B.at(j);
        break;
      }
      if (i + j > max)
        max = i + j;
    }
  }
  cout << max << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define D(x) cout << #x << "=" << x << endl
#define De(...)
using namespace std;

int _;

int n, m, ans = 0, ma = -1, mb = -1;
long long k, a[200050], b[200050];

inline bool isPrime(int x) {
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0)
      return false;
  return true;
}

int main() {
  // for(scanf("%d",&_);_;_--)
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  for (int i = 0; i < m; i++)
    scanf("%d", b + i);
  for (int i = 0; i < n; i++)
    if (k >= a[i]) {
      ma++;
      k -= a[i];
      ans++;
    } else
      break;
  for (int i = 0; i < m; i++)
    if (k >= b[i]) {
      mb++;
      k -= b[i];
      ans++;
    } else
      break;
  while (ma >= 0) {
    k += a[ma];
    ma--;
    for (int i = mb + 1; i < m; i++)
      if (k >= b[i]) {
        mb++;
        k -= b[i];
      } else
        break;
    ans = max(ans, ma + mb + 2);
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i < r; i++)
#define hrp(i, l, r) for (register int i = l; i <= r; i++)
#define rev(i, r, l) for (register int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define U 200500
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#define R(x, l)
#define sp
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void);
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v);
int w[U], v[U];
signed main(void) {
#ifdef JOEON
  //		freopen("C:\\\\Users\\\\Joeon\\\\Desktop\\\\IN.txt", "r", stdin);
  //		freopen("C:\\\\Users\\\\Joeon\\\\Desktop\\\\OUT.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int a, b, t;
  cin >> a >> b >> t;
  rep(i, 0, a) cin >> w[i];
  rep(i, 0, b) cin >> v[i];
  int wPos = 0, vPos = 0, ans = 0;
  while (t > 0 && (t > w[wPos] || t > v[vPos])) {
    if (w[wPos] > v[vPos])
      t -= v[vPos++];
    else
      t -= w[wPos++];
    ans++;
    if (ans == a + b)
      break;
  }
  cout << ans << endl;
  return 0;
}
template <typename tn> inline tn next(void) {
  tn k;
  cin >> k;
  return k;
}
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v) {
  rep(i, 0, v.size()) os << v[i] << ' ';
  return os;
}