Problem p02824 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define int long long int
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
using namespace std;
/// age ye mosh dp ro baze dashti dp tedad baz shodeye baste nashode yadet nare
/// !!
const int N = 1e5 + 100;
ll a[N];
ll n, m, v, p;
ll vis[N];
ll fen[N];
void add(ll idx, ll val) {
  for (; idx < N; idx += idx & (-idx)) {
    fen[idx] += val;
  }
}
ll get(ll idx) {
  ll s = 0;
  for (; idx; idx -= idx & (-idx)) {
    s += fen[idx];
  }
  return s;
}
ll bs(ll x) {
  ll val = a[x];
  val += m;
  memset(vis, 0, sizeof vis);
  memset(fen, 0, sizeof fen);
  vector<int> c;
  for (int i = x + 1; i <= n; i++) {
    if (a[i] > val) {
      vis[i] = 1;
      c.pb(m);
    }
  }
  ll p1 = c.size();
  if (p1 > p - 1)
    return 0;
  for (int i = n; i > 0; i--) {
    if (vis[i] || i == x)
      continue;
    if (p1 < p - 1) {
      c.pb(m);
      // if (x==3)
      // cout << c.back() << " " << i << endl;

      p1++;
      continue;
    } else {
      c.pb(min(m, val - a[i]));
      // if (x==3)
      // cout << c.back() << " " << i << endl;
    }
  }
  ll ans = 0;
  for (int i = 0; i < c.size(); i++) {
    ll u = c[i];
    ans += u;
  }
  ans /= (v - 1);
  if (ans >= m)
    return 1;
  return 0;
}
int32_t main() {
  cin >> n >> m >> v >> p;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  ll l = 0, r = n + 1;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll z = bs(mid);
    if (z == 1)
      r = mid;
    else
      l = mid;
  }
  cout << n + 1 - r;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define int long long int
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
using namespace std;
/// age ye mosh dp ro baze dashti dp tedad baz shodeye baste nashode yadet nare
/// !!
const int N = 1e5 + 100;
ll a[N];
ll n, m, v, p;
ll vis[N];
ll fen[N];
void add(ll idx, ll val) {
  for (; idx < N; idx += idx & (-idx)) {
    fen[idx] += val;
  }
}
ll get(ll idx) {
  ll s = 0;
  for (; idx; idx -= idx & (-idx)) {
    s += fen[idx];
  }
  return s;
}
ll bs(ll x) {
  ll val = a[x];
  val += m;
  memset(vis, 0, sizeof vis);
  memset(fen, 0, sizeof fen);
  vector<int> c;
  for (int i = x + 1; i <= n; i++) {
    if (a[i] > val) {
      vis[i] = 1;
      c.pb(m);
    }
  }
  ll p1 = c.size();
  if (p1 > p - 1)
    return 0;
  for (int i = n; i > 0; i--) {
    if (vis[i] || i == x)
      continue;
    if (p1 < p - 1) {
      c.pb(m);
      // if (x==3)
      // cout << c.back() << " " << i << endl;

      p1++;
      continue;
    } else {
      c.pb(min(m, val - a[i]));
      // if (x==3)
      // cout << c.back() << " " << i << endl;
    }
  }
  ll ans = 0;
  for (int i = 0; i < c.size(); i++) {
    ll u = c[i];
    ans += u;
  }
  if (v == 1) {
    return 1;
  }
  ans /= (v - 1);
  if (ans >= m)
    return 1;
  return 0;
}
int32_t main() {
  cin >> n >> m >> v >> p;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  ll l = 0, r = n + 1;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll z = bs(mid);
    if (z == 1)
      r = mid;
    else
      l = mid;
  }
  cout << n + 1 - r;
}