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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (int)n; i++)
#define rep2(i, a, n) for (long long i = (a); i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
using ll = long long;
using vi = vector<int>;
using vs = vector<string>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using psi = pair<string, int>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// const ll mod = 1e9+7;
int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) { return a * b / gcd(a, b); }

ll N, M, K, H, W, L, R, X;
string S, T;

int main() {
  cin >> N >> M >> K;
  vll A(N + 1, 0), B(M + 1, 0);
  vll asum(N + 1, 0), bsum(M + 1, 0);

  rep2(i, 1, N + 1) {
    cin >> A[i];
    asum[i] = asum[i - 1] + A[i];
  }
  rep2(i, 1, M + 1) {
    cin >> B[i];
    bsum[i] = bsum[i - 1] + B[i];
  }
  ll ans = 0;
  ll bchk = M;
  for (ll i = 0; i <= N; i++) {
    if (asum[i] > K)
      break;
    for (ll j = bchk; j >= 0; j--) {
      if (asum[i] + bsum[j] > K)
        continue;
      else {
        ans = max(ans, i + j);
        bchk = j;
        break;
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (int)n; i++)
#define rep2(i, a, n) for (long long i = (a); i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
using ll = long long;
using vi = vector<int>;
using vs = vector<string>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using psi = pair<string, int>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// const ll mod = 1e9+7;
int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) { return a * b / gcd(a, b); }

ll N, M, K, H, W, L, R, X;
string S, T;

int main() {
  cin >> N >> M >> K;
  vll A(N + 1, 0), B(M + 1, 0);
  vll asum(N + 1, 0), bsum(M + 1, 0);

  rep2(i, 1, N + 1) {
    cin >> A[i];
    asum[i] = asum[i - 1] + A[i];
  }
  rep2(i, 1, M + 1) {
    cin >> B[i];
    bsum[i] = bsum[i - 1] + B[i];
  }
  ll ans = 0;
  ll bchk = 0;
  for (ll i = N; i >= 0; i--) {
    if (asum[i] > K)
      continue;
    for (ll j = bchk; j < M + 1; j++) {
      if (asum[i] + bsum[j] > K) {
        bchk = j;
        break;
      } else
        ans = max(ans, i + j);
    }
    if (bchk == M)
      break;
  }

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

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

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

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

  int64_t totaltime = 0;
  int64_t Bnum = 0;
  for (int64_t i = 0; i < N; ++i) {
    if (totaltime + A[i] > K)
      break;
    totaltime = totaltime + A[i];
    Bnum = Bnum + 1;
  }

  int64_t maxBnum = Bnum;
  int64_t Mini = 0;
  for (int64_t i = Bnum - 1; i >= 0; --i) {
    for (int64_t j = Mini; j < M; ++j) {
      if (totaltime + B[j] > K) {
        Mini = j;
        break;
      }
      totaltime = totaltime + B[j];
      Bnum = Bnum + 1;
    }
    // maxBnum = max(maxBnum,Bnum);
    if (Bnum > maxBnum)
      maxBnum = Bnum;
    Bnum = Bnum - 1;
    totaltime = totaltime - A[i];
  }
  for (int64_t j = Mini; j < M; ++j) {
    if (totaltime + B[j] > K) {
      Mini = j;
      break;
    }
    totaltime = totaltime + B[j];
    Bnum = Bnum + 1;
  }
  // maxBnum = max(maxBnum,Bnum);
  if (Bnum > maxBnum)
    maxBnum = Bnum;

  cout << maxBnum << endl;

  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

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

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

  int64_t totaltime = 0;
  int64_t Bnum = 0;
  for (int64_t i = 0; i < N; ++i) {
    if (totaltime + A[i] > K)
      break;
    totaltime = totaltime + A[i];
    Bnum = Bnum + 1;
  }

  int64_t maxBnum = Bnum;
  int64_t Mini = 0;
  for (int64_t i = Bnum - 1; i >= 0; --i) {
    for (int64_t j = Mini; j < M; ++j) {
      if (totaltime + B[j] > K) {
        Mini = j;
        break;
      }
      totaltime = totaltime + B[j];
      Bnum = Bnum + 1;
    }
    maxBnum = max(maxBnum, Bnum);
    Bnum = Bnum - 1;
    totaltime = totaltime - A[i];
  }
  for (int64_t j = Mini; j < M; ++j) {
    if (totaltime + B[j] > K) {
      Mini = j;
      break;
    }
    totaltime = totaltime + B[j];
    Bnum = Bnum + 1;
  }
  maxBnum = max(maxBnum, Bnum);

  cout << maxBnum << endl;

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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(begin, i, end) for (ll i = begin; i < (ll)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(200020), B(200020), A1(200020, 0), B1(200020, 0);

  // 入力と累積和の計算
  rep(0, i, N) {
    cin >> A.at(i);
    A1.at(i + 1) = A1.at(i) + A.at(i);
  }

  rep(0, i, M) {
    cin >> B.at(i);
    B1.at(i + 1) = B1.at(i) + B.at(i);
  }

  ll ans = 0, itr = M;
  for (ll i = 0; i <= N; i++) {
    // Aの机で読む本の数を決め打ち
    ll t = K - A1.at(i);

    // 時間が足りない、かつA1は単調増加列なのでbreak
    if (t < 0) {
      break;
    }

    for (ll j = itr; j >= 0; j--) {
      // 時間が足りない、かつB1は単調増加列なのでbreak
      // tは単調減少なのでこれ以降のtでj以上を見るのは無駄
      if (t >= B1.at(j)) {
        itr = j;
        ans = max(ans, i + j);
        break;
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(begin, i, end) for (ll i = begin; i < (ll)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  vector<ll> A(200020), B(200020), A1(200020, 0), B1(200020, 0);

  // 入力と累積和の計算
  rep(0, i, N) {
    cin >> A.at(i);
    A1.at(i + 1) = A1.at(i) + A.at(i);
  }

  rep(0, i, M) {
    cin >> B.at(i);
    B1.at(i + 1) = B1.at(i) + B.at(i);
  }

  ll ans = 0, itr = M;
  for (ll i = 0; i <= N; i++) {
    // Aの机で読む本の数を決め打ち
    ll t = K - A1.at(i);

    // 時間が足りない、かつA1は単調増加列なのでbreak
    if (t < 0) {
      break;
    }

    for (ll j = 0; j <= itr; j++) {
      // 時間が足りない、かつB1は単調増加列なのでbreak
      // tは単調減少なのでこれ以降のtでj以上を見るのは無駄
      if (t < B1.at(j)) {
        itr = j;
        break;
      } else {
        ans = max(ans, i + j);
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

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

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N >> M >> K;
  FOR(i, N) {
    cin >> x;
    A[i + 1] = A[i] + x;
  }
  FOR(i, M) {
    cin >> x;
    B[i + 1] = B[i] + x;
  }

  int ma = 0;
  FOR(i, N + 1) if (A[i] <= K) {
    x = lower_bound(B, B + M + 1, K - A[i] + 1) - B - 1;
    ma = max(ma, i + x);
  }
  cout << ma << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)


Fixed submission:

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

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

  int64_t restime = 0;
  int ai = 0, bi = 0;
  int c;
  while (restime <= k) {
    bool flaga = false;
    bool flagb = false;
    if (ai + 1 < n && bi + 1 < m) {
      c = min(a.at(ai), b.at(bi));
      if (c == a.at(ai))
        flaga = true;
      else
        flagb = true;
    } else if (ai + 1 == n && bi + 1 < m) {
      c = min(a.at(n), b.at(bi));
      if (c == a.at(n))
        flaga = true;
      else
        flagb = true;
      flagb = true;
    } else if (ai + 1 < n && bi + 1 == m) {
      c = min(a.at(ai), b.at(m));
      if (c == a.at(n))
        flaga = true;
      else
        flagb = true;
    } else if (ai + 1 == n && bi + 1 == m) {
      c = min(a.at(n), b.at(m));
      if (c == a.at(n))
        flaga = true;
      else
        flagb = true;
    } else
      break;

    if (k < c - restime)
      break;
    if (flaga)
      ai++;
    else if (flagb)
      bi++;
    restime += c;
  }
  cout << ai + bi << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
namespace IO {
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }

template <typename T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void writesp(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\\n');
}
} // namespace IO
using namespace IO;
const int N = 2e5 + 5;
int n, m, k, a[N], b[N];
ll suma[N], sumb[N];
int main() {
  read(n);
  read(m);
  read(k);
  for (int i = 1; i <= n; i++)
    read(a[i]);

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

  for (int i = 1; i <= n; i++)
    suma[i] = suma[i - 1] + a[i];
  for (int i = 1; i <= m; i++)
    sumb[i] = sumb[i - 1] + b[i];

  int ans = 0;
  int npos = m;
  for (int i = 0; i <= n; i++) {
    while (npos >= 0 && suma[i] + sumb[npos] > k)
      npos--;
    if (suma[i] + sumb[npos] <= k)
      chkmax(ans, i + npos);
  }

  writeln(ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
namespace IO {
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }

template <typename T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void writesp(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\\n');
}
} // namespace IO
using namespace IO;
const int N = 2e5 + 5;
int n, m, k, a[N], b[N];
ll suma[N], sumb[N];
int main() {
  read(n);
  read(m);
  read(k);
  for (int i = 1; i <= n; i++)
    read(a[i]);

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

  for (int i = 1; i <= n; i++)
    suma[i] = suma[i - 1] + a[i];
  for (int i = 1; i <= m; i++)
    sumb[i] = sumb[i - 1] + b[i];

  int ans = 0;
  int npos = m;
  for (int i = 1; i <= n; i++) {
    while (suma[i] + sumb[npos] > k)
      npos--;
    if (suma[i] + sumb[npos] <= k)
      chkmax(ans, i + npos);
  }

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

#include <iostream>
using namespace std;
inline long long max(long long a, long long b) { return (a > b) ? a : b; }
int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  long long sum = 0, t;
  long long asum[n + 1], bsum[m + 1];
  int nmax = 0, mmax = 0;
  asum[0] = 0;
  bsum[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> t;
    if (sum > k) {
      continue;
    }
    sum += t;
    if (sum > k) {
      continue;
    }
    asum[i + 1] = sum;
    nmax = i + 1;
  }
  sum = 0;
  for (int i = 0; i < m; i++) {
    cin >> t;
    if (sum > k) {
      continue;
    }
    sum += t;
    if (sum > k) {
      continue;
    }
    bsum[i + 1] = sum;
    mmax = i + 1;
  }
  long long max_count = 0;
  int j_before = mmax;
  for (int i = 0; i <= nmax; i++) {
    sum = asum[i];
    long long _remaining = k - sum;
    if (_remaining == 0) {
      max_count = max(max_count, i);
      break;
    } else if (_remaining < 0) {
      break;
    }
    int j;
    for (j = j_before; j > 0; j--) {
      if (bsum[j] <= _remaining)
        break;
    }
    max_count = max(max_count, i + j);
    j_before = j;
  }
  cout << max_count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
inline long long max(long long a, long long b) { return (a > b) ? a : b; }
int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  long long sum = 0, t;
  long long asum[n + 1], bsum[m + 1];
  int nmax = 0, mmax = 0;
  asum[0] = 0;
  bsum[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> t;
    if (sum > k) {
      continue;
    }
    sum += t;
    if (sum > k) {
      continue;
    }
    asum[i + 1] = sum;
    nmax = i + 1;
  }
  sum = 0;
  for (int i = 0; i < m; i++) {
    cin >> t;
    if (sum > k) {
      continue;
    }
    sum += t;
    if (sum > k) {
      continue;
    }
    bsum[i + 1] = sum;
    mmax = i + 1;
  }
  long long max_count = 0;
  for (int i = 0; i <= nmax; i++) {
    sum = asum[i];
    long long _remaining = k - sum;
    if (_remaining == 0)
      max_count = max(max_count, i);
    else if (_remaining < 0)
      break;
    int j;
    for (j = 0; j <= mmax; j++) {
      if (bsum[j] > _remaining)
        break;
    }
    j--;
    max_count = max(max_count, i + j);
  }
  cout << max_count << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be(), (a).en()
#define REP(i, n) for (int i = 0; i < (n); i++)         // repeat n times
#define REP2(i, s, n) for (int i = (s); i < (n); i++)   // repeat from s to n
#define REPD(i, n) for (int i = (n); i >= 0; i--)       // repeat from n to 0
#define REPD2(i, s, e) for (int i = (s); i >= (e); i--) // repeat from s to e
#define RANGE(i, v) for (auto &i : v)                   // repeat range
#define ASIZE(a) (sizeof(a) / sizeof(a[0]))             // array size

using LL = long long;

template <typename T> using V = vector<T>;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

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];

  Vll A_time(N + 1);
  REP2(i, 1, N + 1) A_time[i] = A_time[i - 1] + A[i - 1];

  Vll B_time(M + 1);
  REP2(i, 1, M + 1) B_time[i] = B_time[i - 1] + B[i - 1];

  int max_book = 0;
  int j = M;
  REP(i, N + 1) {
    if (A_time[i] > K)
      break;
    while (A_time[i] + B_time[j] > K)
      j--;
    max_book = max(max_book, i + j);
  }

  cout << max_book << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be(), (a).en()
#define REP(i, n) for (int i = 0; i < (n); i++)         // repeat n times
#define REP2(i, s, n) for (int i = (s); i < (n); i++)   // repeat from s to n
#define REPD(i, n) for (int i = (n); i >= 0; i--)       // repeat from n to 0
#define REPD2(i, s, e) for (int i = (s); i >= (e); i--) // repeat from s to e
#define RANGE(i, v) for (auto &i : v)                   // repeat range
#define ASIZE(a) (sizeof(a) / sizeof(a[0]))             // array size

using LL = long long;

template <typename T> using V = vector<T>;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

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];

  Vll A_time(N + 1);
  REP2(i, 1, N + 1) A_time[i] = A_time[i - 1] + A[i - 1];

  Vll B_time(M + 1);
  REP2(i, 1, M + 1) B_time[i] = B_time[i - 1] + B[i - 1];

  int max_book = 0;
  REP(i, N + 1) {
    REP(j, M + 1) {
      if (A_time[i] + B_time[j] > K)
        break;
      max_book = max(max_book, i + j);
    }
    if (A_time[i] > K)
      break;
  }

  cout << max_book << endl;

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

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

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N), B(M);
  ll A_sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A_sum += A[i];
  }
  for (int i = 0; i < M; i++)
    cin >> B[i];

  int num = 0, su = 0;
  ll B_sum = 0;
  for (int i = N; i >= 0; i--) {
    for (int j = num; j <= M; j++) {
      num = j;
      if (A_sum + B_sum > K)
        break;
      su = max(su, i + j);
      if (j == M)
        break;
      B_sum += B[j];
    }
    if (i == 0)
      break;
    A_sum -= A[i - 1];
  }
  cout << su << endl;
}

Error: TLE

Fixed submission:

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

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

void func(int &ans, int A_read, int B_read, int time, bool &flag) {
  if (A_read == N && B_read == M) {
    ans = max(ans, A_read + B_read);
    return;
  }
  if (time > K) {
    ans = max(ans, A_read + B_read - 1);
    return;
  }

  if (A_read < N && flag)
    func(ans, A_read + 1, B_read, time + A[A_read], flag);
  flag = false;
  if (B_read < M)
    func(ans, A_read, B_read + 1, time + B[B_read], flag);
  return;
}

int main() {
  cin >> N >> M >> K;
  A.resize(N);
  B.resize(M);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++)
    cin >> B[i];
  int ans = 0;
  bool flag = true;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  rep1(i, n) {
    if (i == 1)
      cin >> a[i];
    else {
      cin >> a[i];
      a[i] += a[i - 1];
    }
  }
  vector<ll> b(m + 1);
  rep1(i, m) {
    if (i == 1)
      cin >> b[i];
    else {
      cin >> b[i];
      b[i] += b[i - 1];
    }
  }
  ll ans = 0;
  rep(i, n + 1) { // aのサーチ
    ll target = k - a[i];
    // cout << i << " " << target << " ";
    // for(auto p : b) cout << p << ",";
    ll b_num = upper_bound(all(b), target) - begin(b) - 1;
    ll tmp = i + b_num;
    ll val = a[i] + *(upper_bound(all(b), target) - 1);
    // cout << " " << tmp << " " << val;
    // cout << endl;
    if (val <= k)
      ans = max(ans, tmp);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n + 1);
  rep1(i, n) {
    if (i == 1)
      cin >> a[i];
    else {
      cin >> a[i];
      a[i] += a[i - 1];
    }
  }
  vector<ll> b(m + 1);
  rep1(i, m) {
    if (i == 1)
      cin >> b[i];
    else {
      cin >> b[i];
      b[i] += b[i - 1];
    }
  }
  ll ans = 0;
  rep(i, n + 1) { // aのサーチ
    rep(j, m + 1) {
      // cout << i+j << "," << a[i]+b[j] << endl;
      if (a[i] + b[j] <= k)
        ans = max(ans, i + j);
    }
  }
  cout << ans << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// int ar[200005][200005];

vector<ll> a;
vector<ll> b;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll i, j, k, l, m, n;

  cin >> n >> m >> k;

  a.push_back(0);
  b.push_back(0);

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

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

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

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

  ll maxi = -1, best_j = m, indexj;

  for (i = 0; i <= n; i++) {
    for (j = best_j; j >= 0; j--) {
      indexj = j;
      // cout << i << " " << j << '\\n';
      // cout << (((a[i]-a[0])+(b[j]-b[0]))) << '\\n';
      if (((a[i] - a[0]) + (b[j] - b[0])) <= k) {
        maxi = max(maxi, i + j);
        break;
      }
    }

    best_j = indexj;
  }

  cout << maxi << '\\n';
}

Error: TLE

Fixed submission:

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

// int ar[200005][200005];

vector<ll> a;
vector<ll> b;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll i, j, k, l, m, n;

  cin >> n >> m >> k;

  a.push_back(0);
  b.push_back(0);

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

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

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

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

  ll maxi = -1, best_j = m, indexj;

  for (i = 0; i <= n; i++) {
    for (j = 0; j <= best_j; j++) {
      indexj = j;
      // cout << i << " " << j << '\\n';
      // cout << (((a[i]-a[0])+(b[j]-b[0]))) << '\\n';
      if (((a[i] - a[0]) + (b[j] - b[0])) <= k) {
        maxi = max(maxi, i + j);

      } else {
        // cout << i << " " << j << '\\n';
        break;
      }
    }

    best_j = indexj;
  }

  cout << maxi << '\\n';
}