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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  ll n, m, k = 0;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<ll> aSum(n + 1);
  vector<ll> bSum(m + 1);
  aSum[0] = 0;
  bSum[0] = 0;
  for (ll i = 1; i < n + 1; i++) {
    aSum[i] = a[i - 1] + aSum[i - 1];
  }
  for (ll i = 1; i < m + 1; i++) {
    bSum[i] = b[i - 1] + bSum[i - 1];
  }
  /*
      for(ll i = 0;i <= n;i++){
          cout << aSum[i] << endl;
      }
      for(ll i = 0;i <= m;i++){
          cout << bSum[i] << endl;
      }
  */
  ll bCP = m;
  ll sum = 0;
  for (ll i = 0; i <= n; i++) {
    if (aSum[i] > k) {
      continue;
    }
    for (ll j = bCP; j >= 0; j--) {
      if (aSum[i] + bSum[j] <= k) {
        bCP = j;
        break;
      }
    }
    // cout << aSum[i] << " " << bSum[bCP] << endl;
    sum = max(sum, i + bCP);
  }

  cout << sum << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  ll n, m, k = 0;
  cin >> n >> m >> k;
  vector<ll> a(n);
  vector<ll> b(m);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<ll> aSum(n + 1);
  vector<ll> bSum(m + 1);
  aSum[0] = 0;
  bSum[0] = 0;
  for (ll i = 1; i < n + 1; i++) {
    aSum[i] = a[i - 1] + aSum[i - 1];
  }
  for (ll i = 1; i < m + 1; i++) {
    bSum[i] = b[i - 1] + bSum[i - 1];
  }
  /*
      for(ll i = 0;i <= n;i++){
          cout << aSum[i] << endl;
      }
      for(ll i = 0;i <= m;i++){
          cout << bSum[i] << endl;
      }
  */
  ll bCP = m;
  ll sum = 0;
  for (ll i = 0; i <= n; i++) {
    if (aSum[i] > k) {
      continue;
    }
    for (ll j = bCP; j >= 0; j--) {
      if (aSum[i] + bSum[j] <= k) {
        bCP = j + 1;
        break;
      }
    }
    // cout << aSum[i] << " " << bSum[bCP] << endl;
    sum = max(sum, i + bCP);
  }

  cout << sum << endl;

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

#include <bitset>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

#define MAX_N 200000
#define MAX_M 200000

int N, M, K;
int64_t A[MAX_N + 1], a[MAX_N + 1];
int64_t B[MAX_M + 1], b[MAX_M + 1];

void input() {
  a[0] = 0;
  b[0] = 0;
  A[0] = 0;
  B[0] = 0;
  cin >> N >> M >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i + 1];
    a[i + 1] = A[i + 1] + a[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i + 1];
    b[i + 1] = B[i + 1] + b[i];
  }
}

void solve() {
  // A[0]~A[i]の和a[i]
  // B[0]~B[j]の和b[i]
  // a[i]+b[j]<=K min? の条件下でi+jを最大とする。
  int ans = 0;
  int 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;
}

int main() {
  input();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bitset>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

#define MAX_N 200000
#define MAX_M 200000

int N, M, K;
int64_t A[MAX_N + 1], a[MAX_N + 1];
int64_t B[MAX_M + 1], b[MAX_M + 1];

void input() {
  a[0] = 0;
  b[0] = 0;
  A[0] = 0;
  B[0] = 0;
  cin >> N >> M >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i + 1];
    a[i + 1] = A[i + 1] + a[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> B[i + 1];
    b[i + 1] = B[i + 1] + b[i];
  }
}

void solve() {
  // A[0]~A[i]の和a[i]
  // B[0]~B[j]の和b[i]
  // a[i]+b[j]<=K min? の条件下でi+jを最大とする。
  int ans = 0;

  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < M + 1; j++) {
      if (a[i] + b[j] <= K) {
        ans = max(ans, i + j);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  input();
  solve();
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<int> A(N);
  vector<int64_t> SUMA(N + 1);
  SUMA.at(0) = 0;
  rep(i, N) {
    cin >> A.at(i);
    SUMA.at(i + 1) = SUMA.at(i) + A.at(i);
  }

  vector<int> B(M);
  vector<int64_t> SUMB(M + 1);
  SUMB.at(0) = 0;
  int jMax = 0;
  rep(i, M) {
    cin >> B.at(i);
    SUMB.at(i + 1) = SUMB.at(i) + B.at(i);
    if (SUMB.at(i + 1) <= K) {
      jMax = i + 1;
    }
  }

  int Ans = jMax;

  for (int i = 1; i <= N; i++) {
    for (int j = jMax; j >= 0; j--) {
      if (SUMA.at(i) + SUMB.at(j) <= K) {
        Ans = max(Ans, i + j);
        jMax = j;
        break;
      }
    }
  }
  cout << Ans << endl;
}

Error: TLE

Fixed submission:

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

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

  vector<int> A(N);
  vector<int64_t> SUMA(N + 1);
  SUMA.at(0) = 0;
  rep(i, N) {
    cin >> A.at(i);
    SUMA.at(i + 1) = SUMA.at(i) + A.at(i);
  }

  vector<int> B(M);
  vector<int64_t> SUMB(M + 1);
  SUMB.at(0) = 0;
  int jMax = 0;
  rep(i, M) {
    cin >> B.at(i);
    SUMB.at(i + 1) = SUMB.at(i) + B.at(i);
    if (SUMB.at(i + 1) <= K) {
      jMax = i + 1;
    }
  }

  int Ans = jMax;

  for (int i = 1; i <= N; i++) {
    for (int j = jMax; j >= max(Ans - i, 0); j--) {
      if (SUMA.at(i) + SUMB.at(j) <= K) {
        Ans = max(Ans, i + j);
        break;
      }
    }
  }
  cout << Ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

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

  long long int t = 0;
  for (int i = 0; i < m; 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: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

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

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

  int j = m;
  int ans = 0;

  for (int i = 0; i < n + 1; i++) {
    while (t > 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;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define md 1000000007
double PI = 3.1415926535;
const int N = (int)(1 * 1e6 + 10);
using namespace std;

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

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

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define md 1000000007
double PI = 3.1415926535;
const int N = (int)(1 * 1e6 + 10);
using namespace std;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  ll A[n + 1], B[m + 1];
  A[0] = 0;
  B[0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    A[i] = A[i - 1] + a[i];
  }
  for (ll i = 1; i <= m; i++) {
    cin >> b[i];
    B[i] = B[i - 1] + b[i];
  }
  ll j = m, ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (B[j] > k)
      j--;
    while (A[i] + B[j] > k)
      j--;

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

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

#include <bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define inf 0x3f3f3f3f
#define db double
using namespace std;
const int maxn = 2e5 + 10;
struct node {
  ll num;
  ll pos1, pos2;
  ll value;
};
int k, n, m;
int ma[2][maxn];
ll cal[2][maxn];
int ans = 0;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  ll tmp = 0, x = 0, y = 0;
  for (int i = 1; i <= n; i++) {
    cin >> ma[0][i];
    if (tmp > k)
      continue;
    tmp += ma[0][i];
    if (tmp <= k) {
      cal[0][i] = tmp;
      x = i;
    }
  }
  tmp = 0;
  for (int i = 1; i <= m; i++) {
    cin >> ma[1][i];
    if (tmp > k)
      continue;
    tmp += ma[1][i];
    if (tmp <= k) {
      cal[1][i] = tmp;
      y = i;
    }
  }
  if (x > y) {
    int j = 0;
    for (int i = x; i >= 0; i--) {
      while (j <= y) {
        if (cal[0][i] + cal[1][j] > k)
          break;
        ans = max(i + j, ans);
        j++;
      }
    }
  } else {
    int j = 0;
    for (int i = y; i >= 0; i--) {
      while (j <= x) {
        if (cal[1][i] + cal[0][j] > k)
          break;
        ans = max(i + j, ans);
        j++;
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define inf 0x3f3f3f3f
#define db double
using namespace std;
const int maxn = 2e5 + 10;
struct node {
  ll num;
  ll pos1, pos2;
  ll value;
};
int k, n, m;
int ma[2][maxn];
int ans = 0;
void dfs(int pos1, int pos2, ll value) {
  ll v = value;
  if (pos1 < n) {
    v += ma[0][pos1 + 1];
    // cout<<v<<endl;
    if (v <= k) {
      dfs(pos1 + 1, pos2, v);
    } else {
      ans = max(pos1 + pos2, ans);
      return;
    }
  }
  v = value;
  if (pos2 < m) {
    v += ma[1][pos2 + 1];
    if (v <= k) {
      dfs(pos1, pos2 + 1, v);
    } else {
      ans = max(pos1 + pos2, ans);
      return;
    }
  }
  ans = max(pos1 + pos2, ans);
  return;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    cin >> ma[0][i];
  for (int i = 1; i <= m; i++)
    cin >> ma[1][i];
  dfs(0, 0, 0);
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  for (int T = 0; T < t; T++) {
    ll n, m, k;
    cin >> n >> m >> k;
    std::vector<ll> a(n), b(m);
    cin >> a[0];
    for (int i = 1; i < n; i++) {
      cin >> a[i];
      a[i] += a[i - 1];
    }
    cin >> b[0];
    for (int i = 1; i < m; i++) {
      cin >> b[i];
      b[i] += b[i - 1];
    }
    ll csum = 0, ans = 0;
    for (int i = 0; i <= n; i++) {
      if (i > 0)
        csum = a[i - 1];
      if (csum > k)
        break;
      ll er = upper_bound(b.begin(), b.end(), k - csum) - b.begin() - 1;
      ans = max(er + i + 1, ans);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  for (int T = 0; T < t; T++) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll a[n], b[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> b[i];
    }
    ll ans = 0, u = 0;
    ll q = 0, w = 0;
    while (ans < k) {
      if (q < n && w < m) {
        if (a[q] <= b[w]) {
          ans += a[q];
          q++;
          u++;
          if (ans > k) {
            ans -= a[q];
            u--;
            break;
          }
        }
        if (a[q] > b[w]) {
          ans += b[w];
          w++;
          u++;
          if (ans > k) {
            ans -= b[w];
            u--;
            break;
          }
        }
      } else if (q < n) {
        ans += a[q];
        q++;
        u++;
        if (ans > k) {
          ans -= a[q];
          u--;
          break;
        }
      } else if (w < m) {
        ans += b[w];
        w++;
        u++;
        if (ans > k) {
          ans -= b[w];
          u--;
          break;
        }
      } else
        break;
    }
    std::cout << u << std::endl;
  }
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, m, k, ans = 0, maxi = -1;
  bool out = false;
  cin >> n >> m >> k;
  int a[n + 1], b[m + 1];
  a[0] = 0;
  b[0] = 0;
  long long total = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (total + a[i] <= k && !out) {
      total += a[i];
      maxi = i;
    } else {
      out = true;
    }
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
  }
  for (int i = maxi, j = 0; i >= 0; i--) {
    while (j < m && total + b[j + 1] <= k) {
      total += b[++j];
    }
    ans = max(ans, i + j);
    total -= a[i];
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int a, b, i;
  cin >> a >> b;
  long long c;
  cin >> c;

  long long s[a + b];
  for (i = 0; i < a + b; i++) {
    cin >> s[i];
  }
  int sum = 0, l = 0;
  for (i = 0; i < a + b; i++) {
    sort(s, s + a + b);
    if (sum > c)
      continue;
    else
      sum = sum + s[i];
    l++;
  }
  if (sum > c)
    cout << l - 1;
  else
    cout << l;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring> //memset()
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define PI 3.14159265358979323846
#define IO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define rep(i, start, end) for (int i = start; i < end; i++)
#define per(i, start, end) for (int i = end - 1; i >= start; i--)
using namespace std;

const ll mod = 1000000007;
const ll inf = 1e9;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll leastbit(ll a) { return a & (-a); }
ll C(int a, int b) {
  ll res = 1;
  rep(i, 0, b) res = res * (a - i) / (i + 1);
  return res;
}
ll powmod(ll a, ll b) {
  if (b == 0)
    return 1;
  ll cnt = powmod(a, b / 2);
  (cnt *= cnt) %= mod;
  if (b & 1) {
    (cnt *= a) %= mod;
  }
  return cnt;
}

int n, m, k, a[200005], b[200005], idx = -1, ans = 0;
ll sum = 0;

int main() {
  IO
#ifndef ONLINE_JUDGE
      freopen("output.txt", "w", stdout);
#else
#endif

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

Error: WA

Fixed submission:

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

int main() {
  int n, m, i, num = 0;
  long long k, x;
  cin >> n >> m >> k;
  vector<long long> v1, v2;
  vector<long long>::iterator it1, it2;
  for (i = 0; i < n; i++) {
    cin >> x;
    v1.push_back(x);
  }
  for (i = 0; i < m; i++) {
    cin >> x;
    v2.push_back(x);
  }
  it1 = v1.begin();
  it2 = v2.begin();
  for (it1 = v1.begin(), it1 = v1.begin(); it1 != v1.end(), it2 != v2.end();) {
    if (*it1 <= k || *it2 <= k) {
      if (*it1 <= *it2) {
        num++;
        it1++;
      } else {
        num++;
        it2++;
      }
    } else
      break;
  }
  /*
  while (*it1 <=k || *it2 <=k)
  {
      if (*it1 <= k && *it1<=*it2)
      {
          num ++;
          v1.erase(it1);
      }
      else if (*it2 <=k && *it2 <=*it1)
      {
          num ++;
          v2.erase(it2);
      }
  }
  */
  cout << num << endl;
  return 0;
}
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;
  vector<ll> a(n), b(m), aSum(n + 1), bSum(m + 1);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    aSum[i + 1] = aSum[i] + a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
    bSum[i + 1] = bSum[i] + b[i];
  }
  // for (int i=0;i<=n;i++)cout<<aSum[i]<<" ";cout<<"\\n";
  // for (int i=0;i<=m;i++)cout<<bSum[i]<<" ";cout<<"\\n";
  for (ll i = 0; i <= n; i++) {
    ll dif = k - aSum[i];
    if (dif < 0)
      continue;
    if (dif == 0) {
      ans = max(ans, i);
      continue;
    }
    ll j = upper_bound(bSum.begin(), bSum.end(), dif) - bSum.begin() - 1;
    ans = max(ans, i + j);
  }
  cout << ans << "\\n";
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long int deskA, deskB, totalTime;
  cin >> deskA >> deskB >> totalTime;
  vector<long long int> timeA(deskA);
  vector<long long int> timeB(deskB);
  for (int i = 0; i < deskA; i++)
    cin >> timeA[i];
  for (int j = 0; j < deskB; j++)
    cin >> timeB[j];
  stack<long long int> sA;
  stack<long long int> sB;
  for (int i = deskA - 1; i >= 0; i--)
    sA.push(timeA[i]);
  for (int j = deskB - 1; j >= 0; j--)
    sB.push(timeB[j]);
  long long int maxBooks = 0;
  long long int remainingTime = totalTime;
  long long int value;
  if (deskA <= deskB)
    value = deskB;
  else
    value = deskA;
  for (int i = 0; i < value; i++) {
    if (sA.top() < sB.top() && sA.size() != 0) {
      remainingTime -= sA.top();
      sA.pop();
      if (remainingTime >= 0)
        maxBooks++;
      else
        break;
    } else {
      if (sB.size() != 0) {
        remainingTime -= sB.top();
        sB.pop();
        if (remainingTime >= 0)
          maxBooks++;
        else
          break;
      }
    }
  }
  cout << maxBooks;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, num = 0;
  long long k;
  cin >> n >> m >> k;
  long long a[n + 1], b[m + 1];
  memset(a, 0, (n + 1) * sizeof(a[0]));
  memset(b, 0, (m + 1) * sizeof(b[0]));
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i + 1];
    b[i + 1] += b[i];
  }
  for (int i = 0, j = m; i <= n;
       ++i) // iterate on all possible values of a & b and we start with zero to
            // consider the case of reading all from b
  {
    if (a[i] > k) // if a alone exceeds the sum then break
      break;
    while (a[i] + b[j] > k) // start with the largest value of b possible and
                            // decrease until you reach
      --j;
    num = std::max(
        i + j,
        num); // use max to store the max value of all possible combinations
  }
  cout << num;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int n, m, k, x, c(0);
  cin >> n >> m >> k;
  list<int> v1;
  list<int> v2;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v1.push_back(x);
  }
  for (int i = 0; i < m; i++) {
    cin >> x;
    v2.push_back(x);
  }
  if (v1.front() > k && v2.front() > k) {
    cout << 0;
    return 0;
  }
  while (k > 0) {
    if (v1.front() < v2.front()) {
      k = k - v1.front();
      v1.pop_front();
    } else {
      k = k - v2.front();
      v2.pop_front();
    }
    c++;
  }
  cout << c;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
const int Size = 200005;
long long Arr1[Size], Arr2[Size];
using namespace std;
int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> cumm1, cumm2;
  cumm1.push_back(0);
  cumm2.push_back(0);
  for (int i = 1; i <= N; i++) {
    cin >> Arr1[i];
    cumm1.push_back(cumm1[i - 1] + Arr1[i]);
  }
  for (int i = 1; i <= M; i++) {
    cin >> Arr2[i];
    cumm2.push_back(cumm2[i - 1] + Arr2[i]);
  }
  long long sum = 0;
  for (long long i = 0; i <= N; i++) {
    if (K >= cumm1[i]) {
      auto upper = upper_bound(cumm2.begin(), cumm2.end(), (K - cumm1[i]));
      long long idx = (upper - cumm2.begin()) - 1;
      sum = max(sum, idx + i);
    }
  }
  cout << sum;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define endl "\\n"
#define goat()                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cout.tie(0);                                                                 \\
  cin.tie(0);                                                                  \\
  srand(time(NULL))
typedef long long ll;
int arr[300000];
int main() {
  goat();
  ll n, m, k, i, x, y, z, c = 0, s = 0;
  cin >> n >> m >> k;
  for (i = 0; i < (n + m); i++) {
    cin >> arr[i];
  }
  sort(arr, arr + (n + m));
  for (i = 0; i < (n + m); i++) {
    s += arr[i];
    if (s <= k) {
      c++;
    } else {
      break;
    }
  }
  cout << c;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long k, n, m;
  cin >> n >> m >> k;
  vector<long long> v1(n + 2, 0);
  vector<long long> v2(m + 2, 0);
  for (int i = 1; i <= n; i++) {
    cin >> v1[i];
    v1[i] += v1[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> v2[i];
    v2[i] += v2[i - 1];
  }
  int j = m, val = 0;
  for (int i = 0; i <= n; i++) {
    if (v1[i] > k)
      break;
    while (v2[j] > (k - v1[i]))
      j--;
    val = max(val, (i + j));
  }
  cout << val << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main() {

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