Language: C++

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000000000000000
#define MOD 1000000007
typedef pair<int, int> P;

bool prime(int n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)return false;
	}
	return n != 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int mod_pow(int n, int p, int m) {
	if (p == 0) return 1;
	if (p % 2 == 0) {
		int t = mod_pow(n, p / 2, m);
		return (t * t) % m;
	}
	return n * mod_pow(n, p - 1, m) % m;
}
int extGCD(int a, int b, int& x, int& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	long long d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
int modinv(int a, int m) {
	int b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
int digit(int x) {
	int cnt = 0;
	while (x > 0) {
		cnt += x % 10;
		x /= 10;
	}
	return cnt;
}
int read() {
	int tmp;
	cin >> tmp;
	return tmp;
}
int xor128() {
	static int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
	int t = (x xor (x << 11));
	x = y; y = z; z = w;
	return (w = (w xor (w >> 19)) xor (t xor (t >> 8)));
}

int zero = 0;

int n, m, v, p;
int a[100005], ans;

bool check(int x) {
	int be = a[x];
	int af = a[x] + m;
	int up = a + n - upper_bound(a, a + n, af);
	//cout << be << " " << af << " " << up << endl;
	if (up >= p) return false;
	up = n - p + 1;
	int rest = max(zero, v - (p - 1) - (upper_bound(a, a + n, be) - a));
	rest *= m;
	//cout << " REST" << rest << endl;
	if (rest <= 0) return true;
	signed bou = n - p;
	for (int i = (upper_bound(a, a + n, be) - a); i <= min((lower_bound(a, a + n, af) - a - 1), bou); i++) {
		rest -= min(m, af - a[i]);
		//cout <<"#"<< min(m, af - a[i]) << " ";
	}
	//cout << endl;
	if (rest > 0) return false;
	return true;
}

signed main() {
	cin >> n >> m >> v >> p;
	rep(i, n) cin >> a[i];
	sort(a, a + n);
	int ok = n - 1, ng = -1;
	while ((ok - ng) > 1) {
		int mid = (ok + ng) / 2;
		//cout << "MID" << mid << endl;
		if (check(mid)) {
			//cout <<"MIDOK"<< mid << endl;
			ok = mid;
		}
		else {
			//cout << "MIDNG" << mid << endl;
			ng = mid;
		}
	}
	cout << n - ok << endl;

	return 0;
}


Compiler output:
a.cc: In function 'bool check(long long int)':
a.cc:90:63: error: no matching function for call to 'min(long int, int&)'
   90 |         for (int i = (upper_bound(a, a + n, be) - a); i <= min((lower_bound(a, a + n, af) - a - 1), bou); i++) {
      |                                                            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:90:63: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
   90 |         for (int i = (upper_bound(a, a + n, be) - a); i <= min((lower_bound(a, a + n, af) - a - 1), bou); i++) {
      |                                                            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:90:63: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   90 |         for (int i = (upper_bound(a, a + n, be) - a); i <= min((lower_bound(a, a + n, af) - a - 1), bou); i++) {
      |                                                            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~