/*
思路:
    1. 创建一个 struct student 结构体,里面包含学生 成绩 与 分数。
    2. 再创建一个数组 vector<struct student>, 把读到的学生放到数组中
    3. 使用 stable_sort 对数据进行排序
    4. 自定以 stable_sort 的排序函数
    5. 打印输出结果。
*/
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// 自定义比较函数
struct user{
    string name;
    int score;
};

bool myIncrease(struct user stTmpUser1, struct user stTmpUser2){
    return stTmpUser1.score < stTmpUser2.score;
}

bool myDecrease(struct user stTmpUser1, struct user stTmpUser2){
    return stTmpUser1.score > stTmpUser2.score;
}

int main() {
    int n, bisIncrease;
    cin >> n >> bisIncrease; 
    vector<struct user> userForm;
    struct user stTmpUser;
    for(int i = 0; i < n; i++){
        cin >> stTmpUser.name >> stTmpUser.score;
        userForm.push_back(stTmpUser);
    }

    if(bisIncrease){
        stable_sort(userForm.begin(), userForm.end(), myIncrease);
    }
    else{
        stable_sort(userForm.begin(), userForm.end(), myDecrease );
    }
    
    for(int i = 0; i < n; i++){
        cout << userForm[i].name << " " << userForm[i].score << endl;
    }

    return 0;
}
// 64 位输出请用 printf("%lld")