#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
int score;
char name[200];
} student;
int cmp1(const void* a, const void* b) {
student s1 = *(student*)a;
student s2 = *(student*)b;
return s1.score - s2.score;
}
int cmp2(const void* a, const void* b) {
student s1 = *(student*)a;
student s2 = *(student*)b;
return s2.score - s1.score;
}
int main() {
int n, kind;
student stu[200];
while (scanf("%d %d", &n, &kind) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%s %d", stu[i].name, &stu[i].score);
}
if (kind == 1) {
qsort(stu, n, sizeof(stu[0]), cmp1);
} else if (kind == 0) {
qsort(stu, n, sizeof(stu[0]), cmp2);
}
for (int i = 0; i < n; i++) {
printf("%s %d\n", stu[i].name, stu[i].score);
}
}
return 0;
}
// void bubbleSort(int *a, int n, int kind) {
// int temp;
// if (kind == 0) {
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n - i - 1; j++) {
// if (a[j] < a[j + 1]) {
// temp = a[j];
// a[j] = a[j + 1];
// a[j + 1] = temp;
// }
// }
// }
// }
// if (kind == 1) {
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n - i - 1; j++) {
// if (a[j] > a[j + 1]) {
// temp = a[j];
// a[j] = a[j + 1];
// a[j + 1] = temp;
// }
// }
// }
// }
// }
// int main() {
// int n, kind;
// while (scanf("%d %d", &n, &kind) != EOF) {
// int a[200];
// student stu[200];
// for (int i = 0; i < n; i++) {
// scanf("%s %d", stu[i].name, &stu[i].score);
// a[i] = stu[i].score;
// }
// bubbleSort(a, n, kind);
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// if (a[i] == stu[j].score) {
// printf("%s %d\n", stu[j].name, stu[j].score);
// stu[j].score = -1; // 标记该学生已经输出过
// break;
// }
// }
// }
// }
// return 0;
// }