#include <bits/stdc++.h>
using namespace std;
class people{
public:
string name;
int height;
};
bool cmp(people a,people b){
if(a.height==b.height)
return a.name<b.name;
else
return a.height>b.height;
}
int main(){
int n,k;
cin>>n>>k;
int front_row=n/k;
int last_row=n-front_row*(k-1);
vector<string>photo(k);
vector<people>a(n);
for(int i=0;i<n;i++)
cin>>a[i].name>>a[i].height;
sort(a.begin(),a.end(),cmp);
int pos=0;
for(int i=k-1;i>=0;i--){
string s="";
if(i==k-1){
s+=a[pos].name;
pos++;
int fangxiang=1;
for(int j=1;j<last_row;j++){
if(fangxiang){
s.insert(0," ");
s.insert(0,a[pos].name);
fangxiang=0;
pos++;
}
else{
s.push_back(' ');
s+=a[pos].name;
fangxiang=1;
pos++;
}
}
}
else{
s+=a[pos].name;
pos++;
int fangxiang=1;
for(int j=1;j<front_row;j++){
if(fangxiang){
s.insert(0," ");
s.insert(0,a[pos].name);
fangxiang=0;
pos++;
}
else{
s.push_back(' ');
s+=a[pos].name;
fangxiang=1;
pos++;
}
}
}
photo[i]=s;
}
for(int i=photo.size()-1;i>0;i--)
cout<<photo[i]<<endl;
cout<<photo[0];
}
|