#include <asm-generic/errno-base.h>
#include <iostream>
#include <algorithm>//sort的头文件
using namespace std;
struct Student {
string name;
int score;
int rank;//rank是用来表示输入顺序的变量
};
bool compare0(Student x, Student y) {
if (x.score == y.score) {
return x.rank < y.rank;//返回输入顺序用以解决题设“相同成绩都按先录入排列在前的规则处理。”
} else {
return x.score > y.score;
}
}
bool compare1(Student x, Student y) {
if (x.score == y.score) {
return x.rank < y.rank;//同上
} else {
return x.score < y.score;
}
}
int main() {
int n, f;
Student arr[1000];//变量太小好像会溢出,这里多分配一点
while (cin >> n >> f) {
for (int i = 0; i < n; ++i) {
cin >> arr[i].name >> arr[i].score;
arr[i].rank = i;//把输入顺序录入rank
}
if (f == 0) {
sort(arr, arr + n, compare0);
} else {
sort(arr, arr + n, compare1);
}
for (int j = 0; j < n; ++j) {
std::cout << arr[j].name << " " << arr[j].score << std::endl;
}
}
}