Problem p02623 (C++) — buggy submission, verdict: Runtime Error


// Problem : C - Tsundoku
// Contest : AtCoder - AtCoder Beginner Contest 172
// URL : https://atcoder.jp/contests/abc172/tasks/abc172_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(a) (int)((a).size())
#define pb push_back
#define mp make_pair
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbp(x)                                                                 \\
  cout << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define dbv(v)                                                                 \\
  {                                                                            \\
    cout << #v << " : ";                                                       \\
    for (auto x : v)                                                           \\
      cout << x << ' ';                                                        \\
    cout << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define die(x) return cout << x << '\\n', 0;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int32_t main() {
  fastio;
  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
  int testcases;
  testcases = 1;
  // cin>>testcases;

  while (testcases--) {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> a(n), b(m), pa(n + 1, 0), pb(m + 1, 0);
    rep(i, 0, n - 1) {
      cin >> a[i];
      pa[i + 1] = pa[i] + a[i];
    }
    rep(i, 0, m - 1) {
      cin >> b[i];
      pb[i + 1] = pb[i] + b[i];
    }

    int ans = 0;

    for (int i = 0; i <= n; i++) {
      if (pa[i] > k)
        break;
      int r = k - pa[i];
      int lo = 0;
      int hi = m;
      int a = 0;
      while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (pb[mid] <= r) {
          a = mid;
          lo = mid + 1;
        } else {
          hi = mid - 1;
        }
      }
      ans = max(a + i, ans);
    }
    cout << ans << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:


// Problem : C - Tsundoku
// Contest : AtCoder - AtCoder Beginner Contest 172
// URL : https://atcoder.jp/contests/abc172/tasks/abc172_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(a) (int)((a).size())
#define pb push_back
#define mp make_pair
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbp(x)                                                                 \\
  cout << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define dbv(v)                                                                 \\
  {                                                                            \\
    cout << #v << " : ";                                                       \\
    for (auto x : v)                                                           \\
      cout << x << ' ';                                                        \\
    cout << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define die(x) return cout << x << '\\n', 0;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int32_t main() {
  fastio;
  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
  int testcases;
  testcases = 1;
  // cin>>testcases;

  while (testcases--) {
    int n, m, k;
    cin >> n >> m >> k;
    vi a(n), b(m);
    rep(i, 0, n - 1) { cin >> a[i]; }
    rep(i, 0, m - 1) { cin >> b[i]; }
    int i = 0, j = 0;
    int sum = 0, ans = 0;
    ;
    while (i < n && j < m) {
      if (a[i] < b[j]) {
        sum += a[i];
        i++;
      } else {
        sum += b[j];
        j++;
      }
      if (sum <= k)
        ans++;
      else
        die(ans);
    }
    while (i < n) {
      sum += a[i];
      i++;
      if (sum <= k)
        ans++;
      else
        die(ans);
    }
    while (j < m) {
      sum += a[j];
      j++;
      if (sum <= k)
        ans++;
      else
        die(ans);
    }
    cout << ans << '\\n';
  }

  return 0;
}