C++ 代码
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(){
int n , m;
cin >> n >> m;
priority_queue<int,vector<int>,less<int>>q;
while(n--){
int x;
cin >> x;
q.push(x);
if(q.size() > m){
q.pop();
}
}
vector<int> res;
while(!q.empty()){
res.push_back(q.top());
q.pop();
}
for(int i = res.size() - 1;i >= 0;--i) cout << res[i] << " ";
return 0;
}