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

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define pw(b, p) pow(b, p) + 0.1
#define __                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define endl "\\n"
#define m(a) memset(a, 0, sizeof(a))
#define all(x) begin(x), end(x)

void fastscan(int &x) {
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar()) {
    x = x * 10 + c - 48;
  }
  if (neg) {
    x *= -1;
  }
}

void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}

int bs(int a[], int s, int e, int x) {
  if (s <= e) {
    int m = (s + e) / 2;
    if (a[m] == x)
      return m;
    else if (a[m] > x)
      return bs(a, s, m - 1, x);
    else
      return bs(a, m + 1, e, x);
  }
  return -1;
}

#define tr(x) cout << x << endl;
#define tr2(x, y) cout << x << " " << y << endl;
#define tr3(x, y, z) cout << x << " " << y << " " << z << endl;
#define tr4(w, x, y, z) cout << w << " " << x << " " << y << " " << z << endl;
#define tr5(v, w, x, y, z)                                                     \\
  cout << v << " " << w << " " << x << " " << y << " " << z << endl;
#define tr6(u, v, w, x, y, z)                                                  \\
  cout << u << " " << v << " " << w << " " << x << " " << y << " " << z << endl;

#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define deba(a)                                                                \\
  for (auto &x : a)                                                            \\
    cout << x << " ";

#define sd(x) cin >> x;
#define sd2(x, y) cin >> x >> y;
#define sd3(x, y, z) cin >> x >> y >> z;
#define sd4(w, x, y, z) cin >> w >> x >> y >> z;

bool is_pal(string s) { return equal(all(s), s.rbegin()); }

int mypow(int a, int b) { //(logn) fast exponentiation
  int res = 1;            // call int x = mypow(2,5);
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return (res % mod);
}

void solve1() {
  // 659 D2 B..restore permutations by merger..
  int n;
  cin >> n;
  map<int, int> m;
  for (int i = 0; i < n * 2; ++i) {
    int x;
    sd(x);
    m[x]++;
    if (m[x] == 1) {
      cout << x << " ";
    } else
      continue;
  }
  cout << endl;
}

void solve2() {
  // 659 D2 C => mention in notes...
  /*

  PICKING ELEMENTS FROM THE TERMINALS OF AN ARRAY IF FORMS A SORTED SEQUENCE,
  THEN IT IS EITHER :
          1. INCREASING
          2. DECREASING
          3. FORMS A PEAK i.e (increasing ... max ... decreasing)
          4. HAD THE NEW SEQUENCE BEEN DECREASING, THEN WE WOULD LOOK FOR A "V"
  PATTERN IN THE ARRAY..i.e a ditch instead of a peak..

  Algo:
          remove the longest prefix such that the resulting array satisfies the
  following:
                  1. appending terminal (either side) elements to a new array
  results in the new array being sorted always..
                  2. observe that for this, the original array (excluding the
  prefix to be removed) must be of the follwing forms a. Increasing completely
  (we pick from the start and keep appending) b. completely Decreasing, (we pick
  from the end and keep appending) c. should have behaviour similar to a ** PEAK
  **	(very important observation !!)
                                          => [a[0] <= a[1] <= ....a[mid].... >=
  a[i] >= a[i+1] ...a[end]]
                  3. thus we move from right to left and while the above
  conditions hold, we keep going back, the moment this is violated, we remove
  from the start to the current index (prefix to be removed)..
  */
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) sd(a[i]);
  int pos = n - 1;
  while (pos > 0 and a[pos - 1] >= a[pos])
    --pos;
  while (pos > 0 and a[pos - 1] <= a[pos])
    --pos;
  cout << pos << endl;
}

void solve3() {
  // 656 D3 A
  /*
          given (x,y,x), find (a,b,c) s.t max(a,b)=x ; max(b,c) = y; max(a,c) =
     z; if(x<= y <= z) => m(a,b) <= m(b,c) <= m(a,c) => (if(b>a) then (c>b)) or
     if(a>b) then (c>a)) therefore, a value always repeats => take into a[] ,
     sort(a,a+3). if(a[1] != a[2]) return -1; else cout << a[0] << a[0] << a[2];
  */
  int a[3];
  rep(i, 0, 3) sd(a[i]);
  sort(a, a + 3);
  if (a[1] != a[2]) {
    cout << "NO" << endl;
    return;
  }
  cout << "YES" << endl;
  tr3(a[0], a[0], a[1]);
}

void solve4() {
  // neighbouring grid..
  int n, m;
  cin >> n >> m;
  int a[n][m];
  rep(i, 0, n) {
    rep(j, 0, m) { sd(a[i][j]); }
  }
  int f = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int lvl = 0;
      if (i > 0)
        ++lvl;
      if (i < n - 1)
        ++lvl;
      if (j > 0)
        ++lvl;
      if (j < m - 1)
        ++lvl;
      if (a[i][j] > lvl) {
        f = 1;
        break;
      }
    }
    if (f) {
      break;
    }
  }
  if (f) {
    cout << "NO" << endl;
  } else {
    cout << "YES" << endl;
    rep(i, 0, n) {
      rep(j, 0, m) {
        int lvl = 0;
        if (i > 0)
          ++lvl;
        if (i < n - 1)
          ++lvl;
        if (j > 0)
          ++lvl;
        if (j < m - 1)
          ++lvl;
        a[i][j] = lvl;
        cout << a[i][j] << " ";
      }
      cout << endl;
    }
  }
}

void solve5() {
  // CF ED90 A
  int a, b, c;
  cin >> a >> b >> c;
  // check the cost of 1 donut...cuz in first..
  if (a < c)
    cout << 1 << " ";
  else
    cout << -1 << " ";

  // check the cost of b donuts, cuz in second..
  if (a * b > c)
    cout << b << " ";
  else
    cout << -1 << " ";

  cout << endl;
}

void atcoder173D() {
  // sequence is a[0] + a[1] + a[1] + a[2] + a[2] + a[3] + a[3]....
  int n;
  cin >> n;
  vi a(n + 1);
  rep(i, 0, n) sd(a[i]);
  int ans = 0;
  sort(all(a));
  reverse(all(a));
  ans += a[0];
  for (int i = 2; i < n; ++i) {
    ans += (a[(int)floor(i / 2)]);
  }
  tr(ans);
}

void atcoder172A() {
  int n;
  sd(n);
  cout << n + n * n + n * n * n << endl;
}

void atcoder172B() {
  string s, t;
  cin >> s >> t;
  int ans = 0;
  for (int i = 0; i < (int)s.length(); ++i) {
    if (s[i] != t[i])
      ++ans;
  }
  cout << ans << endl;
}

/*const int N = 1e7+1;
int spf[N];
void sieve(){
        for(int i=1 ; i<N+1 ; ++i) spf[i] = i;
        for(int i=4 ; i<N+1 ; i+=2) spf[i]=2;
        for(int i=3 ; i*i <= N ;++i){
                if(spf[i]==i){
                        for(int j=i*i ; j<=N ; j+=i){
                                if(spf[j]==j){
                                        spf[j]=i;
                                }
                        }
                }
        }
}

int getFact(int n){
        map<int,int>m;
        while(n!=1){
                m[spf[n]]++;
                n/=spf[n];
        }
        int ans=1;
        for(auto itr : m){
                ans*=(itr.ss+1);
        }
        return ans;
}
*/
int32_t main() {
  __;
  file();
  int n, m, k;
  cin >> n >> m >> k;
  int ans = 0;
  int a[n];
  int b[m];
  rep(i, 0, n) sd(a[i]);
  rep(i, 0, m) sd(b[i]);

  vi pa(n + 1);
  vi pb(m + 1);

  for (int i = 0; i < n; ++i) {
    pa[i + 1] = pa[i] + a[i];
  }
  for (int i = 0; i < m; ++i) {
    pb[i + 1] = pb[i] + b[i];
  }

  for (int i = 0; i < n + 1; ++i) {
    int target = k - pa[i];
    if (target < 0)
      continue;
    int lb = lower_bound(all(pb), target) - pb.begin();
    while (lb > m || pb[lb] > target) {
      --lb;
    }
    ans = max(ans, lb + i);
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define pw(b, p) pow(b, p) + 0.1
#define __                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define endl "\\n"
#define m(a) memset(a, 0, sizeof(a))
#define all(x) begin(x), end(x)

void fastscan(int &x) {
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar()) {
    x = x * 10 + c - 48;
  }
  if (neg) {
    x *= -1;
  }
}

void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}

int bs(int a[], int s, int e, int x) {
  if (s <= e) {
    int m = (s + e) / 2;
    if (a[m] == x)
      return m;
    else if (a[m] > x)
      return bs(a, s, m - 1, x);
    else
      return bs(a, m + 1, e, x);
  }
  return -1;
}

#define tr(x) cout << x << endl;
#define tr2(x, y) cout << x << " " << y << endl;
#define tr3(x, y, z) cout << x << " " << y << " " << z << endl;
#define tr4(w, x, y, z) cout << w << " " << x << " " << y << " " << z << endl;
#define tr5(v, w, x, y, z)                                                     \\
  cout << v << " " << w << " " << x << " " << y << " " << z << endl;
#define tr6(u, v, w, x, y, z)                                                  \\
  cout << u << " " << v << " " << w << " " << x << " " << y << " " << z << endl;

#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define deba(a)                                                                \\
  for (auto &x : a)                                                            \\
    cout << x << " ";

#define sd(x) cin >> x;
#define sd2(x, y) cin >> x >> y;
#define sd3(x, y, z) cin >> x >> y >> z;
#define sd4(w, x, y, z) cin >> w >> x >> y >> z;

bool is_pal(string s) { return equal(all(s), s.rbegin()); }

int mypow(int a, int b) { //(logn) fast exponentiation
  int res = 1;            // call int x = mypow(2,5);
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return (res % mod);
}

void solve1() {
  // 659 D2 B..restore permutations by merger..
  int n;
  cin >> n;
  map<int, int> m;
  for (int i = 0; i < n * 2; ++i) {
    int x;
    sd(x);
    m[x]++;
    if (m[x] == 1) {
      cout << x << " ";
    } else
      continue;
  }
  cout << endl;
}

void solve2() {
  // 659 D2 C => mention in notes...
  /*

  PICKING ELEMENTS FROM THE TERMINALS OF AN ARRAY IF FORMS A SORTED SEQUENCE,
  THEN IT IS EITHER :
          1. INCREASING
          2. DECREASING
          3. FORMS A PEAK i.e (increasing ... max ... decreasing)
          4. HAD THE NEW SEQUENCE BEEN DECREASING, THEN WE WOULD LOOK FOR A "V"
  PATTERN IN THE ARRAY..i.e a ditch instead of a peak..

  Algo:
          remove the longest prefix such that the resulting array satisfies the
  following:
                  1. appending terminal (either side) elements to a new array
  results in the new array being sorted always..
                  2. observe that for this, the original array (excluding the
  prefix to be removed) must be of the follwing forms a. Increasing completely
  (we pick from the start and keep appending) b. completely Decreasing, (we pick
  from the end and keep appending) c. should have behaviour similar to a ** PEAK
  **	(very important observation !!)
                                          => [a[0] <= a[1] <= ....a[mid].... >=
  a[i] >= a[i+1] ...a[end]]
                  3. thus we move from right to left and while the above
  conditions hold, we keep going back, the moment this is violated, we remove
  from the start to the current index (prefix to be removed)..
  */
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) sd(a[i]);
  int pos = n - 1;
  while (pos > 0 and a[pos - 1] >= a[pos])
    --pos;
  while (pos > 0 and a[pos - 1] <= a[pos])
    --pos;
  cout << pos << endl;
}

void solve3() {
  // 656 D3 A
  /*
          given (x,y,x), find (a,b,c) s.t max(a,b)=x ; max(b,c) = y; max(a,c) =
     z; if(x<= y <= z) => m(a,b) <= m(b,c) <= m(a,c) => (if(b>a) then (c>b)) or
     if(a>b) then (c>a)) therefore, a value always repeats => take into a[] ,
     sort(a,a+3). if(a[1] != a[2]) return -1; else cout << a[0] << a[0] << a[2];
  */
  int a[3];
  rep(i, 0, 3) sd(a[i]);
  sort(a, a + 3);
  if (a[1] != a[2]) {
    cout << "NO" << endl;
    return;
  }
  cout << "YES" << endl;
  tr3(a[0], a[0], a[1]);
}

void solve4() {
  // neighbouring grid..
  int n, m;
  cin >> n >> m;
  int a[n][m];
  rep(i, 0, n) {
    rep(j, 0, m) { sd(a[i][j]); }
  }
  int f = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int lvl = 0;
      if (i > 0)
        ++lvl;
      if (i < n - 1)
        ++lvl;
      if (j > 0)
        ++lvl;
      if (j < m - 1)
        ++lvl;
      if (a[i][j] > lvl) {
        f = 1;
        break;
      }
    }
    if (f) {
      break;
    }
  }
  if (f) {
    cout << "NO" << endl;
  } else {
    cout << "YES" << endl;
    rep(i, 0, n) {
      rep(j, 0, m) {
        int lvl = 0;
        if (i > 0)
          ++lvl;
        if (i < n - 1)
          ++lvl;
        if (j > 0)
          ++lvl;
        if (j < m - 1)
          ++lvl;
        a[i][j] = lvl;
        cout << a[i][j] << " ";
      }
      cout << endl;
    }
  }
}

void solve5() {
  // CF ED90 A
  int a, b, c;
  cin >> a >> b >> c;
  // check the cost of 1 donut...cuz in first..
  if (a < c)
    cout << 1 << " ";
  else
    cout << -1 << " ";

  // check the cost of b donuts, cuz in second..
  if (a * b > c)
    cout << b << " ";
  else
    cout << -1 << " ";

  cout << endl;
}

void atcoder173D() {
  // sequence is a[0] + a[1] + a[1] + a[2] + a[2] + a[3] + a[3]....
  int n;
  cin >> n;
  vi a(n + 1);
  rep(i, 0, n) sd(a[i]);
  int ans = 0;
  sort(all(a));
  reverse(all(a));
  ans += a[0];
  for (int i = 2; i < n; ++i) {
    ans += (a[(int)floor(i / 2)]);
  }
  tr(ans);
}

void atcoder172A() {
  int n;
  sd(n);
  cout << n + n * n + n * n * n << endl;
}

void atcoder172B() {
  string s, t;
  cin >> s >> t;
  int ans = 0;
  for (int i = 0; i < (int)s.length(); ++i) {
    if (s[i] != t[i])
      ++ans;
  }
  cout << ans << endl;
}

/*const int N = 1e7+1;
int spf[N];
void sieve(){
        for(int i=1 ; i<N+1 ; ++i) spf[i] = i;
        for(int i=4 ; i<N+1 ; i+=2) spf[i]=2;
        for(int i=3 ; i*i <= N ;++i){
                if(spf[i]==i){
                        for(int j=i*i ; j<=N ; j+=i){
                                if(spf[j]==j){
                                        spf[j]=i;
                                }
                        }
                }
        }
}

int getFact(int n){
        map<int,int>m;
        while(n!=1){
                m[spf[n]]++;
                n/=spf[n];
        }
        int ans=1;
        for(auto itr : m){
                ans*=(itr.ss+1);
        }
        return ans;
}
*/
int32_t main() {
  __;
  file();
  int n, m, k;
  cin >> n >> m >> k;
  int a[n];
  rep(i, 0, n) sd(a[i]);
  int b[n];
  rep(i, 0, m) sd(b[i]);
  int ans = 0;
  int p, q;
  for (p = 0, q = 0; p < n and q < m;) {
    if (a[p] < b[q] and a[p] <= k) {
      k -= a[p];
      // cout << a[p] << endl;
      ++ans;
      p++;
    } else if (b[q] <= a[p] and b[q] <= k) {
      k -= b[q];
      // cout << b[q] << endl;
      ++ans;
      q++;
    } else
      break;
  }
  while (q < m and b[q] <= k) {
    // cout << b[q] << endl;
    k -= b[q++];
    ++ans;
  }
  while (p < n and a[p] <= k) {
    // cout << a[p] << endl;
    k -= a[p++];
    ++ans;
  }
  cout << ans << endl;
  return 0;
}