Language: C++

#include <bits/stdc++.h>
#define ll long long
#define ALL(x) x.begin(),x.end()
#define MOD (1000000000+7)
using namespace std;
#define PI 3.14159265359
#define tMOD 998244353
#define mkpr(x,y) make_pair(x,y)

int main(){
 
ll N,M,V,P;
cin>>N>>M>>V>>P;
vector<ll> A(N);
vector<pair<ll,ll>> a(N);
for(ll i=0;i<N;i++){
  cin>>A[i];
  
}

sort(ALL(A));
reverse(ALL(A));


if(V<=P){
//tooru
ll tmp = P;
ll tmp1 = A[P-1];
for(ll i=P;i<N;i++){
if(A[i]==tmp1){
tmp++;
}
else{
  break;
}
}

ll ans =tmp;
ll tt = tmp;
for(ll i=tt;i<N;i++){
  if(M+A[i]>=tmp1){
    ans++;
    //cout<<i<<" "<<M+a[i].first<<endl;
  }
}

cout<<ans<<endl;


return 0;
}



 
//tooru
ll tmp = P;
ll tmp1 = A[P-1];
for(ll i=P;i<N;i++){
if(A[i]==tmp1){
tmp++;
}
else{
  break;
}
}

ll yyy =N-1;
for(ll i=tt;i<N;i++){
  if(A[i]+M<tmp1){
    yyy=i-1;
    break;
  }
}
ll amari = V-P;
ll kaku = tmp;
ll ans =tmp;
ll tt = tmp;


ll l = tt;
ll r = yyy;

while(1){
if(r-l<=1){
  break;
}


ll mid = (l+r)/2;


ll i=mid;
ll tmp=0;


  //自分以下
  ll sita = N-1-i;
  ll gomi = amari - sita;
  if(gomi<=0){
  if(M+A[i]>=tmp1){
    tmp=1;
    //cout<<i<<" "<<M+a[i].first<<endl;
  }
  }
  else{
    ll aida = i - (P-2) -1;
     if(M+A[i]>=tmp1){
    if(gomi>=aida)
    tmp=0;
    else{

    priority_queue<ll> q;

    for(ll j=P-1;j<i;j++){
        q.push((M+A[i]-A[j]));
        //cout<<M+a[i].first-a[j].first<<endl;
    }
     for(ll j=0;j<M;j++){
        vector<ll> g(gomi);
        for(ll t=0;t<gomi;t++){
          g[t] = q.top()-1;
          q.pop();
        }
        for(ll t=0;t<gomi;t++){
          q.push(g[t]);
        }
    }

    ll tar =0;
    while(!q.empty()){
      tar = q.top();
      q.pop();
      
    }
    if(tar>=0){
      tmp=1;
    }
    else{
     tmp=0;
    }

    }

  }
  }

if(tmp==1){
  l = mid;
}
else{
  r = mid;
}


}


ll rra = l;
tmp=0;
ll i = l;
  //自分以下
  ll sita = N-1-i;
  ll gomi = amari - sita;
  if(gomi<=0){
  if(M+A[i]>=tmp1){
    tmp=1;
    //cout<<i<<" "<<M+a[i].first<<endl;
  }
  }
  else{
    ll aida = i - (P-2) -1;
     if(M+A[i]>=tmp1){
    if(gomi>=aida)
    tmp=0;
    else{

    priority_queue<ll> q;

    for(ll j=P-1;j<i;j++){
        q.push((M+A[i]-A[j]));
        //cout<<M+a[i].first-a[j].first<<endl;
    }
     for(ll j=0;j<M;j++){
        vector<ll> g(gomi);
        for(ll t=0;t<gomi;t++){
          g[t] = q.top()-1;
          q.pop();
        }
        for(ll t=0;t<gomi;t++){
          q.push(g[t]);
        }
    }

    ll tar =0;
    while(!q.empty()){
      tar = q.top();
      q.pop();
      
    }
    if(tar>=0){
      tmp=1;
    }
    else{
     tmp=0;
    }

    }

  }
  }




if(tmp==1){
 rra = l+1;
}

if(1==1){
tmp=0;
ll i = l+1;
  //自分以下
  ll sita = N-1-i;
  ll gomi = amari - sita;
  if(gomi<=0){
  if(M+A[i]>=tmp1){
    tmp=1;
    //cout<<i<<" "<<M+a[i].first<<endl;
  }
  }
  else{
    ll aida = i - (P-2) -1;
     if(M+A[i]>=tmp1){
    if(gomi>=aida)
    tmp=0;
    else{

    priority_queue<ll> q;

    for(ll j=P-1;j<i;j++){
        q.push((M+A[i]-A[j]));
        //cout<<M+a[i].first-a[j].first<<endl;
    }
     for(ll j=0;j<M;j++){
        vector<ll> g(gomi);
        for(ll t=0;t<gomi;t++){
          g[t] = q.top()-1;
          q.pop();
        }
        for(ll t=0;t<gomi;t++){
          q.push(g[t]);
        }
    }

    ll tar =0;
    while(!q.empty()){
      tar = q.top();
      q.pop();
      
    }
    if(tar>=0){
      tmp=1;
    }
    else{
    tmp=0;
    }

    }

  }
  }




if(tmp==1){
 rra = l+2;
}



}

cout<<rra<<endl;

return 0;


}


Compiler output:
a.cc: In function 'int main()':
a.cc:69:10: error: 'tt' was not declared in this scope; did you mean 'tm'?
   69 | for(ll i=tt;i<N;i++){
      |          ^~
      |          tm