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

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

int main() {
  int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<int> a(n), b(m);

  for (auto &v : a) {
    std::cin >> v;
  }

  for (auto &v : b) {
    std::cin >> v;
  }

  std::int64_t t = 0;
  for (const auto &v : b) {
    t += v;
  }

  int bCount = m;
  int ans = 0;
  for (auto i = 0; i < n + 1; ++i) {
    while (bCount > 0 && t > k) {
      --bCount;
      t -= b[bCount];
    }
    if (t > k) {
      break;
    }
    ans = std::max(ans, i + bCount);
    if (i == n) {
      break;
    }
    t += a[i];
  }
  std::cout << ans;
}

Error: WA

Fixed submission:

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

int CalcCount(const std::vector<int> &a, const std::vector<int> &b, int k,
              int countA) {
  int ac = k;
  for (auto i = 0; i < countA; ++i) {
    ac -= a[i];
  }
  int ans = countA;
  for (const auto &v : b) {
    if (ac < v) {
      break;
    }
    ac -= v;
    ++ans;
  }
  return ans;
};

int BinarySearch(const std::vector<int> &a, const std::vector<int> &b, int k,
                 int minA, int maxA) {
  if (minA == maxA) {
    return minA;
  }

  if (minA + 1 == maxA) {
    if (CalcCount(a, b, k, minA) < CalcCount(a, b, k, maxA)) {
      return maxA;
    } else {
      return minA;
    }
  }

  const int midA = (minA + maxA) / 2;
  const int minCount = CalcCount(a, b, k, minA);
  const int maxCount = CalcCount(a, b, k, maxA);
  const int midCount = CalcCount(a, b, k, midA);

  if (minCount < midCount && midCount < maxCount) {
    return BinarySearch(a, b, k, midA, maxA);
  } else // if (maxCount < midCount && midCount < minCount)
  {
    return BinarySearch(a, b, k, minCount, midCount);
  }
}

int main() {
  int n, m, k;
  std::cin >> n >> m >> k;
  std::vector<int> a(n), b(m);
  for (auto &v : a) {
    std::cin >> v;
  }

  for (auto &v : b) {
    std::cin >> v;
  }

  int maxA = 0;
  {
    std::int64_t total = 0;
    for (const auto &v : a) {
      if (total + v > k) {
        break;
      }
      total += v;
      ++maxA;
    }
  }

  std::cout << CalcCount(a, b, k, BinarySearch(a, b, k, 0, maxA));
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long N, M, K;
  cin >> N >> M >> K;

  vector<long long> A(N), a(N + 1, 0), B(M), b(M + 1, 0);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    a[i + 1] = a[i] + A[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> B[i];
    b[i + 1] = b[i] + B[i];
  }

  long long 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 << endl;
  return 0;
}


Error: 0

Fixed submission:

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

long long N, M, K;
vector<long long> A, B, C;

long long dfs(vector<long long> A, vector<long long> B, long long i,
              long long j, long long cost, long long book) {
  if (cost < 0) {
    return book;
  }
  if (i >= N) {
    i = N;
  }
  if (j >= M) {
    j = M;
  }
  book++;

  long long A_cost = A[i];
  long long B_cost = B[j];
  // cout <<book << ":: " << cost << " : " << A_cost << " : " << B_cost << endl;

  long long A_num, B_num;
  A_num = dfs(A, B, i + 1, j, cost - A_cost, book);
  B_num = dfs(A, B, i, j + 1, cost - B_cost, book);

  return max(A_num, B_num);
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> N >> M >> K;
  A = vector<long long>(N + 1);
  B = vector<long long>(M + 1);
  for (long long i = 0; i < N; ++i) {
    cin >> A[i];
  }
  A[N] = 10000000000;
  for (long long i = 0; i < M; ++i) {
    cin >> B[i];
  }
  B[M] = 10000000000;

  long long ans = dfs(A, B, 0, 0, K, -1);

  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<int> a(n + 1, 0);
  vector<int> b(m + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i];
  vector<ll> sum(m + 1, 0);
  for (int i = 1; i <= m; i++)
    sum[i] = sum[i - 1] + b[i];
  int x = m;
  int res = 0;
  ll now = 0;
  for (int i = 0; i <= n; i++) {
    now += a[i];
    if (now > k)
      break;
    while (now + sum[x] > k)
      x--;
    res = max(res, i + x);
  }
  cout << res << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<int> a(n + 1, 0);
  vector<int> b(m + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i];
  vector<ll> sum(m + 1, 0);
  for (int i = 1; i <= m; i++)
    sum[i] = sum[i - 1] + a[i];
  int x = m;
  int res = 0;
  ll now = 0;
  for (int i = 0; i <= n; i++) {
    now += a[i];
    if (now > k)
      break;
    while (now + sum[x] > k)
      x--;
    res = max(res, i + x);
  }
  cout << res << "\\n";
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 1000000000
using namespace std;

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

  vector<long long> a(n), b(m);
  long long i, j;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < m; i++)
    cin >> b[i];

  vector<long long> aa(n + 1), bb(m + 1);

  aa[0] = 0;
  bb[0] = 0;
  for (i = 1; i <= n; i++) {
    aa[i] = aa[i - 1] + a[i - 1];
  }
  for (j = 1; j <= m; j++) {
    bb[j] = bb[j - 1] + b[j - 1];
  }

  long long cnt = 0;

  for (i = 0; i < n + 1; i++) {
    if (aa[i] > k)
      break;
    long long tmp = 0;
    tmp += i;
    j = lower_bound(bb.begin(), bb.end(), k - aa[i]) - bb.begin();
    if (bb[j] == k - aa[i])
      tmp += j;
    else
      tmp += (j - 1);
    cnt = max(cnt, tmp);
  }

  cout << cnt << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 1000000000
using namespace std;

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

  vector<long long> a(n), b(m);
  long long i, j;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < m; i++)
    cin >> b[i];

  vector<long long> aa(n + 1), bb(m + 1);

  aa[i] = 0;
  bb[i] = 0;
  for (i = 1; i <= n; i++) {
    aa[i] = aa[i - 1] + a[i - 1];
  }
  for (j = 1; j <= m; j++) {
    bb[j] = bb[j - 1] + b[j - 1];
  }

  long long cnt = 0;

  for (i = 0; i < n + 1; i++) {
    if (aa[i] > k)
      break;
    long long tmp = 0;
    tmp += i;
    j = lower_bound(bb.begin(), bb.end(), k - aa[i]) - bb.begin();
    if (bb[j] == k - aa[i])
      tmp += j;
    else
      tmp += (j - 1);
    cnt = max(cnt, tmp);
  }

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (long long i = 0; i < n; i++)

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  long long tmp;
  long long A[N], B[M];
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];
  // 入力終了

  // A[]とB[]の累積和
  long long cua[N + 1];
  cua[0] = 0;
  long long cub[M + 1];
  cub[0] = 0;
  rep(i, N) cua[i + 1] = cua[i] + A[i];
  rep(i, M) cub[i + 1] = cub[i] + B[i];

  long long ans = 0;

  // B[]をなめるindexと，その中で最適なbn
  int bn, bestbn = M;

  rep(an, N + 1) {
    // 累積和の性質上，bestbnから左に見ていけばOK
    bn = bestbn;

    // Kを下回るまでbnを減らしていく
    while (cua[an] + cub[bn] > K && bn > 0)
      bn--;

    // bestの更新
    bestbn = bn;

    if (cua[an] + cub[bn] <= K) {
      // 最大値の更新
      if (an + bn > ans)
        ans = an + bn;
    }
  }

  // 解答
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (long long i = 0; i < n; i++)

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  long long tmp;
  long long A[N], B[M];
  rep(i, N) cin >> A[i];
  rep(i, M) cin >> B[i];
  // 入力終了

  // A[]とB[]の累積和
  long long cua[N + 1];
  cua[0] = 0;
  long long cub[M + 1];
  cub[0] = 0;
  rep(i, N) cua[i + 1] = cua[i] + A[i];
  rep(i, M) cub[i + 1] = cub[i] + B[i];

  long long ans = 0;

  // B[]をなめるindexと，その中で最適なbn
  int bn, bestbn = M;

  rep(an, N + 1) {
    // 累積和の性質上，bestbnから左に見ていけばOK
    bn = bestbn;

    // Kを下回るまでbnを減らしていく
    while (cua[an] + cub[bn] > K)
      bn--;

    // bestの更新
    bestbn = bn;

    // 最大値の更新
    if (an + bn > ans)
      ans = an + bn;
  }

  // 解答
  cout << ans << 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++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);

int main() {

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

  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> b(m);
  REP(i, m) cin >> b[i];

  vector<ll> A(n + 1), B(m + 1);
  A[0] = 0, B[0] = 0;
  for (ll i = 0; i < n; i++) {
    A[i + 1] = a[i] + A[i];
  }
  for (ll i = 0; i < m; i++) {
    B[i + 1] = b[i] + B[i];
  }

  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (A[i] > k)
      break;

    ll ng = 0;
    ll ok = (ll)B.size();
    ll key = k - A[i];

    while (abs(ok - ng) > 1) {
      ll mid = (ok + ng) / 2;

      if (B[mid] > key)
        ok = mid;
      else
        ng = mid;
    }

    ans = max(ans, i + ng);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);

bool isOK(vector<ll> a, ll index, ll key) {
  if (a[index] > key)
    return true;
  else
    return false;
}

ll binary_search(vector<ll> a, ll key) {
  ll ng = 0;
  ll ok = (ll)a.size();

  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;

    if (isOK(a, mid, key))
      ok = mid;
    else
      ng = mid;
  }
  return ng;
}

int main() {

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

  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> b(m);
  REP(i, m) cin >> b[i];

  vector<ll> A(n + 1), B(m + 1);
  A[0] = 0, B[0] = 0;
  for (ll i = 0; i < n; i++) {
    A[i + 1] = a[i] + A[i];
  }
  for (ll i = 0; i < m; i++) {
    B[i + 1] = b[i] + B[i];
  }

  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (A[i] > k)
      break;
    // cout << i << " " << binary_search(B, k - A[i]) << endl;
    ans = max(ans, i + binary_search(B, k - A[i]));
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
  int n, m, k, x, t, maxx = 0;
  cin >> n >> m >> k;
  int a[n + 1], b[m + 1];
  a[0] = 0, b[0] = 0;

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

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

  int L = 0, R = n + m, lastf, lastr;

  while (L <= R) {
    lastf = L, lastr = R;
    bool f = false;
    int Mid = (L + R) / 2;
    for (int i = 0; i <= min(n, Mid); i++)
      if (Mid - i <= m) {
        t = a[i] + b[Mid - i];
        if (t <= k) {
          maxx = max(maxx, Mid);
          f = true;
          break;
        }
      }

    if (f)
      L = Mid;
    else
      R = Mid;

    if (lastf == L && lastr == R) {
      f = false;
      for (int i = 0; i <= min(n, R); i++)
        if (R - i <= m) {
          t = a[i] + b[R - i];
          if (t <= k) {
            cout << max(maxx, R) << endl;
            return 0;
          }
        }
      L = R + 1;
    }
  }

  cout << maxx << endl;
  return 0;
}

Error: -11

Fixed submission:

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

queue<int> a, b;

int min(int x, int y) { return x < y ? x : y; }

inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline void writeln(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
  puts("");
}

signed main() {
  int n = read(), m = read(), k = read();

  for (int i = 1; i <= n; i++) {
    int c = read();
    a.push(c);
  }

  for (int i = 1; i <= m; i++) {
    int d = read();
    b.push(d);
  }

  int ans = 0;

  while (k > 0 && !a.empty() || !b.empty()) {
    k -= min(a.front(), b.front());
    if (a.front() < b.front())
      a.pop();
    else
      b.pop();
    if (k >= 0)
      ans++;
  }

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

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

const int mx = 200005;
int n, m, k;
int a[mx], b[mx];
int pa[mx] = {}, pb[mx] = {};

template <typename T>

bool chkmax(T a, T b) {
  return (a > b) ? a : b;
}

void solve() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }

  pa[1] = a[0];
  pb[1] = b[0];
  for (int i = 2; i <= n; i++) {
    pa[i] = pa[i - 1] + a[i - 1];
  }
  for (int i = 2; i <= m; i++) {
    pb[i] = pb[i - 1] + b[i - 1];
  }
  int res = 0;
  for (int i = 0; i <= n; i++) {
    int left = k - pa[i];
    if (left < 0)
      break;
    auto idx = lower_bound(pb, pb + m, left);
    if (*idx > k - pa[i]) {
      res = max(res, i - 1 + (idx - pb));
      // cout << k - pa[i] << ' ' << (i - 1 + (idx - pb)) << '\\n';
    } else {
      res = max(res, i + (idx - pb));
      // cout << left << ' ' << (i + (idx - pb)) << '\\n';
    }
  }
  cout << res << '\\n';
}

signed main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

int n, m;
int a[200005], b[200005];

int recurse(int k, int as, int bs) {
  if (as == n and bs == m)
    return 0;
  if (k == 0)
    return 0;
  if (k < 0)
    return -1;
  if (as < n and bs < m) {
    int first = recurse(k - a[as], as + 1, bs),
        second = recurse(k - b[bs], as, bs + 1);
    return max(first + 1, second + 1);
  } else {
    if (as < n) {
      return recurse(k - a[as], as + 1, bs) + 1;
    }
    return recurse(k - b[bs], as, bs + 1) + 1;
  }
}

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define fornt(k, n) for (int i = k; i < n; ++i)
#define fornt2(p, n) for (int j = p; j < n; ++j)
#define pb push_back
#define ll long long
#define sc(n) scanf("%d", &n)
#define sc2(x, y) scanf("%d %d", &x, &y)
#define scl(n) scanf("%lld", &n)
#define scl2(x, y) scanf("%lld %lld", &x, &y)
#define pr(n) printf("%d\\n", n)
#define prl(n) printf("%lld\\n", n)
#define pry printf("YES\\n")
#define prn printf("NO\\n")
using namespace std;

const int MAX = 1e6 + 7;
unsigned ll a[MAX], b[MAX], x[MAX], y[MAX];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m, k, cnt;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++)
    a[i] += a[i - 1];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  for (int i = 1; i < m; i++)
    b[i] += b[i - 1];
  unsigned ll ans1 = 0, ans2 = 0, sum = 0;
  for (ll i = 0; i < n; i++) {
    sum = a[i];
    unsigned ll temp = k - sum;
    if (temp > k)
      break;
    unsigned ll pos = upper_bound(b, b + m, temp) - b;
    ans1 = max(pos + i + 1, ans1);
  }
  sum = 0;
  for (ll i = 0; i < m; i++) {
    sum = b[i];
    unsigned ll temp = k - sum;
    if (temp > k)
      break;
    unsigned ll pos = upper_bound(a, a + n, temp) - a;
    ans2 = max(pos + i + 1, ans2);
  }
  cout << max(ans1, ans2);
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define pb push_back
#define ll long long
#define fornt(k, n) for (ll i = k; i < n; ++i)
#define fornt2(p, n) for (ll j = p; j < n; ++j)
#define sc(n) scanf("%d", &n)
#define sc2(x, y) scanf("%d %d", &x, &y)
#define scl(n) scanf("%lld", &n)
#define scl2(x, y) scanf("%lld %lld", &x, &y)
#define pr(n) printf("%d\\n", n)
#define prl(n) printf("%lld\\n", n)
#define pry printf("YES\\n")
#define prn printf("NO\\n")
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n], b[n];
  fornt(0, n) cin >> a[i];
  fornt(1, n) a[i] += a[i - 1];
  fornt(0, m) cin >> b[i];
  fornt(0, m) b[i] += b[i - 1];
  unsigned ll sum, ans(0), ans2(0);
  fornt(0, n) {
    sum = a[i];
    unsigned ll t = k - sum;
    unsigned ll pos = upper_bound(b, b + n, t) - b;
    ans = max(pos + i + 1, ans);
  }
  fornt(0, m) {
    sum = b[i];
    unsigned ll t = k - sum;
    unsigned ll pos = upper_bound(a, a + n, t) - a;
    ans2 = max(pos + i + 1, ans2);
  }
  cout << max(ans, ans2);
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long> as, bs; // 累積和
  as.push_back(0);
  bs.push_back(0);

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    as.push_back(as[i] + a);
  }
  for (int i = 0; i < m; i++) {
    int b;
    cin >> b;
    bs.push_back(bs[i] + b);
  }

  // aを固定してbを最大化する、を後方から行う
  int ans = 0;
  int j = 0;
  for (int i = n; i >= 0; i--) {
    if (as[i] > k)
      continue;

    for (; j <= m; j++) {
      if (as[i] + bs[j] > k) {
        break;
      }
      ans = max(ans, i + j);
    }
  }

  cout << ans;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long> as, bs; // 累積和
  as.push_back(0);
  bs.push_back(0);

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    as.push_back(as[i] + a);
  }
  for (int i = 0; i < m; i++) {
    int b;
    cin >> b;
    bs.push_back(bs[i] + b);
  }

  // aを固定してbを最大化する、を後方から行う
  int ans = 0;
  for (int i = n; i >= 0; i--) {
    if (as[i] > k)
      continue;
    for (int j = m; j >= 0; j--) {
      if (as[i] + bs[j] > k)
        continue;
      else {
        ans = max(ans, i + j);
        break;
      }
    }
  }

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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long N, M;
  long long K;
  cin >> N;
  cin >> M;
  cin >> K;

  vector<long long> sumA(N + 1, 0);
  vector<long long> sumB(M + 1, 0);

  long long tmp = 0L;
  for (int i = 0; i < N; ++i) {
    long long a;
    cin >> a;
    tmp += a;
    sumA[i + 1] = tmp;
  }

  tmp = 0L;
  for (int j = 0; j < M; ++j) {
    long long b;
    cin >> b;
    tmp += b;
    sumB[j + 1] = tmp;
  }

  long long j = M;
  long long ans = 0;
  // read i books from the top of A
  for (int i = 0; i <= N; ++i) {
    if (sumA[i] > K)
      break;
    while (sumA[i] + sumB[j] > K)
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int solve(long K, vector<long> A, vector<long> B) {
  int a, b;
  a = 0;
  b = 0;
  if (!A.empty()) {
    if (K - A[0] >= 0) {
      a = solve(K - A[0], vector<long>(A.begin() + 1, A.end()), B);
      a++;
    }
  }
  if (!B.empty()) {
    if (K - B[0] >= 0) {
      b = solve(K - B[0], A, vector<long>(B.begin() + 1, B.end()));
      b++;
    }
  }
  return max(a, b);
}

int main() {
  int N, M;
  long K;
  cin >> N;
  cin >> M;
  cin >> K;

  vector<long> A(N);
  vector<long> B(M);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> B[i];
  }
  cout << solve(K, A, B) << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e6 + 5;
const long long inf = 0x3f3f3f3f;
long long a[maxn], b[maxn], maxv = -inf;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  long long n, m, k;
  cin >> n >> m >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (long long i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  for (long long i = 0; i <= n; i++)
    if (k >= a[i])
      maxv = max(maxv, upper_bound(b + 1, b + m + 1, k - a[i]) - b - 1 + i);
  cout << maxv << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
const long long inf = 0x3f3f3f3f;
long long a[maxn], b[maxn], maxv = -inf;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  long long n, m, k;
  cin >> n >> m >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (long long i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  for (long long i = 1; i <= n; i++)
    if (k >= a[i])
      maxv = max(maxv, upper_bound(b + 1, b + m + 1, k - a[i]) - b - 1 + i);
  cout << maxv << endl;
  return 0;
}