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

#include <bits/stdc++.h>

#define f(i, a, b) for (ll i = a; i < b; i++)
#define af(i, a, b) for (ll i = a; i >= b; i--)
#define rep(i, a, b, k) for (ll i = a; i < b; i += k)
#define arep(i, a, b, k) for (ll i = a; i >= b; i -= k)
#define ones(x) (ll) __builtin_popcount(x)
#define fs first
#define sc second
#define pb push_back
#define po pop_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort(a.begin(), a.end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define ller                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tsie(nullptr);                                                           \\
  cout.tie(nullptr)

#define PI 3.1415926535
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
const ll mod = 1e9 + 7;
const ll MAX = 2e5 + 7;
const ll inf = 1e9 + 7;

int main() {
  fastio;
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 1] = {0};
  f(i, 1, n + 1) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  ll b[m + 1] = {0};
  f(i, 1, m + 1) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  ll ans = 0;
  f(i, 0, n + 1) {
    if (k >= a[i])
      ans = max(ans, i + ll(upper_bound(b, b + m + 1, k - a[i]) - b - 1));
  }
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int book[3][200010];
int max = 0;
int aMax, bMax;

void read(int aIndex, int bIndex, int remains, int counter) {
  bool bisa = 0;
  if (remains >= book[1][aIndex] && aIndex <= aMax) {
    read(aIndex + 1, bIndex, remains - book[1][aIndex], counter + 1);
    bisa = 1;
  }
  if (remains >= book[2][bIndex] && bIndex <= bMax) {
    read(aIndex, bIndex + 1, remains - book[2][aIndex], counter + 1);
    bisa = 1;
  }
  if (!bisa) {
    max = std::max(max, counter);
  }
}

int main() {
  int remains;
  scanf("%d %d %d", &aMax, &bMax, &remains);
  for (int i = 1; i <= aMax; i++) {
    scanf("%d", &book[1][i]);
  }
  for (int i = 1; i <= bMax; i++) {
    scanf("%d", &book[2][i]);
  }
  read(1, 1, remains, 0);
  printf("%d\\n", max);
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N), B(M);
  vector<long long> sumA(N + 1, 0), sumB(M + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < N + 1; i++) {
    if (i == 0) {
      sumA.at(i) = 0;
    } else {
      sumA.at(i) = sumA.at(i - 1) + A.at(i - 1);
    }
  }
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
  }
  for (int i = 0; i < M + 1; i++) {
    if (i == 0) {
      sumB.at(i) = 0;
    } else {
      sumB.at(i) = sumB.at(i - 1) + B.at(i - 1);
    }
  }

  int ans = 0;
  int j = M;
  for (int i = 0; i < N + 1; i++) {
    if (sumA.at(i) > K) {
      break;
    }
    while (K - sumA.at(i) < sumB.at(j)) {
      j--;
    }
    ans = max(ans, i + j);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N), B(M);
  vector<long long> sumA(N, 0), sumB(M, 0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (i == 0) {
      sumA.at(i) = A.at(i);
    } else {
      sumA.at(i) = sumA.at(i - 1) + A.at(i);
    }
  }
  for (int i = 0; i < M; i++) {
    cin >> B.at(i);
    if (i == 0) {
      sumB.at(i) = B.at(i);
    } else {
      sumB.at(i) = sumB.at(i - 1) + B.at(i);
    }
  }

  int ans = 0;
  int j = M;
  for (int i = 1; i <= N; i++) {
    if (K >= sumA.at(i - 1)) {
      while (K - sumA.at(i - 1) < sumB.at(j - 1)) {
        j--;
      }
    } else {
      break;
    }
    ans = max(ans, i + j);
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t A, B, n, m, k, j, ans = 0;
  cin >> n >> m >> k;
  int64_t a[n + 1], b[m + 1];
  a[0] = 0, b[0] = 0;
  for (int64_t i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  for (int64_t i = 1; i <= m; i++) {
    cin >> b[i];
    b[i] += b[i - 1];
  }
  j = m;
  for (int i = 0; i <= n; i++) {
    if (k < a[i])
      break;
    while (b[j] > k - a[i])
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t n, m, k, sum = 0, cnt = 0;
  cin >> n >> m >> k;
  queue<int64_t> a, b;
  for (int64_t i = 0; i < n; i++) {
    int64_t e;
    cin >> e;
    a.push(e);
  }
  for (int64_t i = 0; i < m; i++) {
    int64_t e;
    cin >> e;
    b.push(e);
  }
  while (1) {
    if (a.front() < b.front()) {
      sum += a.front();
      if (sum >= k)
        break;
      cnt++;
      a.pop();
    } else {
      sum += b.front();
      if (sum >= k)
        break;
      cnt++;
      b.pop();
    }
  }
  cout << cnt << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main() {
  int n, m;
  cin >> n >> m;
  ll k;
  cin >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  int ans = 0;
  rep(i, n - 1) a[i + 1] += a[i];
  rep(i, m - 1) b[i + 1] += b[i];
  rep(i, n) {
    if (a[i] > k)
      continue;
    int left = -1, right = m;
    while (right - left > 1) {
      int mid = (left + right) / 2;
      if (a[i] + b[mid] <= k)
        left = mid;
      else
        right = mid;
    }
    ans = max(ans, i + 1 + left + 1);
    // cout << ans << endl;
  }
  rep(i, m) {
    if (b[i] > k)
      continue;
    int left = -1, right = n;
    while (right - left > 1) {
      int mid = (left + right) / 2;
      if (b[i] + a[mid] <= k)
        left = mid;
      else
        right = mid;
    }
    ans = max(ans, i + 1 + left + 1);
    // cout << ans << endl;
  }
  /*
  rep(i,n) cout << a[i] << " " ;
  cout << endl;
  rep(i,m) cout << b[i] << " " ;
  cout << endl;
  */

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

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main() {
  int n, m;
  cin >> n >> m;
  ll k;
  cin >> k;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  ll ans = 0;
  rep(i, n) a[i + 1] += a[i];
  rep(i, m) b[i + 1] += b[i];
  rep(i, n) {
    if (a[i] > k)
      continue;
    ll left = -1, right = m;
    while (right - left > 1) {
      ll mid = (left + right) / 2;
      if (a[i] + b[mid] <= k)
        left = mid;
      else
        right = mid;
    }
    ans = max(ans, i + 1 + left + 1);
    // cout << ans << endl;
  }
  /*
  rep(i,n) cout << a[i] << " " ;
  cout << endl;
  rep(i,m) cout << b[i] << " " ;
  cout << endl;
  */
  cout << ans << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long arr[3000000], arr1[3000000], cum[3000000], cum1[3000000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, k, i, j, x, y, z, a, b, z1, c, cc;
  cin >> n >> m >> k;
  a = 0;
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
    cum[i] = cum[i - 1] + arr[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> arr1[i];
    cum1[i] = cum1[i - 1] + arr1[i];
  }
  z = 0;
  for (i = 0; i <= n; i++) {
    if (cum[i] > k) {
      break;
    }
  }
  z = i - 1;
  x = z;
  y = 0;
  //	for (i=1;i<=m;i++)
  //	{
  //		if (b<cum1[i])
  //		{
  //			break;
  //		}
  //	}
  //	//cout<<z<<endl;
  //	y=i-1;
  //	z+=y;
  // cout<<y<<endl;
  b = 0;
  while (1) {
    if (y == m && x == 0) {
      break;
    }
    y++;
    if (y > m) {
      y = m;
    }
    c = cum1[y] + cum[x];
    if (c <= k) {
      cc = x + y;
      z = max(z, cc);
      if (y == m) {
        break;
      }
    } else {
      x--;
      if (x < 0)

      {
        x = 0;
      }
    }
  }

  cout << z << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long arr[1000000], arr1[1000000], cum[1000000], cum1[1000000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, k, i, j, x, y, z, a, b, z1, c, cc;
  cin >> n >> m >> k;
  a = 0;
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
    cum[i] = cum[i - 1] + arr[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> arr1[i];
    cum1[i] = cum1[i - 1] + arr1[i];
  }
  z = 0;
  for (i = 0; i <= n; i++) {
    if (cum[i] > k) {
      break;
    }
  }
  z = i - 1;
  x = z;
  y = 0;
  //	for (i=1;i<=m;i++)
  //	{
  //		if (b<cum1[i])
  //		{
  //			break;
  //		}
  //	}
  //	//cout<<z<<endl;
  //	y=i-1;
  //	z+=y;
  // cout<<y<<endl;
  b = 0;
  while (1) {
    if (y == m && x == 0) {
      break;
    }
    y++;
    if (y > m) {
      y = m;
    }
    c = cum1[y] + cum[x];
    if (c <= k) {
      cc = x + y;
      z = max(z, cc);
      if (y == m) {
        break;
      }
    } else {
      x--;
    }
  }

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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

int main() {
  int N, M;
  cin >> N >> M;
  ll K;
  cin >> K;
  vecl A(N), B(M);
  REP(i, N) cin >> A[i];
  REP(i, M) cin >> B[i];
  ll ans = 0;
  vecl cumA(N + 1), cumB(M + 1);
  REP(i, N) { cumA[i + 1] = cumA[i] + A[i]; }
  REP(i, M) { cumB[i + 1] = cumB[i] + B[i]; }
  for (ll i = 1; i <= N; i++) {
    ll l = 0;
    ll r = M + 1;
    if (cumA[i] > K)
      continue;
    while (r - l > 1) {
      ll mid = (r + l) / 2;
      if (cumB[mid] > K - cumA[i])
        r = mid;
      else
        l = mid;
    }
    chmax(ans, i + l);
  }
  for (ll i = 1; i <= M; i++) {
    ll l = 0;
    ll r = N + 1;
    if (cumB[i] > K)
      continue;
    while (r - l > 1) {
      ll mid = (r + l) / 2;
      if (cumA[mid] > K - cumB[i])
        r = mid;
      else
        l = mid;
    }
    // cout << l << " " << r << endl;
    chmax(ans, i + l);
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

int main() {
  int N, M;
  cin >> N >> M;
  ll K;
  cin >> K;
  vecl A(N), B(M);
  REP(i, N) cin >> A[i];
  REP(i, M) cin >> B[i];
  ll T = 0;
  int ans = 0;
  int i, j = 0;
  while (1) {
    if (i < N) {
      if (j < M) {
        if (A[i] < B[j]) {
          T += A[i];
          i++;
        } else {
          T += B[j];
          j++;
        }
      } else {
        T += A[i];
        i++;
      }
    } else if (j < M) {
      T += B[j];
      j++;
    } else {
      cout << ans << endl;
      return 0;
    }
    if (T > K) {
      cout << ans << endl;
      return 0;
    }
    ans++;
  }
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> P;
struct edge {
  int to, id;
};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  ll a[n + 1];
  ll b[m + 1];
  ll suma[n + 1];
  ll sumb[m + 1];
  rep(i, n + 1) {
    a[i] = 0;
    suma[i] = 0;
  }
  rep(i, m + 1) {
    b[i] = 0;
    sumb[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    ll temp;
    cin >> temp;
    a[i] = temp;
    suma[i] = suma[i - 1] + temp;
  }
  for (int i = 1; i <= m; i++) {
    ll temp;
    cin >> temp;
    b[i] = temp;
    sumb[i] = sumb[i - 1] + temp;
  }
  int upper = m;
  int ans = -1;
  for (int i = 0; i <= n && suma[i] <= k; i++) {
    while (sumb[upper] + suma[i] > k && upper > 0) {
      upper--;
    }
    // ans = max(ans,upper+i+2);
    int temp = upper + i;
    if (ans < upper + i) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> P;
struct edge {
  int to, id;
};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 10];
  ll b[m + 10];
  ll suma[n + 10];
  ll sumb[m + 10];
  rep(i, n) {
    ll temp;
    cin >> temp;
    a[i] = temp;
    if (i == 0) {
      suma[i] = temp;
    } else {
      suma[i] = suma[i - 1] + temp;
    }
  }
  rep(i, m) {
    ll temp;
    cin >> temp;
    b[i] = temp;
    if (i == 0) {
      sumb[i] = temp;
    } else {
      sumb[i] = sumb[i - 1] + temp;
    }
  }
  ll upper = m - 1;
  bool ok = false;
  ll ans = -1;
  for (int i = 0; i < n && suma[i] <= k; n) {
    while (sumb[upper] > k - suma[i] && upper > 0) {
      upper--;
    }
    // ans = max(ans,upper+i+2);
    if (suma[i] + sumb[upper] <= k) {
      ok = true;
      ll temp = upper + i + 2;
      if (ans < upper + i + 2) {
        ans = temp;
      }
    }
  }
  if (ok)
    cout << ans << endl;
  else
    cout << 0 << endl;
}
Problem p02623 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, m, z = 0;
  ll a[200005] = {}, b[200005] = {}, k;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> b[i];
  for (int i = 1; i <= n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i <= m; i++)
    b[i] += b[i - 1];
  for (int i = 0; i <= n; i++) {
    if (a[i] <= k) {
      int l = 0, r = m + 1;
      while (l + 1 < r) {
        int x = (l + r) / 2;
        if (a[i] + b[x] <= k) {
          l = x;
        } else {
          r = x;
        }
      }
      z = max(z, i + l);
    }
  }
  cout << z;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, m, k;
  cin >> n >> m >> k;
  int c, d;
  deque<int> a, b;
  ll z = 0;
  for (int i = 0; i < n; i++)
    cin >> c, a.push_back(c), z += c;
  for (int i = 0; i < m; i++)
    cin >> d, b.push_back(d), z += d;
  ll x = 0;
  int y = 0;
  if (z <= k)
    cout << n + m;
  else {
    while (a.size() > 0 || b.size() > 0) {
      if (x + a[0] < k && x + b[0] < k) {
        y++;
        if (a[0] < b[0])
          x += a[0], a.pop_front();
        else if (b[0] < a[0])
          x += b[0], b.pop_front();
        else {
          x += b[0];
          b.pop_front();
        }
      } else if (x + a[0] < k && x + b[0] > k)
        x += a[0], y++;
      else if (x + b[0] < k && x + a[0] > k)
        x += b[0], y++;
      else {
        cout << y;
        return 0;
      }
    }
  }
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m, k;
vi a, b;

bool Read() {
  cin >> n;
  if (cin.eof())
    return 0;
  cin >> 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];

  return 1;
}

void Process() {
  vector<ll> psaa(n), psab(m);
  psaa[0] = a[0];
  psab[0] = b[0];
  for (int i = 1; i < n; i++)
    psaa[i] = psaa[i - 1] + a[i];
  for (int i = 1; i < m; i++)
    psab[i] = psab[i - 1] + b[i];
  int i = 0, j = 0;
  while (i < n && psaa[i] <= k)
    i++;
  int ans = i;
  while (i >= 0) {
    const ll t = i ? psaa[i - 1] : 0;
    while (j < m && t + psab[j] <= k)
      j++;
    ans = max(ans, i + j);
    i--;
  }
  cout << ans << "\\n";
}

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

  while (Read())
    Process();

  return 0;
}

Error: TLE

Fixed submission:

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

int n, m, k;
vi a, b;

bool Read() {
  cin >> n;
  if (cin.eof())
    return 0;
  cin >> 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];

  return 1;
}

void Process() {
  int ans = 0, x = 0;
  ll suma = 0;
  for (int i = 0; i <= n && suma <= k; i++) {
    int y = 0;
    ll sumb = 0;
    for (int j = 0; j <= m && suma + sumb <= k; j++) {
      ans = max(ans, x + y);
      ++y;
      sumb += j < m ? b[j] : 0;
    }
    ++x;
    suma += i < n ? a[i] : 0;
  }
  cout << ans << "\\n";
}

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

  while (Read())
    Process();

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, an, am, k, res = 0;
ll dp[400001], a[200001], b[200001];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i == 1)
      continue;
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    if (i == 1)
      continue;
    b[i] += b[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] > k)
      break;
    an = i;
  }
  for (int i = 1; i <= m; i++) {
    if (b[i] > k)
      break;
    am = i;
  }
  int i = 0, j = am;
  while (i <= an && j >= 0) {
    if (a[i] + b[j] <= k)
      res = max(res, i + j);
    else {
      while (j >= 0) {
        j--;
        if (a[i] + b[j] <= k) {
          res = max(res, i + j);
          break;
        }
      }
    }
    i++;
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, k, res = 0;
ll dp[400001], a[200001], b[200001];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i == 1)
      continue;
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    if (i == 1)
      continue;
    b[i] += b[i - 1];
  }
  for (int i = 1; i <= n + m; i++) {
    for (int x = 1; x <= i; x++) {
      int y = i - x;
      if (x > n || y > m)
        continue;
      if (a[x] + b[y] <= k)
        res = max(res, i);
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02623 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdint.h>
#include <string>
#include <vector>

using namespace std;

#define PI (3.14159265358979323846)
#define INF (2147483647)
#define INF_LL (9223372036854775807)
#define DIV_NUM (1000000007)
typedef long long ll;

// 配列初期化テンプレ
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int main() {

  ll N, M, K;
  cin >> N >> M >> K;

  vector<ll> A(N + 1, 0);
  for (ll i = 0; i < N; i++) {
    ll tmp;
    cin >> tmp;
    A[i + 1] = A[i] + tmp;
  }

  vector<ll> B(M + 1, 0);
  for (ll i = 0; i < M; i++) {
    ll tmp;
    cin >> tmp;
    B[i + 1] = B[i] + tmp;
  }

  ll ans = 0;
  for (ll i = 0; i < A.size(); i++) {
    ll rest = K - A[i];
    if (rest < 0)
      break;
    vector<ll>::iterator ite = upper_bound(B.begin(), B.end(), rest);
    ll Bnum = 0;
    if (ite == B.end())
      Bnum = B.size() - 1;
    else {
      Bnum = ite - B.begin();
      Bnum -= 1;
    }
    ll total_num = i + Bnum;
    ans = max(ans, total_num);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdint.h>
#include <string>
#include <vector>

using namespace std;

#define PI (3.14159265358979323846)
#define INF (2147483647)
#define INF_LL (9223372036854775807)
#define DIV_NUM (1000000007)
typedef long long ll;

// 配列初期化テンプレ
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int main() {

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

  ll all_time = 0;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    all_time += A[i];
  }

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

  size_t countA = A.size();
  size_t countB = B.size();

  while (all_time > K) {

    if (countA > 0 && countB > 0) {
      if (A[countA - 1] >= B[countB - 1]) {
        all_time -= A[countA - 1];
        countA--;
      } else {
        all_time -= B[countB - 1];
        countB--;
      }
    } else if (countA > 0) {
      all_time -= A[countA - 1];
      countA--;
    } else if (countB > 0) {
      all_time -= B[countB - 1];
      countB--;
    }
  }

  while (true) {
    if (countA < A.size()) {
      if (all_time + A[countA] <= K)
        countA++;
    } else if (countB < B.size()) {
      if (all_time + B[countB] <= K)
        countB++;
    } else {
      break;
    }
  }

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

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

static const int MAX = 2e+5 + 1;

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

  ll sum_a[MAX], sum_b[MAX];
  sum_a[0] = sum_b[0] = 0;
  for (int i = 0; i < n; i++) {
    sum_a[i + 1] = a[i] + sum_a[i];
  }
  for (int i = 0; i < m; i++) {
    sum_b[i + 1] = b[i] + sum_b[i];
  }

  int ans = 0;
  int j = m;
  for (int i = 0; i <= n; i++) {
    if (sum_a[i] > k)
      break;
    while (k - sum_a[i] < sum_b[j])
      j--;
    ans = max(ans, i + j);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

static const int MAX = 1e+5;

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

  ll sum_a[MAX], sum_b[MAX];
  sum_a[0] = a[0];
  sum_b[0] = b[0];
  for (int i = 1; i < n; i++) {
    sum_a[i] = a[i] + sum_a[i - 1];
  }
  for (int i = 1; i < m; i++) {
    sum_b[i] = b[i] + sum_b[i - 1];
  }

  int j = m - 1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    while (k - sum_a[i] < sum_b[j])
      j--;
    ans = max(ans, i + j + 2);
  }
  cout << ans << endl;
  return 0;
}