#include<bits/stdc++.h>

using namespace std;

int main(){

int n,k;
cin>>n>>k;
int a[n];
priority_queue<int , vector<int>,greater<int>> minh;
vector<int> vec;
for(int i=0;i<n;i++){
  cin>>a[i];
}
int t=0;
while(t<=n){
  minh.push(a[t]);
  if(minh.size()>k){
    int tp=minh.top();
    vec.push_back(tp);
    minh.pop();
  }
  t++;
}
while(minh.size()>0){
  int tp1=minh.top();
  vec.push_back(tp1);
  minh.pop();
}

for(int i=0;i<n;i++){
  cout<<vec[i]<<" ";
}


return 0;
}


Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: