#include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; struct Student{ string name; int score; int order; }; bool Compare1(Student a,Student b){ if(a.score==b.score){ return a.order<b.order; } else{ return a.score<b.score; } } bool Compare2(Student a,Student b){ if(a.score==b.score){ return a.order<b.order; } else{ return a.score>b.score; } } int main(){ int n; bool a; while(scanf("%d\n",&n)!=EOF){ Student array[n]; cin>>a; for(int i=0;i<n;++i){ cin>>array[i].name>>array[i].score; array[i].order=i; } if(a){ sort(array,array+n,Compare1); } if(!a){ sort(array,array+n,Compare2); } for(int i=0;i<n;++i){ cout<<array[i].name<<" "<<array[i].score<<endl; } } return 0; }