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

#include <bits/stdc++.h>

using namespace std;

void input(int &N, int &M, int &K, vector<int> &deskA, vector<int> &deskB) {
  int a, b;
  cin >> N >> M >> K;
  deskA.resize(N + 1, 0);
  deskB.resize(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> a;
    deskA[i] = deskA[i - 1] + a;
  }
  for (int i = 1; i <= M; i++) {
    cin >> b;
    deskB[i] = deskB[i - 1] + b;
  }
}

void findans(int &N, int &M, int &K, vector<int> &deskA, vector<int> &deskB,
             int &maximum) {
  for (int i = 0; i <= N; i++)
    if (K - deskA[i] >= 0)
      for (int j = max(maximum - i, 0); j <= M; j++)
        if (K - deskA[i] - deskB[j] >= 0) {
          if (i + j > maximum)
            maximum = i + j;
        } else
          break;
    else
      break;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M, K, maximum = 0;
  vector<int> deskA, deskB;
  input(N, M, K, deskA, deskB);
  findans(N, M, K, deskA, deskB, maximum);
  cout << maximum << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void input(int &N, int &M, int &K, vector<int> &deskA, vector<int> &deskB) {
  cin >> N >> M >> K;
  deskA.resize(N);
  deskB.resize(M);
  for (int i = 0; i < N; i++)
    cin >> deskA[i];
  for (int i = 0; i < M; i++)
    cin >> deskB[i];
}

void findans(vector<int> &deskA, vector<int> &deskB, int K, unsigned indexa,
             unsigned indexb, int cnt, int &maximum) {
  if (cnt > maximum)
    maximum = cnt;
  if (indexa < deskA.size() && K - deskA[indexa] >= 0)
    findans(deskA, deskB, K - deskA[indexa], indexa + 1, indexb, cnt + 1,
            maximum);
  if (indexb < deskB.size() && K - deskB[indexb] >= 0)
    findans(deskA, deskB, K - deskB[indexb], indexa, indexb + 1, cnt + 1,
            maximum);
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M, K, maximum = 0;
  vector<int> deskA, deskB;
  input(N, M, K, deskA, deskB);
  findans(deskA, deskB, K, 0, 0, 0, maximum);
  cout << maximum << '\\n';
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
LL s, t, n, m;
const int maxn = 2e6 + 5;
il void chkmax(int &a, int b) { a = a > b ? a : b; }
il void chkmin(int &a, int b) { a = a < b ? a : b; }
il int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -f;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
il void write(int x) {
  char c[33] = {0}, tot = 0;
  if (x == 0) {
    puts("0");
    return;
  }
  while (x) {
    c[++tot] = x % 10 + '0';
    x /= 10;
  }
  while (tot) {
    putchar(c[tot--]);
  }
  return;
}
LL a[maxn], b[maxn], dp[maxn];
int main() {
  n = read(), m = read(), t = read();
  for (int i = 1; i <= n; i++)
    a[i] = read() + a[i - 1];
  for (int i = 1; i <= m; i++)
    b[i] = read() + b[i - 1];
  LL j = m;
  s = 0;
  for (int i = 0; i <= n; i++) {
    while (a[i] + b[j] > t && j)
      j--;
    if (i + j > s && a[i] <= t)
      s = i + j;
  }
  cout << s;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
int s, t, n, m;
const int maxn = 2e6 + 5;
il void chkmax(int &a, int b) { a = a > b ? a : b; }
il void chkmin(int &a, int b) { a = a < b ? a : b; }
il int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -f;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
il void write(int x) {
  char c[33] = {0}, tot = 0;
  if (x == 0) {
    puts("0");
    return;
  }
  while (x) {
    c[++tot] = x % 10 + '0';
    x /= 10;
  }
  while (tot) {
    putchar(c[tot--]);
  }
  return;
}
LL a[maxn], b[maxn], dp[maxn];
int main() {
  n = read(), m = read(), t = read();
  for (int i = 1; i <= n; i++)
    a[i] = read() + a[i - 1];
  for (int i = 1; i <= m; i++)
    b[i] = read() + b[i - 1];
  int j = m;
  s = 0;
  for (int i = 1; i <= n; i++) {
    while (a[i] + b[j] > t)
      j--;
    if (j <= 0)
      break;
    if (i + j > s)
      s = i + j;
  }
  cout << s;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, M;
ll K;
ll A[200010];
ll B[200010];

int main() {
  cin >> N >> M >> K;
  // 0インデックスには何も読まない0という初期値をそのまま入れておく
  rep(i, N) cin >> A[i + 1];
  rep(i, M) cin >> B[i + 1];

  rep(i, N) A[i + 1] += A[i];
  rep(i, M) B[i + 1] += B[i];

  int ans = 0;
  int cnt_b = M;
  rep(cnt_a, N + 1) {
    if (A[cnt_a] > K)
      continue;
    while (A[cnt_a] + B[cnt_b] > K) {
      cnt_b--;
    }
    // cout << ans << endl;
    ans = max(ans, cnt_a + cnt_b);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  long long A[N + 1];
  long long B[M + 1];
  long long SA[N + 1];
  long long SB[M + 1];
  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }
  SA[0] = 0;
  SB[0] = 0;
  // SA[N] = pow(10, 9) + 1;
  // SB[M] = pow(10, 9) + 1;
  rep(i, N) SA[i + 1] = SA[i] + A[i];
  rep(i, M) SB[i + 1] = SB[i] + B[i];
  long long ans = 0;
  long long book = 0;
  // long long cnt_a = 1;
  // long long cnt_b = 1;

  rep(i, N + 1) {
    if (SA[i] > K)
      break;
    long long books = i;
    ans = max(ans, books);
    rep(j, M + 1) {
      if (SB[j] > K)
        break;
      if ((SA[i] + SB[j]) > K)
        break;
      if (j == 0)
        continue;
      if (SA[i] + SB[j] <= K)
        books++;
      ans = max(ans, books);
    }
  }

  cout << ans << endl;

  // while (true){
  // if (SA[cnt_a] >= SB[cnt_b]){
  //   ans += B[cnt_b-1];
  //   if (ans > K){
  //     ans -= B[cnt_b-1];
  //     break;
  //   }
  //   cnt_b++;
  //   book++;
  //   if (book == N + M) break;
  // }
  // if (SA[cnt_a] < SB[cnt_b]){
  //   ans += A[cnt_a-1];
  //   if (ans > K){
  //     ans -= A[cnt_a-1];
  //     break;
  //   }
  //   cnt_a++;
  //   book++;
  //   if (book == N + M) break;
  // }
  // }
  // cout << book << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, M;
ll K;
vector<ll> A;
vector<ll> B;

bool is_ok(int cnt_a, int mid) { return A[cnt_a] + B[mid] <= K; }

int binary_search(int cnt_a) {
  int left = 0;         // 配列の左端
  int right = B.size(); // 配列の右端
  // 両端の差が1になるまで続ける
  while (right - left > 1) {
    int mid = left + (right - left) / 2; // 区間の真ん中
    if (is_ok(cnt_a, mid))
      left =
          mid; // もし調べている対象が「is_okの条件」に当てはまればleftをmidまで移動
    else
      right = mid; // 当てはまらなければrightをmidまで移動
  }
  return left;
}

int main() {
  cin >> N >> M >> K;
  // 0インデックスには何も読まない0という初期値をそのまま入れておく
  A.push_back(0);
  B.push_back(0);

  rep(i, N) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  rep(i, M) {
    ll b;
    cin >> b;
    B.push_back(b);
  }

  rep(i, N) A[i + 1] += A[i];
  rep(i, M) B[i + 1] += B[i];

  int ans = 0;
  int cnt_b;
  rep(cnt_a, N + 1) {
    if (A[cnt_a] > K)
      continue;
    cnt_b = binary_search(cnt_a);
    ans = max(ans, cnt_a + cnt_b);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int N, M;
ll K;
vector<ll> A;
vector<ll> B;

bool is_ok(int cnt_a, int mid) { return A[cnt_a] + B[mid] <= K; }

int binary_search(vector<ll> B, int cnt_a) {
  int left = -1;        // 配列の左端
  int right = B.size(); // 配列の右端
  // 両端の差が1になるまで続ける
  while (right - left > 1) {
    int mid = left + (right - left) / 2; // 区間の真ん中
    if (is_ok(cnt_a, mid))
      left =
          mid; // もし調べている対象が「is_okの条件」に当てはまればleftをmidまで移動
    else
      right = mid; // 当てはまらなければrightをmidまで移動
  }
  return left;
}

int main() {
  cin >> N >> M >> K;
  // 0インデックスには何も読まない0という初期値をそのまま入れておく
  A.push_back(0);
  B.push_back(0);

  rep(i, N) {
    ll a;
    cin >> a;
    A.push_back(a);
  }
  rep(i, M) {
    ll b;
    cin >> b;
    B.push_back(b);
  }

  rep(i, N) A[i + 1] += A[i];
  rep(i, M) B[i + 1] += B[i];

  int ans = 0;
  int cnt_b;
  rep(cnt_a, N + 1) {
    if (A[cnt_a] > K)
      continue;
    cnt_b = binary_search(B, cnt_a);
    ans = max(ans, cnt_a + cnt_b);
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#include <string>
// #include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
typedef long long int ll;
using namespace std;
ll mod = 1000000007;
// ll mod=67280421310721;
// ll mod=998244353;
ll INF = 1e18;
ll po(ll a, ll b) {
  ll re = 1;
  while (b) {
    if (b % 2 == 1) {
      re = (re * a);
      re %= mod;
    }
    a = (a * a);
    a %= mod;
    b >>= 1;
  }
  return re;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  //	cin>>test;
  while (test--) {
    int n, m;
    ll k;
    cin >> n >> m >> k;
    ll a[n + 1], b[m + 1];
    a[0] = 0;
    b[0] = 0;
    FOR(i, 1, n + 1) {
      cin >> a[i];
      a[i] += a[i - 1];
    }
    FOR(i, 1, m + 1) {
      cin >> b[i];
      b[i] += b[i - 1];
    }
    int ans = 0;
    int j = m;
    FOR(i, 0, n + 1) {
      if (k < a[i])
        break;
      while (k < (a[i] + b[j]))
        j--;
      ans = max(ans, i + j);
    }
    cout << ans;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#include <string>
// #include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
typedef long long int ll;
using namespace std;
ll mod = 1000000007;
// ll mod=67280421310721;
// ll mod=998244353;
ll INF = 1e18;
ll po(ll a, ll b) {
  ll re = 1;
  while (b) {
    if (b % 2 == 1) {
      re = (re * a);
      re %= mod;
    }
    a = (a * a);
    a %= mod;
    b >>= 1;
  }
  return re;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  //	cin>>test;
  while (test--) {
    int n, m;
    ll k;
    cin >> n >> m >> k;
    ll a[n + 1], b[n + 1];
    a[0] = 0;
    b[0] = 0;
    FOR(i, 1, n + 1) {
      cin >> a[i];
      a[i] += a[i - 1];
    }
    FOR(i, 1, m + 1) {
      cin >> b[i];
      b[i] += b[i - 1];
    }
    a[0] = 0;
    int ans = 0;
    int j = m;
    FOR(i, 0, n + 1) {
      if (k < a[i])
        break;
      while (k < (a[i] + b[j])) {
        j--;
      }
      ans = max(ans, i + j);
    }
    cout << ans;
  }
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

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

  int64_t N, M, K;
  std::cin >> N >> M >> K;

  std::vector<int64_t> A(N + 1, 0);
  for (int64_t i = 0; i != N; i++) {
    std::cin >> A[i + 1];
    A[i + 1] += A[i];
  }

  std::vector<int64_t> B(M + 1, 0);
  for (int64_t i = 0; i != M; i++) {
    std::cin >> B[i + 1];
    B[i + 1] += B[i];
  }

  int64_t result = 0;
  int64_t j = M;
  for (int64_t i = 0; i <= N; i++) {
    if (A[i] > K)
      break;

    for (; j >= 0; j--) {
      if (B[j] > K)
        continue;

      if (A[i] + B[j] <= K) {
        result = std::max(result, i + j);
        break;
      }
    }
  }

  std::cout << result << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

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

  int64_t N, M, K;
  std::cin >> N >> M >> K;

  std::vector<int64_t> A(N + 1, 0);
  for (int64_t i = 0; i != N; i++) {
    std::cin >> A[i + 1];
    A[i + 1] += A[i];
  }

  std::vector<int64_t> B(M + 1, 0);
  for (int64_t i = 0; i != M; i++) {
    std::cin >> B[i + 1];
    B[i + 1] += A[i];
  }

  int64_t result = 0;
  int64_t j = M;
  for (int64_t i = 0; i <= N; i++) {
    if (A[i] > K)
      break;

    for (; j != -1; j--) {
      if (B[j] > K)
        continue;

      if (A[i] + B[j] <= K) {
        result = std::max(result, i + j);
        break;
      }
    }
  }

  std::cout << result << std::endl;

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

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

typedef long long int ll;
using ii = pair<ll, ll>;
#define F first
#define S second
#define MP make_pair
#define PB push_back
// using min_of_3=min({ll a,ll b,ll c});

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

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

  vector<ll> a(n + 1, 0);
  vector<ll> b(m + 1, 0);

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

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

  ll count = 0;

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

    if (sum < 0) {
      break;
    }

    else {

      ll j = upper_bound(b.begin(), b.end(), k - a[i]) - b.begin();
      if (b[j - 1] <= k - a[i]) {
        count = max(count, i + j - 1);
        // k = k-a[i]-b[j-1];
      }
    }
  }

  cout << count << "\\n";
}


Error: 0

Fixed submission:

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

typedef long long int ll;
using ii = pair<ll, ll>;
#define F first
#define S second
#define MP make_pair
#define PB push_back
// using min_of_3=min({ll a,ll b,ll c});

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

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

  queue<ll> a;
  queue<ll> b;

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

  for (ll i = 0; i < m; i++) {
    ll x;
    cin >> x;
    b.push(x);
  }

  ll count = 0;
  ll cost = 0;

  if (min(a.back(), b.back()) > k) {
    cout << 0 << "\\n";
  }

  else {

    while (cost <= k && ((!a.empty()) || (!b.empty()))) {
      if (a.back() <= b.back()) {
        cost += a.back();
        count++;
        a.pop();
      }

      else {
        cost += b.back();
        count++;
        b.pop();
      }
    }

    cout << count << "\\n";
  }
}
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 drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
using ll = long long;

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N + 1), B(M + 1);
  rep(i, N) {
    int a;
    cin >> a;
    A[i + 1] = a + A[i];
  }
  rep(i, M) {
    int b;
    cin >> b;
    B[i + 1] = b + B[i];
  }

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

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(N + 1), B(M + 1);
  rep(i, N) {
    int a;
    cin >> a;
    A[i + 1] = a + A[i];
  }
  rep(i, M) {
    int b;
    cin >> b;
    B[i + 1] = b + B[i];
  }

  int ans = 0;
  rep(i, N + 1) {
    if (A[i] > K)
      break;
    rep(j, M + 1) {
      if (B[j] > K - A[i])
        break;
      ans = max(ans, i + j);
    }
  }

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

#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

typedef long long int ll;

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<ll> as(n + 1), bs(m + 1);
  for (int i = 1; i < n + 1; i++)
    as[i] = as[i - 1] + a[i - 1];
  for (int i = 1; i < m + 1; i++)
    bs[i] = bs[i - 1] + b[i - 1];

  int val = 0;
  for (int i = 0; i < n + 1; i++) {
    if (k < as[i])
      break;
    int j = lower_bound(bs.begin(), bs.end(), k - as[i] + 1) - bs.begin() - 1;
    //      int j = 0;
    //      for (j = 0; j < m; j++) {
    //          if (bs[j + 1] > k - as[i]) break;
    //      }

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

  cout << val << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

typedef long long int ll;

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<ll> as(n + 1), bs(m + 1);
  for (int i = 1; i < n + 1; i++)
    as[i] = as[i - 1] + a[i - 1];
  for (int i = 1; i < m + 1; i++)
    bs[i] = bs[i - 1] + b[i - 1];

  int val = 0;
  for (int i = 0; i < n + 1; i++) {
    if (k < as[i])
      break;
    int j = 0;
    for (j = 0; j < m; j++) {
      if (bs[j + 1] > k - as[i])
        break;
    }
    val = max(val, i + j);
  }

  cout << val << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define N 0
#define INF 0x3f3f3f3f
#define LL long long
#define mz(x) ms(x, 0)
#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcopy(x, y, sizeof(y))
#define MOD 1000000007

using namespace std;

int a[200010], b[200010];
int sa[200010], sb[200010];

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    sa[i] = (i > 0 ? sa[i - 1] : 0) + a[i];
  }

  for (int i = 0; i < m; i++) {
    scanf("%d", b + i);
    sb[i] = (i > 0 ? sb[i - 1] : 0) + b[i];
  }

  int rda = 0, rdb = 0;
  int ai = 0, bi = 0;
  while ((ai < n && k >= a[ai]) || (bi < m && k >= b[bi])) {
    if (ai >= n) {
      k -= b[bi];
      rdb += b[bi++];
    } else if (bi >= m) {
      k -= a[ai];
      rda += a[ai++];
    } else {
      int ans1 = upper_bound(sa, sa + n, k + rda) - sa - ai;
      int ans2 = upper_bound(sb, sb + m, k + rdb) - sb - bi;
      //			cout << ans1 << " " << ans2 << endl ;
      if (ans1 > ans2) {
        k -= a[ai];
        rda += a[ai++];
      } else if (ans1 < ans2) {
        k -= b[bi];
        rdb += b[bi++];
      } else {
        if (a[ai] < b[bi]) {
          k -= a[ai];
          rda += a[ai++];
        } else {
          k -= b[bi];
          rdb += b[bi++];
        }
      }
    }

    //		cout << k << endl ;
  }

  cout << ai + bi << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define N 0
#define INF 0x3f3f3f3f
#define LL long long
#define mz(x) ms(x, 0)
#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcopy(x, y, sizeof(y))
#define MOD 1000000007

using namespace std;

typedef struct {
  int num = -1;
} MYINT;

map<int, map<int, map<int, MYINT>>> dp; // k, i, j
int n, m;
int a[200010], b[200010];

int dfs(int k, int i, int j) {
  //	if (dp.find(k) != dp.end() && dp[k].find(i) != dp[k].end() &&
  //dp[k][i].find(j) != dp[k][i].end()) 		return dp[k][i][j] ;

  int ans = dp[k][i][j].num;
  if (ans != -1)
    return ans;

  int ans1 = 0, ans2 = 0;
  if (i < n && k >= a[i])
    ans1 = dfs(k - a[i], i + 1, j) + 1;
  if (j < m && k >= b[j])
    ans2 = dfs(k - b[j], i, j + 1) + 1;

  //	if (dp.find(k) == dp.end())
  //		dp[k] = map<int, map <int, int> > () ;
  //	if (dp[k].find(i) == dp[k].end())
  //		dp[k][i] = map <int, int> () ;
  ans = dp[k][i][j].num = max(ans1, ans2);

  return ans;
}

int main() {
  int k;
  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);

  cout << dfs(k, 0, 0) << endl;

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

#include <bits/stdc++.h>
#include <time.h>
using namespace std;
#include <math.h>
#define ll long long
// to increase precision
// std::cout<<std::fixed;
// 	std::cout<<std::setprecision(6)<<z;
// clock_t tStart = clock();
// printf("Time taken: %.9fs\\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
#define maxn 100001

#define ll long long

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long int> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[i + 1] = a[i] + t;
  }
  for (int i = 0; i < m; ++i) {
    int t;
    cin >> t;
    b[i + 1] = b[i] + t;
  }
  // for(auto x:a)
  // 	cout<<x<<" ";
  // cout<<endl;
  // for(auto x:b)
  // 	cout<<x<<" ";
  // cout<<endl;
  int ans = 0;
  int i = 0;
  while (b[i] <= k && i <= m) {
    long long int res = k - b[i];
    // cout<<res<<endl;
    int ind = upper_bound(a.begin(), a.end(), res) - a.begin();
    // cout<<a[ind]<<endl;
    ans = max(ans, i + ind - 1);
    // cout<<i<<" "<<ind<<endl;
    i++;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <time.h>
using namespace std;
#include <math.h>
#define ll long long
// to increase precision
// std::cout<<std::fixed;
// 	std::cout<<std::setprecision(6)<<z;
// clock_t tStart = clock();
// printf("Time taken: %.9fs\\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
#define maxn 100001

#define ll long long

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<long long int> a(n + 1), b(m + 1);
  a[0] = 0;
  b[0] = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[i + 1] = a[i] + t;
  }
  for (int i = 0; i < m; ++i) {
    int t;
    cin >> t;
    b[i + 1] = b[i] + t;
  }
  // for(auto x:a)
  // 	cout<<x<<" ";
  // cout<<endl;
  // for(auto x:b)
  // 	cout<<x<<" ";
  // cout<<endl;
  int ans = 0;
  int i = 0;
  while (b[i] <= k) {
    long long int res = k - b[i];
    // cout<<res<<endl;
    int ind = upper_bound(a.begin(), a.end(), res) - a.begin();
    // cout<<a[ind]<<endl;
    ans = max(ans, i + ind - 1);
    // cout<<i<<" "<<ind<<endl;
    i++;
  }
  cout << ans << endl;
}