alexjercan/bugnet huggingface dataset:
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);

  vector<int> book(N + 1); // 読める本の数 Aの本はN冊～0冊
  int64_t total = 0;       // Aの本を〇冊読むのにかかる時間
  int64_t btotal = 0;
  int bbook = 0;
  int y = 0;
  int maxbook = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    total += A.at(i);
  }
  for (int i = 0; i < M; i++)
    cin >> B.at(i);

  for (int i = N; i >= 0; i--) {
    if (i != N)
      total -= A.at(i);
    if (total > K)
      continue;
    book.at(i) += i;
    while (total + btotal < K && y < M) {
      if (total + btotal + B.at(y) <= K) {
        btotal += B.at(y);
        y++;
        bbook++;
      } else
        break;
    }
    book.at(i) += bbook;
  }
  for (int i = 0; i <= N; i++) {
    if (maxbook < book.at(i))
      maxbook = book.at(i);
  }

  cout << maxbook << endl;
}

Error: TLE

Fixed submission:

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

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

  int64_t total = 0;
  int64_t time = 0;
  int book = 0;
  int maxb = 0;

  vector<int> A(N);
  vector<int> B(M);

  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }

  for (int i = -1; i < N; i++) {
    if (i != -1) {
      total += A.at(i); // Aの本をi番目まで読み終わるのにかかる時間
      if (total > K) {
        break;
      }
      book = i + 1;
    }
    time = total;
    for (int j = 0; j < M; j++) {
      if (time + B.at(j) <= K) {
        time += B.at(j);
        book++;
      } else {
        break;
      }
    }
    if (maxb < book) {
      maxb = book;
    }
  }
  cout << maxb << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll sum = 0;
  int cnt = 0, itr1 = 0;
  rep(i, n) {
    if (sum + a[i] <= k)
      sum += a[i], cnt++, itr1 = i;
    else
      break;
  }
  int ans = cnt;
  rep(i, m) {
    if (sum + b[i] <= k)
      sum += b[i], cnt++;
    else {
      while (sum > 0 && itr1 >= 0) {
        sum -= a[itr1--];
        cnt--;
        if (sum + b[i] <= k)
          break;
      }
      if (sum + b[i] <= k)
        sum += b[i], cnt++;
      else
        break;
    }
    chmax(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

int main() {
  int n, m;
  ll k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll sum = 0;
  int cnt = 0, itr1 = 0;
  rep(i, n) {
    if (sum + a[i] <= k)
      sum += a[i], cnt++, itr1 = i;
    else
      break;
  }
  int ans = cnt;
  rep(i, m) {
    if (sum + b[i] <= k)
      sum += b[i], cnt++;
    else {
      while (sum > 0) {
        sum -= a[itr1--];
        cnt--;
        if (sum + b[i] <= k)
          break;
      }
      if (sum + b[i] <= k)
        sum += b[i], cnt++;
      else
        break;
    }
    chmax(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int64_t> s_a(n + 1);
  vector<int64_t> s_b(m + 1);
  for (i = 0; i < n; i++) {
    s_a[i + 1] = s_a[i] + a[i];
  }
  for (i = 0; i < m; i++) {
    s_b[i + 1] = s_b[i] + b[i];
  }

  int ans = 0;
  for (i = 0; i < n + 1; i++) {
    if (s_a[i] > k) {
      break;
    }

    int tmp = upper_bound(s_b.begin(), s_b.end(), k - s_a[i]) - s_b.begin();
    ans = max(ans, tmp + i - 1);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

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

  int64_t sum = 0;
  i = 0;
  int j = 0, cnt = 0;
  if (a[0] <= k && b[0] <= k) {
    while (1) {
      if (sum > k) {
        break;
      }

      if (a[i] <= b[j]) {
        sum += a[i];
        i++;
      }
      if (a[i] > b[j]) {
        sum += b[j];
        j++;
      }
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define MAX 200000
using namespace std;

/*
1 <= n,m < 200000
1 <= k,a,b < 10^9
*/

long long k, m, n;
vector<long long> a, b;

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

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }

  long long sum = 0;

  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    b.push_back(x);
    sum += x;
  }

  /*尺取り法*/
  /*sum : bの合計*/
  int j = m;
  int ans = 0;

  for (int i = 0; i < n + 1; i++) {
    /*aを0冊からn冊まで考える*/
    while (j > 0 && sum > k) {
      j--;
      sum -= b[j];
    }
    if (sum > k)
      break;
    ans = max(ans, j + i);
    if (i == n)
      break;
    // cout << j + i << " " << i <<" " << j << endl;
    sum += a[i];
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define MAX 200000
using namespace std;

/*
1 <= n,m < 200000
1 <= k,a,b < 10^9
*/

long long k, m, n;
vector<long long> a, b;

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

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

  /**/
  long long suma[MAX], sumb[MAX];
  int maxa = 0, maxb = 0;

  suma[0] = a[0];
  sumb[0] = b[0];

  for (int i = 1; i < n; i++) {
    suma[i] = suma[i - 1] + a[i];
    maxa++;
    if (suma[i] > k) {
      break;
    }
  }

  for (int i = 1; i < n; i++) {
    sumb[i] = sumb[i - 1] + b[i];
    maxb++;
    if (sumb[i] > k) {
      break;
    }
  }

  long long dp[m + n];

  dp[0] = min(suma[0], sumb[0]);

  int count = 0;

  if (dp[0] > k)
    count = 0;
  else {

    for (int i = 1; i < m + n; i++) {
      long long minsum = suma[i];
      for (int x = 0; x < i; x++) {
        minsum = min(minsum, suma[x] + sumb[i - x]);
      }
      count++;
      dp[i] = minsum;
      if (dp[i] > k)
        break;
    }
  }

  cout << count + 1 << endl;

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

#include <bits/stdc++.h>

#define maxN 200005

using namespace std;

long long a[maxN], b[maxN], pA[maxN], pB[maxN], sum, k;

int main() {
  int n, m, ans = 0;

  cin >> n >> m >> k;
  for (int i = 0; i < maxN; ++i) {
    a[i] = INT_MAX;
    b[i] = INT_MAX;
    pA[i] = INT_MAX;
    pB[i] = INT_MAX;
  }

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

  for (int i = 0; i <= n; ++i) {
    if (k < pA[i])
      continue;

    int x = k - pA[i];
    int y = upper_bound(pB, pB + maxN, x) - pB - 1;

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

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long a[2000005], b[2000005], sum, k;

int main() {
  int n, m, ans = 0;

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

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

  int i, j;
  i = j = 0;
  while (1) {
    if (a[i] > b[j]) {
      sum += b[j];
      ++j;
      if (sum > k)
        break;
      ++ans;
    } else {
      sum += a[i];
      ++i;
      if (sum > k)
        break;
      ++ans;
    }

    if (i >= n && j >= m)
      break;
  }

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

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

using ll = long long;

int main() {

  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 sum = 0;
  for (int i = 0; i < M; i++) {
    sum += b[i];
  }

  int ans = 0;
  int j = M;
  for (int i = 0; i <= N; i++) {
    while (j > 0 && sum > K) {
      j--;
      sum -= b[j];
    }
    if (sum > K)
      break;
    ans = max(ans, i + j);
    if (i == N)
      break;
    sum += a[i];
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

#define INF 1000000001

int main() {

  int N, M, K;
  cin >> N >> M >> K;

  vector<int> a(N);
  vector<int> b(M);
  int sum = 0;
  int aStop, bStop;
  vector<int> adp(N + 1, 0);
  vector<int> bdp(M + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    adp[i + 1] = adp[i] + a[i];
    if (adp[i + 1] > K) {
      break;
    }
    aStop = i + 1;
  }
  sum = 0;
  for (int i = 0; i < M; i++) {
    cin >> b[i];
    bdp[i + 1] = bdp[i] + b[i];
    if (bdp[i + 1] > K) {
      break;
    }
    bStop = i + 1;
  }
  int ans = 0;
  // cout << "aStop:" << aStop << endl;
  // cout << "bStop:" << bStop << endl;

  for (int i = 0; i <= aStop; i++) {
    for (int j = 0; j <= bStop; j++) {
      if (adp[i] + bdp[j] <= K) {
        ans = max(ans, i + j);
        // cout << i << "," << j << endl;
        // cout << adp[i]+bdp[j] << endl << endl;
      } else {
        break;
      }
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];

  vector<ll> va(n + 1, 0), vb(m + 1, 0);
  rep(i, 0, n) va[i + 1] = va[i] + a[i];
  rep(i, 0, m) vb[i + 1] = vb[i] + b[i];

  int ans = 0, j = m;
  rep(i, 0, n + 1) {
    if (va[i] > k)
      break;
    while (vb[j] > k - va[i])
      j -= 1;
    chmax(ans, i + j);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n), b(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];

  vector<ll> va(n + 1, 0), vb(n + 1, 0);
  rep(i, 0, n) va[i + 1] = va[i] + a[i];
  rep(i, 0, m) vb[i + 1] = vb[i] + b[i];

  int ans = 0, j = m;
  rep(i, 0, n + 1) {
    if (va[i] > k)
      break;
    while (vb[j] > k - va[i])
      j -= 1;
    chmax(ans, i + j);
  }

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

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k, ans = 0, x;
  cin >> n >> m >> k;
  int a[n + 5], b[n + 5];
  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;
  }
  if (a[n] + b[n] <= k) {
    cout << m + n << endl;
    return 0;
  }
  for (int i = 0; i <= n; i++)
    if (k >= a[i])
      ans =
          max(ans, (int)(upper_bound(b + 1, b + m + 1, k - a[i]) - b - 1 + i));
  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() {
  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];
  ll t = 0;
  rep(i, m) t += b[i];
  int j = m;
  int ans = 0;
  rep(i, n + 1) {
    while (j > 0 && t > k) {
      --j;
      t -= b[j];
    }
    if (t > k)
      break;
    ans = max(ans, i + j);
    if (i == n)
      break;
    t += a[i];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  int m;
  int k;
  int answer;
  answer = 0;
  cin >> n;
  cin >> m;
  cin >> k;
  vector<int> nvec(n);
  vector<int> mvec(m);
  for (int i = 0; i < n; ++i) {
    cin >> nvec.at(i);
  }
  for (int i = 0; i < m; ++i) {
    cin >> mvec.at(i);
  }
  int i = 0;
  int j = 0;
  while (k > nvec.at(i) || k > mvec.at(j)) {
    if (nvec.at(i) > mvec.at(j)) {
      k -= mvec.at(j);
      answer++;
      j++;
    } else {
      k -= nvec.at(i);
      answer++;
      i++;
    }
  }
  cout << answer << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const int mxeN = 200010;

ll n, m, k;
ll a[mxeN];
ll b[mxeN];

ll pfsumA[mxeN];
ll pfsumB[mxeN];

int main() {

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

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

  pfsumA[0] = 0;
  for (int i = 0; i < n; i++) {
    pfsumA[i + 1] = pfsumA[i] + a[i];
  }
  pfsumB[0] = 0;
  for (int i = 0; i < m; i++) {
    pfsumB[i + 1] = pfsumB[i] + b[i];
  }
  //	cout << lower_bound(pfsumA,pfsumA+n+1,30) - pfsumA<< endl;
  int globalmax = 0;
  for (int i = 0; i < n + 1; i++) {
    if (pfsumA[i] > k)
      continue;
    int lo = lower_bound(pfsumB, pfsumB + m + 1, k - pfsumA[i]) - pfsumB;
    if (lo > m || pfsumB[lo] > k - pfsumA[i])
      lo--;
    globalmax = max(globalmax, i + lo);
  }
  cout << globalmax << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const int mxeN = 200000;

ll n, m, k;
ll a[mxeN];
ll b[mxeN];

int solve(int sum, int pta, int ptb) {
  if (pta >= n && ptb >= m)
    return 0;
  if (ptb >= m) {
    if (sum + a[pta] <= k) {
      return 1 + solve(sum + a[pta], pta + 1, ptb);
    } else
      return 0;
  }
  if (pta >= n) {
    if (sum + b[ptb] <= k) {
      return 1 + solve(sum + b[ptb], pta, ptb + 1);
    } else
      return 0;
  }
  int qt;
  if (sum + a[pta] <= k && sum + b[ptb] <= k)
    qt = max(solve(sum + a[pta], pta + 1, ptb),
             solve(sum + b[ptb], pta, ptb + 1));
  else if (sum + a[pta] <= k)
    qt = solve(sum + a[pta], pta + 1, ptb);
  else if (sum + b[ptb] <= k)
    qt = solve(sum + b[ptb], pta, ptb + 1);
  else
    return 0;

  return qt + 1;
}

int main() {

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

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

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

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define Sort(a) sort(a.begin(), a.end())
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vi a(n), b(m);
  rep(i, n) { cin >> a[i]; }
  rep(i, m) { cin >> b[i]; }
  ll sum = 0;
  int ans = 0;
  int cnt_a = 0, cnt_b = 0;
  rep(i, n) {
    if (sum + a[i] <= k) {
      sum += a[i];
      cnt_a++;
    } else {
      break;
    }
  }
  ans = cnt_a;
  for (int i = cnt_a; i >= 0; i--) {
    for (int j = cnt_b; j < m; j++) {
      if (sum + b[j] <= k) {
        sum += b[j];
        cnt_b++;
      } else {
        break;
      }
    }
    ans = max(ans, cnt_a + cnt_b);
    cnt_a--;
    sum -= a[i - 1];
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define Sort(a) sort(a.begin(), a.end())
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int dp[200200][3];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vi a(n), b(n);
  rep(i, n) { cin >> a[i] >> b[i]; }
  int ans = 0;
  int cnt = 0;

  bool flag_a = false, flag_b = false;
  int i = 0, j = 0;
  while (1) {
    if (flag_a && flag_b)
      break;
    // if(i == 1) {
    dp[i][0] = min(dp[i][0] + a[i], dp[i][1] + a[i]);
    //}
    dp[i][1] = min(dp[i][0] + b[i], dp[i][1] + b[i]);
    i++;
    if (dp[i][1] > k && dp[i][0] > k)
      break;
  }
  cout << min(dp[i][1], dp[i][0]) << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  long long t = 0;

  int N, M, K;
  cin >> N >> M >> K;

  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    cin >> B[i];
    t += B[i];
  }

  int j = M;
  int ans = 0;
  for (int i = 0; i < N + 1; i++) {
    while (j > 0 && t > K) {
      j--;
      t -= B[j];
    }
    if (t > K)
      break;
    ans = max(ans, i + j);
    //    if (i == N) break;
    t += A[i];
  }

  cout << ans << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n, m, k, i = 0, j = 0;
  cin >> n >> m >> k;
  long long int a[n + 1], b[m + 1], count = 0;

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

  i = 0;
  j = 0;

  while (k > 0) {
    k -= min(a[i], b[j]);

    if (a[i] > b[j])
      j++;
    else if (a[i] < b[j])
      i++;
    else {
      if (a[i + 1] > b[j + 1])
        i++;
      else
        j++;
    }
    i++;
    if (k >= 0)
      count++;
  }
  cout << count;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

bool isOK(int &index, ll &x, vector<ll> &v) {
  if (v[index] <= x)
    return true;
  else
    return false;
}

int nibutan(ll &x, vector<ll> &v) {
  int ok = -1;
  int ng = (int)v.size();

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

    if (isOK(mid, x, v))
      ok = mid;
    else
      ng = mid;
  }
  return ok; // max_ok_index
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> ra(n + 1), rb(m + 1);
  ra[0] = 0;
  rb[0] = 0;
  rep(i, n) {
    int a;
    cin >> a;
    ra[i + 1] = ra[i] + a;
  }
  rep(i, m) {
    int b;
    cin >> b;
    rb[i + 1] = rb[i] + b;
  }
  int ans = 0;
  rep(i, n + 1) {
    ll now_k = k - ra[i];
    if (now_k < 0)
      continue;
    int j = nibutan(now_k, rb);
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

bool isOK(int &index, ll &x, vector<ll> &v) {
  if (v[index] <= x)
    return true;
  else
    return false;
}

int nibutan(ll &x, vector<ll> &v) {
  int ok = -1;
  int ng = (int)v.size();

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

    if (isOK(mid, x, v))
      ok = mid;
    else
      ng = mid;
  }
  return ok; // ok_index
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> ra(n + 1), rb(n + 1);
  ra[0] = 0;
  rb[0] = 0;
  rep(i, n) {
    int a;
    cin >> a;
    ra[i + 1] = ra[i] + a;
  }
  rep(i, m) {
    int b;
    cin >> b;
    rb[i + 1] = rb[i] + b;
  }
  int ans = 0;
  rep(i, n + 1) {
    ll now_k = k - ra[i];
    if (now_k < 0)
      continue;
    int j = nibutan(now_k, rb);
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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