题目链接
优先队列

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int arr[maxn];
int main(){
	int n,m,x;
	priority_queue<int, vector<int>, greater<int> > pq;
	while(cin>>n>>m){
		while(!pq.empty()){
			pq.pop();
		}
		for(int i=0;i<n;i++){
			cin>>x;
			if(pq.size() < m) pq.push(x);
			else if(pq.top() < x){
				pq.pop();
				pq.push(x);
			}
		}
		vector<int> ans;
		while(!pq.empty()){
			ans.push_back(pq.top());
			pq.pop();
		}
		reverse(ans.begin(), ans.end());
		for(int i=0;i<ans.size();i++){
			cout<<ans[i];
			if(i!=ans.size()-1) cout<<" ";
			else cout<<endl;
		}
	}
	
	
	return 0;
}