Language: C++

//Æ­·Ö²»¹æ·¶£¬±¬ÁãÁ½ÐÐÀá
#include<bits/stdc++.h>
#define ll long long
#define int unsigned ll
#define mp make_pair
#define endl '\n'
#define rep(i,n) for(register int i=0;i<(n);++i)
#define For(i,x,y) for(register int i=(x);i<=(y);++i)
#define pii pair<int,int>
using namespace std;
#define mod_int quick_num
long long md=19260817;
struct quick_num{
	long long num;
	inline void read(){cin>>num;}
	inline void write(){cout<<num;}
	inline void _write(){cout<<num<<' ';}
	inline void print(){cout<<num<<endl;}
};
inline void read(quick_num&x){cin>>x.num;}
inline void write(quick_num x){cout<<x.num;}
inline void _write(quick_num x){cout<<x.num<<' ';}
inline void print(quick_num x){cout<<x.num<<endl;}
inline long long qp(long long x,long long y){
	long long rt=1;
	for(;y;y>>=1,(x*=x)%=md)if(y&1)(rt*=x)%=md;
	return rt;
}
inline long long inv(long long x){return qp(x,md-2);}
inline quick_num operator +(quick_num a,quick_num b){
	quick_num c;c.num=a.num+b.num;
	if(c.num>=md)c.num-=md;
	return c;
}
inline quick_num operator -(quick_num a,quick_num b){
	quick_num c;c.num=a.num-b.num;
	if(c.num<0)c.num+=md;
	return c;
}
inline quick_num operator *(quick_num a,quick_num b){
	quick_num c;c.num=a.num*b.num;
	c.num%=md;
	return c;
}
inline quick_num operator /(quick_num a,quick_num b){
	quick_num c;c.num=a.num*inv(b.num)%md;
	return c;
}
inline quick_num operator %(quick_num a,quick_num b){
	quick_num c;c.num=a.num%b.num;
	return c;
}
const int maxn=1000;
char buffer[maxn],*S,*T;
inline char Get_Char(){
    if(S==T){
        T=(S=buffer)+fread(buffer,1,maxn,stdin);
        if(S==T)return EOF;
    }
    return *S++;
}

inline int read(){
    char c;
    int re=0,f=0;
    for(c=Get_Char();c<'0' or c>'9';c=Get_Char())if(c=='-')f=1;
    for(;c>='0' and c<='9';)re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char();
    if(f)return -re;
    return re;
}

inline void read(int&x){
    char c;
    int re=0,f=0;
    for(c=Get_Char();c<'0' or c>'9';c=Get_Char())if(c=='-')f=1;
    for(;c>='0' and c<='9';)re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char();
    if(f)x=-re;
    else x=re;
}
const int mxn=1e5+5;
ll a[mxn];int n,m,v,p;
ll sm[mxn];
inline bool ok(int x){
	if(a[x]+m<a[p])return 0;
	int t=a[x];
	if(n-x+1>=v){
		if(a[x]+m>=a[p])return 1;
		else return 0;
	}
	int tv=v-(n-x+1);
	int bt=0,rt;
	for(int i=1;i<=n;++i)if(a[i]>a[x]+m)++bt;else{
		rt=i;
		break;
	}
	tv-=max(bt,p-1);
//	cerr<<bt<<' '<<tv<<' '<<p-1<<endl;
	tv=max(0ll,tv);
	ll tsm=sm[x-1]-sm[rt-1];
	ll tgv=tv*m;
	ll tql=(a[x]+m)*(x-rt);
//	cerr<<a[x]<<' '<<tsm<<' '<<tgv<<' '<<tql<<endl;
	if(tql-tsm>=tgv)return 1;
	else return 0;
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin>>n>>m>>v>>p;
	for(int i=1;i<=n;++i)cin>>a[i];
	sort(a+1,a+n+1);reverse(a+1,a+n+1);
	for(int i=1;i<=n;++i)sm[i]=sm[i-1]+a[i];
	int lo=1,hi=n+1,md;
	for(;lo<hi-1;){
		md=lo+hi>>1;
		if(ok(md))lo=md;
		else hi=md;
	}
	cout<<lo<<endl;
	return 0;
}
//3  2  2 1 1 0
//7  6  6 6 5 5 4 3 2 1 
//8  7  7 7 5 5 5 4 3 2
//9  8  8 8 6 5 5 5 4 3
//10 9  8 8 7 6 6 6 5 4
//11 10 9 9 7 7 6 7 6 5

Compiler output:
a.cc: In function 'bool ok(long long unsigned int)':
a.cc:98:15: error: no matching function for call to 'max(long long int, long long unsigned int&)'
   98 |         tv=max(0ll,tv);
      |            ~~~^~~~~~~~
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:2:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:98:15: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   98 |         tv=max(0ll,tv);
      |            ~~~^~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303: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:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:98:15: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   98 |         tv=max(0ll,tv);
      |            ~~~^~~~~~~~